如何使用eureka.client.service-url:Spring Cloud中netflix eureka的属性

问题描述 投票:0回答:3

我正在尝试在其中一个微服务中添加 Eureka 客户端,但我无法弄清楚如何使用 service-url。

我正在使用 Spring-cloud 的 Greenwich.SR1 版本。

下面是我的应用程序.yml

spring:
  application:
    name: stock-service

server:
  port: 9901

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url: http://${eureka.instance.hostname}:9902/eureka/

我尝试搜索它,但到处都是我得到的旧方法,该版本不支持:

老方法:

eureka:         #tells about the Eureka server details and its refresh time
  instance:
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/ 

有人可以帮忙吗?

spring-boot spring-cloud-netflix
3个回答
11
投票

终于找到配置了:

spring:
  application:
    name: stock-service

server:
  port: 9901

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      default-zone: http://localhost:9902/eureka

3
投票

我刚刚在

Spring Cloud Hoxton.SR4
尝试了这个配置,但不起作用。 然后我找到了正确的方法(至少对我来说):

spring:
  application:
    name: hello-world-server

server:
  port: 8010

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9001/eureka/

启动客户端应用程序后,我们可以看到以下日志:

2020-05-02 16:39:21.914  INFO 27104 --- [           main] c.n.d.DiscoveryClient                    : Discovery Client initialized at timestamp 1588408761914 with initial instances count: 0
2020-05-02 16:39:21.915  INFO 27104 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application HELLO-WORLD-SERVER with eureka with status UP
2020-05-02 16:39:21.915  INFO 27104 --- [           main] c.n.d.DiscoveryClient                    : Saw local status change event StatusChangeEvent [timestamp=1588408761915, current=UP, previous=STARTING]
2020-05-02 16:39:21.916  INFO 27104 --- [nfoReplicator-0] c.n.d.DiscoveryClient                    : DiscoveryClient_HELLO-WORLD-SERVER/tumbleweed:hello-world-server:8010: registering service...

服务器端:

enter image description here

有效!


0
投票

仅按如下方式使用defaultZone:

spring:
  application:
    name: stock-service

server:
  port: 9901

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:9902/eureka/

如果您使用的是 Spring Boot,请使用 defaultZone 而不是带有 service-url 的 .properties 或 .yml 文件中的 default-zone。

这是因为 bean 尝试使用“defaultZone”读取它,所以找不到它。我在使用属性文件设置自己的实例时偶然发现了这一点,并且在此处进行了解释

© www.soinside.com 2019 - 2024. All rights reserved.