spring-boot eureka客户端报错“Cannot execute request on any known server”

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

我正在用 spring-boot 试用尤里卡。我成功启动了尤里卡服务器。它在本地主机上运行:8761.
现在我尝试了 eureka-client。这就是代码

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!

这可能是什么?
请帮忙

java spring-boot netflix-eureka service-discovery
2个回答
2
投票

我有一个类似的问题,并设法通过添加解决它

@EnableEurekaClient

在我的客户端中的 SpintBootApplication 之上。


0
投票

我有一个类似的问题并设法通过添加来解决它 @EnableEurekaServer 在你的服务注册表主类中。

有关更多信息,请使用以下链接: 启用尤里卡服务器

谢谢

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