带有 Eureka DiscoveryClient 的 Spring Boot 应用程序无法启动

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

我正在尝试编写一个简单的 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/lang/字符串; 在 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 发现组件时需要什么。

java spring spring-boot discovery netflix-eureka
4个回答
0
投票

你可以使用

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


0
投票

根据我的经验,当我使用 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;
}

0
投票

如果我们使用最新版本的 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 中的主类无需更改或@注解

请在此处查看我的GIT 存储库以获取工作代码。


0
投票

添加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
© www.soinside.com 2019 - 2024. All rights reserved.