解决Spring Cloud 部署war包到tomcat 注册不到consul 服务

本问题是部署问题所以很麻烦一遍一遍打包,一遍一遍测试,终于得以解决这破壁问题。。。。。。。

开始!

这里配置打包的步骤就不写了,直接写解决问题的步骤。

介绍:    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

https://ask.csdn.net/questions/724241

https://spring.io/projects/spring-cloud-consul

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值