2019-06-04 18:37:36.525  INFO 7128 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2019-06-04 18:37:36.525  INFO 7128 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2019-06-04 18:37:38.729 ERROR 7128 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}

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]
	at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.8.jar:1.9.8]
	at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]

简单介绍一下,我是本地搭了应该eureka服务,config-server服务,客户端client服务。

我让client服务通过config-server服务进行配置,bootstrap.yml:

spring:
  cloud:
    config:
      uri: http://localhost:8551/ # 配置中心的具体地址
      name: spring-config  # 对应{application}部分
      profile: master  # 对应{profile}部分
      label: master  # 对应git的分支,如果配置中心使用的是本地存储,则该参数无用

这样服务都启动正常,可以访问到配置文件。

我想让client服务通过eureka发现config-server,而不是在配置文件里把config-server的url硬编码,所以我改了配置文件:

spring:
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-server-my
#      uri: http://localhost:8551/ # 配置中心的具体地址
      name: spring-config  # 对应{application}部分
      profile: master  # 对应{profile}部分
      label: master  # 对应git的分支,如果配置中心使用的是本地存储,则该参数无用

通过discovery去eureka发现服务,然后启动就报错,报错信息就是上面的。报错是因为在Getting all instance registry info from the eureka server的时候连接不到eureka服务,然后去eureka的默认地址({ serviceUrl='http://localhost:8761/eureka/})去找,报错。但是我配置了eureka的地址,在application.yml里,如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:7001/eureka/
  instance:
    instanceId: client-01
    preferIpAddress: true

我在网上查原因,好多都是说是配置写的有问题,包括驼峰命名啊啥的,但是都不起作用。

解决办法:

是因为bootstrap.yml和application.yml的加载顺序导致的啊,项目启动的时候,先加在bootstrap.yml,再加在application.yml。最开始是使用uri: http://localhost:8551/ # 配置config-server,这个时候就没用到eureka,所以可以正常启动。但是修改为通过eureka发现服务的时候,由于之前的bootstrap.yml里并没有eureka的配置,所以根本不知道eureka的访问路径,所以就报错了,访问不到eureka。

所以只需要将eureka的配置提到bootstrap.yml里,就一切正常了。

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:7001/eureka/
  instance:
    instanceId: client-01
    preferIpAddress: true

spring:
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-server-my
#      uri: http://localhost:8551/ # 配置中心的具体地址
      name: spring-config  # 对应{application}部分
      profile: master  # 对应{profile}部分
      label: master  # 对应git的分支,如果配置中心使用的是本地存储,则该参数无用

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