本问题是部署问题所以很麻烦一遍一遍打包,一遍一遍测试,终于得以解决这破壁问题。。。。。。。
开始!
这里配置打包的步骤就不写了,直接写解决问题的步骤。
介绍: Spring Cloud 2.1之后 注册服务是通过今天WebServerInitializedEvent 事件进行的,这个事件是干啥的呢
就是算计应用程序上下文并准备好web服务器后发布,用于获取正在运行的服务器的本地端口。
但是现在通过tomcat容器启动服务时监听不到WebServerInitializedEvent事件 。
那就自己创建一个事件去监听
代码:
package cn.com.dyrs.cmp.structure.provider.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
/**
* @author 荡漾
* @title: ConsulEventLinsiner
* @projectName cmp
* @description: TODO Consul注册
* @date 2019/7/12 12:58
*/
@Component
public class ConsulEventLinsiner implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
ConsulAutoServiceRegistration consulAutoServiceRegistration;
@Autowired
ConsulDiscoveryProperties properties;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
consulAutoServiceRegistration.start();
}
}
添加好这个还没玩,还要改一个地方,就是consul 的 discovery 参数
到配置文件当中添加一个port 加的是你工程的端口
这个不加还是会报错的 这是错误信息:service.port has not been set
这就玩活了
参考:
https://cloud.tencent.com/developer/news/59888
https://github.com/spring-cloud/spring-cloud-consul/issues/302
https://blog.csdn.net/qq_24842293/article/details/84620066