【问题标题】:Spring Boot app with Eureka DiscoveryClient fails to start带有 Eureka DiscoveryClient 的 Spring Boot 应用程序无法启动
【发布时间】:2016-03-21 18:43:34
【问题描述】:

我正在尝试编写一个简单的 Spring Boot 应用程序,它可以 (1) 向 Netflix Eureka 服务器注册,以及 (2) 查询 Eureka 服务器以检索其他注册服务的详细信息。

我的客户端类有一个 @Autowired 类型为 com.netflix.discovery.DiscoveryClient 的字段,用于与 Eureka 对话并查询它以了解其他服务。在我的主课上,我有注释@EnableDiscoveryClient:

@SpringBootApplication
@EnableDiscoveryClient
public class AppBootstrap {

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

}

在我的 src/main/resources 下的 application.yml 文件中,我有:

eureka:
    instance:
         lease-renewal-interval-in-seconds: 10
         lease-expiration-duration-in-seconds: 20
         prefer-ip-address: true
         secure-port: 443
         non-secure-port: 80
         metadata-map:
             instanceId: my-test-instance
    client:
         service-url:
             defaultZone: http://localhost:9080/eureka/
         registry-fetch-interval-seconds: 6
         instance-info-replication-interval-seconds: 6
         register-with-eureka: true
         fetch-registry: true
         heartbeat-executor-thread-pool-size: 5
         eureka-service-url-poll-interval-seconds: 10

当我启动我的应用程序时,服务无法启动,引发异常,其根源在于:

原因:java.lang.AbstractMethodError: org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean.getInstanceI d()Ljava/语言/字符串; 在 com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider.get(EurekaConfigBasedInstanceInfoProvider .java:53) 在 com.netflix.appinfo.ApplicationInfoManager.initComponent(ApplicationInfoManager.java:90) ... 25 更多

我不知道这里发生了什么。有任何想法吗?我相信即使我的 Eureka 配置不正确,该应用程序仍应启动,但它在启动时会崩溃。

其次,我是否使用了正确的 DiscoveryClient?理想情况下,我想让它通用,以便我可以将它与 Eureka、Consul 或 ZooKeeper 一起使用作为示例。我发现文档没有很好地说明使用这些 Spring Cloud / Netflix 发现组件时需要什么。

【问题讨论】:

  • 您使用的 eureka-client 版本与 Spring Cloud Netflix 不兼容。为避免此类问题,您应该使用 Spring Cloud 的依赖管理。
  • 我对 Spring Cloud 依赖管理没有太多经验。 ..你能给我举个例子吗?
  • 这取决于您使用的是 Maven 还是 Gradle。尝试在the documentation 中搜索bom

标签: java spring spring-boot discovery netflix-eureka


【解决方案1】:

你可以使用

org.springframework.cloud.client.discovery.DiscoveryClient

然后你可以通过discoveryClient.getInstances获取实例列表

ServiceInstance instance = discoveryClient.getInstances(service).get(0);
instance.getUri().toString();

如果你使用RestTemplate,Ribbon等其他组件,你只需要在URL中使用服务的名称(在eureka中注册的名称)

restTemplate.getForObject("http://PRODUCTSMICROSERVICE/products/{id}", Product.class, id)

你可以在这里看到更多

https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka

【讨论】:

    【解决方案2】:

    当我使用discoveryclient 在任何函数之外的类中获取信息时,我收到了自动装配错误。因此,我使用 eureka 来查找我的服务的端口,因为该端口被描述为 0,因此服务在作为 Spring Boot 应用程序启动时动态拾取端口。我需要以编程方式了解端口。在控制器中,我以错误的方式使用了如下代码

    public class HelloController {
    
    private static final Logger LOG = LoggerFactory.getLogger(HelloController.class);
    
    @Autowired
    private DiscoveryClient discoveryClient;
    
    int port = discoveryClient.getLocalServiceInstance().getPort();
    
    
    @RequestMapping("/hello/{id}")
    public String  sayhello(@PathVariable String id)
    {
        String s ="A very nice and warm welcome to the world "+id;
                LOG.info(String.format("calling helloservice for %s",id));
        LOG.info(String.format("calling helloservice for port %d",port));
        return s;
    } 
    

    一旦我将端口代码放入 sayhello 方法中,错误就消失了。所以获取端口的正确方法如下

    public class HelloController {
    
    private static final Logger LOG = LoggerFactory.getLogger(HelloController.class);
    
    @Autowired
    private DiscoveryClient discoveryClient;
    
    
    
    @RequestMapping("/hello/{id}")
    public String  sayhello(@PathVariable String id)
    {
        String s ="A very nice and warm welcome to the world "+id;
        int port = discoveryClient.getLocalServiceInstance().getPort();
        LOG.info(String.format("calling helloservice for %s",id));
        LOG.info(String.format("calling helloservice for port %d",port));
        return s;
    }
    

    【讨论】:

      【解决方案3】:

      如果我们使用最新版本的 Spring Boot,那么我们不需要在主类中定义 @EnableDiscoveryClient 或 @EnableEurekaClient。当我们在 pom.xml 中添加依赖项时,Spring 会在后台发生这种情况

      请确保您的文件具有以下基本信息。

      pom.xml

          <properties>
              <java.version>1.8</java.version>
              <spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      

      application.properties 或 YAML 文件(根据选择)

      spring.application.name=eureka-client
      
      eureka.client.service-url.defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
      eureka.instance.prefer-ip-address= true
      server.port= 8082
      

      Application.java 的主类中不需要更改或@Annotations

      请查看我的GIT Repository here 以获取工作代码。

      【讨论】:

        【解决方案4】:

        添加application.yml文件这些设置;

        我们的产品应用程序在这个端口运行

        服务器: 端口:8482

        我们的服务将使用自己的服务名称注册

        弹簧: 应用 : 名称:产品-服务

        # To be register we assign eureka service url
        eureka:
           client:
             service-url :
                defaultZone:
                    ${EUREKA_URI:http://localhost:8481/eureka} # add your port where your eureka server running
           instance :
              prefer-ip-address : true
        
        # Logging file path
        logging :
           file :
              path : target/${spring.application.name}.log
        

        【讨论】:

          猜你喜欢
          • 2022-08-21
          • 2021-02-22
          • 2015-05-10
          • 2020-08-04
          • 1970-01-01
          • 1970-01-01
          • 2018-08-17
          相关资源
          最近更新 更多