consul注册报错 connectex: No connection could be made because the target machine actively refused it.

  • Get http://localhost:2000/actuator/health: dial tcp [::1]:2000: connectex: No connection could be made because the target machine actively refused it.
  • Copy Output

项目springboot 注册向consul时,健康检查失败,可以看到上面的连接地址是 localhost:2000/actuator/health ,consul与微服务在不同主机上,就会出现以上错误.

原因是配置properties有问题,

配置如下

spring.application.name=XXX
server.port=9114
spring.cloud.consul.host=192.168.0.17
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.serviceName=service-XXX

解决方式:

1.配置hostname

spring.cloud.consul.discovery.hostname=192.168.0.240

2.配置健康检查地址

spring.cloud.consul.discovery.healthCheckUrl=http://192.168.0.240:9113/actuator/health

以下是向consul注册服务的spring源码,

下面也附上具体的类的名称方便找到方法进行查看

1.注册所有class

//类名
org.springframework.context.annotation.ConfigurationClassEnhancer//方法static {CALLBACKS = new Callback[]{new ConfigurationClassEnhancer.BeanMethodInterceptor(), new ConfigurationClassEnhancer.BeanFactoryAwareMethodInterceptor(), NoOp.INSTANCE};CALLBACK_FILTER = new ConfigurationClassEnhancer.ConditionalCallbackFilter(CALLBACKS); //遍历logger = LogFactory.getLog(ConfigurationClassEnhancer.class);objenesis = new SpringObjenesis();}

2.通过properies写入hostname

//类名
org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties//方法public void setHostname(String hostname) {this.hostname = hostname;this.hostInfo.override = true;}

3.开始注册,传入参数

//类名
org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration//方法@Bean@ConditionalOnMissingBeanpublic ConsulAutoRegistration consulRegistration(AutoServiceRegistrationProperties autoServiceRegistrationProperties, ConsulDiscoveryProperties properties, ApplicationContext applicationContext, ObjectProvider<List<ConsulRegistrationCustomizer>> registrationCustomizers, HeartbeatProperties heartbeatProperties) {return ConsulAutoRegistration.registration(autoServiceRegistrationProperties, properties, applicationContext, (List)registrationCustomizers.getIfAvailable(), heartbeatProperties);}

4.写入参数

//类名
org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration//方法
public static ConsulAutoRegistration registration(AutoServiceRegistrationProperties autoServiceRegistrationProperties, ConsulDiscoveryProperties properties, ApplicationContext context, List<ConsulRegistrationCustomizer> registrationCustomizers, HeartbeatProperties heartbeatProperties) {NewService service = new NewService();String appName = getAppName(properties, context.getEnvironment());service.setId(getInstanceId(properties, context));if (!properties.isPreferAgentAddress()) {service.setAddress(properties.getHostname());}service.setName(normalizeForDns(appName));service.setTags(createTags(properties));if (properties.getPort() != null) {service.setPort(properties.getPort());setCheck(service, autoServiceRegistrationProperties, properties, context, heartbeatProperties);}ConsulAutoRegistration registration = new ConsulAutoRegistration(service, autoServiceRegistrationProperties, properties, context, heartbeatProperties);customize(registrationCustomizers, registration);return registration;}

5.请求注册

//类名
org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration//方法public void start() {if (!this.isEnabled()) {if (logger.isDebugEnabled()) {logger.debug("Discovery Lifecycle disabled. Not starting");}} else {if (!this.running.get()) {this.register();  //注册if (this.shouldRegisterManagement()) {this.registerManagement();}this.context.publishEvent(new InstanceRegisteredEvent(this, this.getConfiguration()));this.running.compareAndSet(false, true);}}}
package com.ecwid.consul.v1.agent.modelpublic class NewService {@SerializedName("ID") private String id;  //XXX-9114@SerializedName("Name")private String name;  //service-XXX@SerializedName("Tags")private List<String> tags;@SerializedName("Address")private String address;        //properties.getHostname()@SerializedName("Port")private Integer port;@SerializedName("EnableTagOverride")private Boolean enableTagOverride;@SerializedName("Check")private NewService.Check check;@SerializedName("Checks")private List<NewService.Check> checks;....省略GetSet
}

6.发送请求,到consul注册

//类名com.ecwid.consul.v1.agent.AgentConsulClient;//方法public Response<Void> agentServiceRegister(NewService newService, String token) {UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;String json = GsonFactory.getGson().toJson(newService); /*{"ID":"oms-xt-plan-9114","Name":"service-oms-xt-plan","Tags":["secure\u003dfalse"],"Address":"localhost","Port":9114,"Check":{"Interval":"10s","HTTP":"http://192.168.0.240:9114/actuator/health"}}*/RawResponse rawResponse = this.rawClient.makePutRequest("/v1/agent/service/register", json, new UrlParameters[]{tokenParam});if (rawResponse.getStatusCode() == 200) {return new Response((Object)null, rawResponse);} else {throw new OperationException(rawResponse);}}

