Java 带有Eureka DiscoveryClient的Spring启动应用程序无法启动
我正在尝试编写一个简单的Spring引导应用程序,它可以(1)向Netflix Eureka服务器注册,(2)查询Eureka服务器以检索其他注册服务的详细信息 我的客户端类有一个类型为Java 带有Eureka DiscoveryClient的Spring启动应用程序无法启动,java,spring,spring-boot,discovery,netflix-eureka,Java,Spring,Spring Boot,Discovery,Netflix Eureka,我正在尝试编写一个简单的Spring引导应用程序,它可以(1)向Netflix Eureka服务器注册,(2)查询Eureka服务器以检索其他注册服务的详细信息 我的客户端类有一个类型为com.netflix.discovery.DiscoveryClient的@Autowired字段,用于与Eureka对话并查询它以了解其他服务。在我的主类上,我有注释@EnableDiscoveryClient: @SpringBootApplication @EnableDiscoveryClient pu
com.netflix.discovery.DiscoveryClient
的@Autowired
字段,用于与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/String;
在com.netflix.appinfo.providers.eurekanconfigbasedinstanceinfoprovider.get(eurekanconfigbasedinstanceinfoprovider
.java:53)
在com.netflix.appinfo.ApplicationInfoManager.initComponent上(ApplicationInfoManager.java:90)
... 还有25个
我不知道这里发生了什么。有什么想法吗?我相信即使我的Eureka配置不正确,应用程序仍然应该启动,但它会在启动时崩溃
其次,我是否使用了正确的DiscoveryClient?理想的情况下,我想让它更一般,这样我就可以用尤里卡,领事或动物园管理员作为例子。我发现这些文档并不擅长准确地说明使用这些Spring Cloud/Netflix discovery组件时所需的内容。您可以使用
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)
你可以在这里看到更多
您可以使用
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)
你可以在这里看到更多
根据我的经验,当我使用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;
}
根据我的经验,当我使用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;
}
如果我们使用的是最新版本的Spring Boot,那么就不需要在主类中定义@EnableDiscoveryClient或@EnableEurekClient。当我们在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.java中的主类不需要更改或@Annotation
请检查my以获取工作代码。如果我们使用的是最新版本的Spring Boot,那么我们不需要在主类中定义@EnableDiscoveryClient或@EnableEurekClient。当我们在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.java中的主类不需要更改或@Annotation
请在my中签出工作代码。添加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
将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
您使用的是与Spring Cloud Netflix不兼容的
eureka client
。为了避免这样的问题,你应该使用SpringCloud的依赖关系管理。。你能给我举个例子吗?这取决于你使用的是Maven还是Gradle。请尝试在中搜索bom
。您使用的eureka client
版本与Spring Cloud Netflix不兼容。为了避免这样的问题,你应该使用SpringCloud的依赖关系管理。。你能给我举个例子吗?这取决于你使用的是Maven还是Gradle。尝试在中搜索bom
。