Spring 自由云

Spring Cloud with Liberty

我目前 运行 Websphere Liberty 中的 Spring 引导应用程序,并使用 Consul 进行服务发现。为了向 Consul 注册服务,我创建了一个 Liberty 功能部件,它连接到应用程序生命周期事件并执行注册/注销。这很好用,但这样做我将自己与 Liberty 结合起来。 Spring-Cloud-Consul 看起来可能会解决这个问题,但我无法让它向 Liberty 注册服务(它确实连接到 Consul)——只能使用嵌入式 Tomcat 服务器。查看 Spring-Cloud-Consul 代码后,问题是没有触发 EmbeddedServletContainerInitializedEvent,因此没有设置端口。

我的问题是,Spring Cloud Consul 是否仅适用于嵌入式 servlet 容器?

这是一个已知问题。欢迎提交 PR。 https://github.com/spring-cloud/spring-cloud-consul/issues/173

我的解决方案是将 spring-cloud-consul 的 ConsulLifecycle class 带到本地并添加一个 ApplicationReadyEvent,如下所示:

@Autowired(required = false)
private ServerProperties serverProperties;

@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
    this.setConfiguredPort(serverProperties.getPort());
    log.info("Application ready on port " + serverProperties.getPort());
    this.start();
}

现在我的服务按预期注册和注销。