如何解决spring-boot eureka 客户端错误“无法在任何已知服务器上执行请求”
我正在使用 spring-boot 尝试 eureka。我成功启动了 eureka-server。它在 localhost:8761 上运行。
现在我尝试了尤里卡客户端。代码是这样的
import ch.qos.logback.classic.Logger;
import java.util.List;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.discoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class,args);
}
}
@RestController
class EurekaTestController {
private static Logger LOGGER = (Logger)LoggerFactory.getLogger(EurekaTestController.class);
@Autowired
discoveryClient discoveryClient;
@GetMapping("/service-instances/{applicationName}")
public List<ServiceInstance> getServiceInstance(@PathVariable("applicationName") String appName) {
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(appName);
LOGGER.info("{}",serviceInstances);
return discoveryClient.getInstances(appName);
}
}
pom.xml 如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shades</groupId>
<artifactId>service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
为此项目定义的属性
server.port=8080
eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://localhost:8761/eureka}
spring.application.name=eureka-client
现在,当我在浏览器中打开 http://localhost:8761/eureka 并在 eureka-client 应用程序上方启动时,它会显示应用程序处于 UP 状态。但是当我在应用程序的日志中看到这一点时。
Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/},exception=Error while extracting response for type [class org.springframework.cloud.netflix.eureka.http.EurekaApplications] and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.netflix.appinfo.InstanceInfo>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.netflix.appinfo.InstanceInfo>` out of START_OBJECT token
at [Source: (pushbackinputstream); line: 1,column: 122] (through reference chain: com.netflix.discovery.converters.jackson.builder.ApplicationsJacksonBuilder["application"]->java.util.ArrayList[0]->com.netflix.discovery.shared.Application["instance"]) stacktrace=org.springframework.web.client.RestClientException: Error while extracting response for type [class org.springframework.cloud.netflix.eureka.http.EurekaApplications] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.netflix.appinfo.InstanceInfo>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.netflix.appinfo.InstanceInfo>` out of START_OBJECT token
at [Source: (pushbackinputstream); line: 1,column: 122] (through reference chain: com.netflix.discovery.converters.jackson.builder.ApplicationsJacksonBuilder["application"]->java.util.ArrayList[0]->com.netflix.discovery.shared.Application["instance"])
后来也看到了这个
2021-01-14 19:13:20.586 WARN 7970 --- [tbeatExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution Failed with message: Error while extracting response for type [class com.netflix.appinfo.InstanceInfo] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Root name 'timestamp' does not match expected ('instance') for type [simple type,class com.netflix.appinfo.InstanceInfo]; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'timestamp' does not match expected ('instance') for type [simple type,class com.netflix.appinfo.InstanceInfo]
at [Source: (pushbackinputstream); line: 1,column: 2] (through reference chain: com.netflix.appinfo.InstanceInfo["timestamp"])
2021-01-14 19:13:20.590 ERROR 7970 --- [tbeatExecutor-0] com.netflix.discovery.discoveryClient : discoveryClient_EUREKA-CLIENT/192.168.43.183:eureka-client:8080 - was unable to send heartbeat!
这可能是什么?
请帮忙
解决方法
我遇到了类似的问题并设法通过添加来解决
@EnableEurekaClient
在我的客户端中的 SpintBootApplication 之上。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。