7.发送put 请求

//类名
com.ecwid.consul.transport.AbstractHttpTransport;//方法
public RawResponse makePutRequest(String url, String content) { /*content =  {"ID":"oms-xt-plan-9114","Name":"service-oms-xt-plan","Tags":["secure\u003dfalse"],"Address":"localhost","Port":9114,"Check":{"Interval":"10s","HTTP":"http://192.168.0.240:9114/actuator/health"}} */HttpPut httpPut = new HttpPut(url);         //PUT http://192.168.0.17:8500/v1/agent/service/register?token= HTTP/1.1httpPut.setEntity(new StringEntity(content, UTF_8));return this.executeRequest(httpPut);}
//类名
com.ecwid.consul.v1.ConsulRawClient;//方法public RawResponse makePutRequest(String endpoint, String content, UrlParameters... urlParams) {String url = this.prepareUrl(this.agentAddress + endpoint);
//  http://192.168.0.17:8500/v1/agent/service/registerurl = Utils.generateUrl(url, urlParams);                //http://192.168.0.17:8500/v1/agent/service/register?token=return this.httpTransport.makePutRequest(url, content);}

consul注册报错 connectex: No connection could be made because the target machine actively refused it.相关推荐

  1. 解决grpc连接报错connectex: No connection could be made because the target machine actively refused it

    如有帮助,欢迎留下足迹哦! 详情如下 code = Unavailable desc = connection error: desc = "transport: Error while d ...

  2. 关于docker报错:No connection could be made because the target machine actively refused it.

    我是在win10下跑的docker 事情是这样的,今天安装好douker跑起来后报了一个很奇怪的错误` C:\Users\Administrator>docker ps error during ...

  3. connectex: No connection could be made because the target machine actively refused it.

    关于阿里云服务器出现以下错误的原因: connectex: No connection could be made because the target machine actively refuse ...

  4. 【TDengine】解决 connectex: No connection could be made because the target machine actively refused it.

    目录 1.遇到问题 2.问题描述 3.问题解决 1.遇到问题 .connectex: No conne ction could be made because the target machine a ...

  5. Redis错误:No connection could be made because the target machine actively refused it

    问题: 利用go连接远程阿里云centos主机的redis,代码如下: redisCli := redis.NewClient(&redis.Options{Addr: url,Passwor ...

  6. nginx connect()错误(10061: No connection could be made because the target machine actively refused it)

    nginx偶尔会报下面这个错误: 2019/05/20 10:54:56 [error] 717920#661892: *16943633 connect() failed (10061: No co ...

  7. connect() failed (10061: No connection could be made because the target machine actively refused it

    nginx运行web前端项目的时候出现了以下错误 项目在加载保存的图片的过程中,报错了,看了一下Nginx的错误日志 日志出现了connect() failed (10061: No connecti ...

  8. No connection could be made because the target machine actively refused it 127.0.0.1:8888

    No connection could be made because the target machine actively refused it 127.0.0.1:8888 你妹的微软 用的Wi ...

  9. No connection could be made because the target machine actively refused it [::1]:808

    No connection could be made because the target machine actively refused it [::1]:808 1.首先查看端口占用情况, 在 ...

最新文章

  1. 2018CTF大赛学习
  2. java属性监听_Java event事件监听属性值变化 demo
  3. RGB、YUV像素基础知识及处理数据
  4. OpenGL.tutorial06键盘和鼠标
  5. Sqoop 数据增量导出,--update-key添加多个字段
  6. 基于51单片机和555定时器的电阻电感电容测量装置设计
  7. 联想微型计算机开机黑屏什么原因,联想笔记本电源键亮但黑屏怎么办
  8. 大数据分析“平民化”演进精准营销
  9. FLOWABLE 流程中的自动跳过
  10. JS实现数字自动转换人民币金额(自动格式化输入的数字/千位分隔符)
  11. 快手涨收近95港币,年初至今已涨超30%
  12. android 计算图片大小
  13. OpenCV 陷波滤波器消除周期性噪音 C++
  14. 利用vs将cs文件编译成dll文件
  15. 【我参加NVIDIA Sky Hackathon】感悟篇
  16. html表白earth,浪漫表白的英文句子大全
  17. 数据源为Excel的解决方法
  18. APS(高级计划与排程)基本概念
  19. 计算机应用自荐书中专1000,中专生的自荐书
  20. 飞行棋小游戏 C#编程记录

热门文章

  1. 前端实现input标签输入框密码框显示文字效果
  2. day 06 非空约束、唯一约束、主键约束、外键约束
  3. Coursera | Grammer and Punctuation(UCI) | Quiz
  4. 生产消费者模式实例(多线程实现价格监控)
  5. vue遍历map对象
  6. Appium的实现原理
  7. 一看就会一学就废之SpringBoot整合通用Mapper以及常用方法
  8. Gitlab Failed to squash
  9. 在Docker中使用Oracle 18c(12.2.0.2)
  10. 有效沟通bic法则_职场生存法则你get了吗?