配置高可用Eureka Server遇到的神坑:无法注册服务

今天在进行高可用的Eureka Server注册中心的配置时,遇到一个神坑。

拓扑图

废话不多说,直接上拓扑图:
这里写图片描述

Eureka Server配置

pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

开启注解:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerHaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerHaApplication.class, args);
    }
}

配置文件application.yml:

---
server:
  port: 8771
spring:
  profiles: peer1
  application:
    name: eureka-ha
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8772/eureka/,http://peer3:8773/eureka/

---
server:
  port: 8772
spring:
  profiles: peer2
  application:
    name: eureka-ha
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8771/eureka/,http://peer3:8773/eureka/

---
server:
  port: 8773
spring:
  profiles: peer3
  application:
    name: eureka-ha
eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
      defaultZone: http://peer1:8771/eureka/,http://peer2:8772/eureka/

在IDEA中,分别通过设置不同的Program arguments参数为--spring.profiles.active=peer1--spring.profiles.active=peer2--spring.profiles.active=peer3启动这三个注册中心服务。

访问http://peer1:8771/Eureka Server配置成功,如下所示:
这里写图片描述

Eureka Client配置

pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

开启注解:

@EnableEurekaClient
@RestController
@SpringBootApplication
public class EurekaHiHaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHiHaApplication.class, args);
    }

    /** 获取端口号 */
    @Value("${server.port}")
    String port;

    /**
     * 定义一个简单接口
     * @param name
     * @return
     */
    @GetMapping("/hi/{name}")
    public String home(@PathVariable String name){
        return "hi " + name + ",I am from port :" + port;
    }
}

配置文件application.yml:

server:
  port: 8783
spring:
  application:
    name: service-hi-ha
eureka:
  client:
    service-url:
      default-zone: http://peer1:8771/eureka/

启动报错:

2018-05-26 18:31:17.438  INFO 12972 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2018-05-26 18:31:19.525 ERROR 12972 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar:1.19.1]
    ...
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_131]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_131]
    ...
    ... 69 common frames omitted

2018-05-26 18:31:19.531  WARN 12972 --- [           main] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.ConnectException: Connection refused: connect
2018-05-26 18:31:19.533 ERROR 12972 --- [           main] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICE-HI-HA/DESKTOP-4J0FACQ:service-hi-ha:8783 - was unable to refresh its cache! status = Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.6.2.jar:1.6.2]
    ...
    at pers.hanchao.springclouddemo.eurekahiha.EurekaHiHaApplication.main(EurekaHiHaApplication.java:17) [classes/:na]

2018-05-26 18:31:19.543  WARN 12972 --- [           main] com.netflix.discovery.DiscoveryClient    : Using default backup registry implementation which does not do anything.
2018-05-26 18:31:19.543  INFO 12972 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2018-05-26 18:31:19.543  INFO 12972 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2018-05-26 18:31:19.543  INFO 12972 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1527330679543 with initial instances count: 0
2018-05-26 18:31:19.583  INFO 12972 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application service-hi-ha with eureka with status UP
2018-05-26 18:31:19.583  INFO 12972 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1527330679583, current=UP, previous=STARTING]
2018-05-26 18:31:19.583  INFO 12972 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICE-HI-HA/DESKTOP-4J0FACQ:service-hi-ha:8783: registering service...
2018-05-26 18:31:19.683  INFO 12972 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8783 (http)
2018-05-26 18:31:19.683  INFO 12972 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8783
2018-05-26 18:31:19.693  INFO 12972 --- [           main] p.h.s.eurekahiha.EurekaHiHaApplication   : Started EurekaHiHaApplication in 6.824 seconds (JVM running for 7.581)
2018-05-26 18:31:21.624 ERROR 12972 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    ...
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_131]
    ...
    ... 30 common frames omitted

2018-05-26 18:31:21.634  WARN 12972 --- [nfoReplicator-0] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.ConnectException: Connection refused: connect
2018-05-26 18:31:21.634  WARN 12972 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SERVICE-HI-HA/DESKTOP-4J0FACQ:service-hi-ha:8783 - registration failed Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.6.2.jar:1.6.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.6.2.jar:1.6.2]
    ...
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

2018-05-26 18:31:21.634  WARN 12972 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.6.2.jar:1.6.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.6.2.jar:1.6.2]
    ...
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

错误解决

通过报错信息,可知,Eureka Client找不到Eureka Server的配置,但是我检查各项配置没有问题。

最终,我将 横线命名 修改为 驼峰命名 ,问题解决:

修改Eureka Client的配置文件application.yml:

server:
  port: 8783
spring:
  application:
    name: service-hi-ha
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8771/eureka/

重新启动,进入Eureka Server后台,发现服务注册成功:

这里写图片描述

在浏览器访问http://localhost:8783/hi/david,服务正常:
这里写图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页