震惊!【Eureka】发生Cannot execute request on any known server异常这样解决

简介: 震惊!【Eureka】发生Cannot execute request on any known server异常这样解决

如题异常相对会出现的概率较大,原因也不一,以下也就不单说以上异常了,具体说下各部分我运行正常后的配置

首先说EUREKA-server,

pom.xml

关键jar包

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--eureka安全登录验证需要-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-security</artifactId>-->
        <!--</dependency>-->
</dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

如题异常大多说的是服务端配置这里:

eureka:
  client:
#    往eureka注册中心注册自己,默认为true
    register-with-eureka: false
#    单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false
    fetch-registry: false

如果上述无误,那么往下继续看配置:

server:
  port: 8087
  servlet:
    context-path: /eureka
eureka:
  instance:
    hostname: localhost
    appname: eurekaServer
  client:
#    自己的eureka中注册自己,默认为true
    register-with-eureka: false
#    单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false
    fetch-registry: false
    service-url:
#      defaultZone: http://127.0.0.1:${server.port}/eureka/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/eureka/

service-url的defaultZone配置:

一般设置不对的话会出现此异常【MismatchedInputException: Root name 'timestamp' does not match expected......】

1.如果在【server.servlet.context-path】中配置了【context-path】,如上图我的是 【/eureka】,那么defaultZone应该为:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/eureka/

否则,如果没有配置【server.servlet.context-path】,应该为:

defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

以下是完整的EUREKA-server配置:

server:
  port: 8087
  servlet:
    context-path: /eureka
eureka:
  instance:
    hostname: localhost
    appname: eurekaServer
  client:
#    自己的eureka中注册自己,默认为true
    register-with-eureka: false
#    单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false
    fetch-registry: false
    service-url:
#      defaultZone: http://127.0.0.1:${server.port}/eureka/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/eureka/
  server:
    #设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
    #会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
    #实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
    enable-self-preservation: true
    #扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
    eviction-interval-timer-in-ms: 10000

最后的server依各人需求,可配可不配

启动类入口处需要装配以下注解:

@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

下来说EUREKA-client

pom.xml

关键jar包

    <dependencies>
        <!--  添加springboot fegin依赖,product项目即可以作为生产者,又可以作为消费者-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

【application.yml配置】

spring:
  application:
    name: mixer-server
eureka:
  instance:
    appname: mixer-server

以上appname 和 name 二选其一用来标识在EUREKA-server内的name

02258a1371f11ff8f7249062226967e.png

对应EUREKA-server内的defauleZone

spring:
  application:
    name: mixer-server
eureka:
  #与spring.application.name二选其一作为euraka-server服务标识,此处我使用的是前者
  #instance:
    #appname: user-server
  client:
    service-url:
      defaultZone: http://localhost:8087/eureka/eureka/

启动类入口处需要装配如图注解:

@SpringBootApplication
//@EnableEurekaClient 和 @EnableDiscoveryClient 都是让eureka发现该服务并注册到eureka上的注解
//相同点:都能让注册中心Eureka发现,并将该服务注册到注册中心上;
//不同点:@EnableEurekaClient只适用于Eureka作为注册中心,
//而@EnableDiscoveryClient可以是其他注册中心;
//@EnableDiscoveryClient
@EnableEurekaClient
//表示开启Fegin客户端a
@EnableFeignClients
public class MixerStationApplication {
    public static void main(String[] args) {
        SpringApplication.run(MixerStationApplication.class, args);
    }
}

写在最后:或者可以试试 先启动server,再启动client

目录
相关文章
SpringCloud - Eureka报错:Connect to localhost:8761 timed out
SpringCloud - Eureka报错:Connect to localhost:8761 timed out
301 0
SpringCloud - Eureka报错:Connect to localhost:8761 timed out
|
4月前
|
Python
【Azure 应用服务】Azure Function HTTP Trigger 遇见奇妙的500 Internal Server Error: Failed to forward request to http://169.254.130.x
【Azure 应用服务】Azure Function HTTP Trigger 遇见奇妙的500 Internal Server Error: Failed to forward request to http://169.254.130.x
|
4月前
|
缓存 NoSQL Java
【Azure Redis 缓存】云服务Worker Role中调用StackExchange.Redis,遇见莫名异常(RedisConnectionException: UnableToConnect on xxx 或 No connection is available to service this operation: xxx)
【Azure Redis 缓存】云服务Worker Role中调用StackExchange.Redis,遇见莫名异常(RedisConnectionException: UnableToConnect on xxx 或 No connection is available to service this operation: xxx)
|
7月前
|
开发框架 .NET 数据库连接
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
2794 0
|
Java 微服务
【Java异常】com.netflix.client.ClientException: Load balancer does not have available server for client
【Java异常】com.netflix.client.ClientException: Load balancer does not have available server for client
515 0
|
7月前
|
Java 微服务
Java错误:微服务报错Cannot execute request on any known serve
Java错误:微服务报错Cannot execute request on any known serve
|
Web App开发 网络协议 数据安全/隐私保护
Win系统 - 如何解决 ERR_PROXY_CONNECTION_FAILED 错误?
Win系统 - 如何解决 ERR_PROXY_CONNECTION_FAILED 错误?
2526 0
Win系统 - 如何解决 ERR_PROXY_CONNECTION_FAILED 错误?
|
微服务
SpringCloud - Cannot execute request on any known server
SpringCloud - Cannot execute request on any known server
212 0
SpringCloud - Cannot execute request on any known server
|
Nacos
解决集群环境nacos频繁报错:找不到节点 send request fail,client not connected。
解决集群环境nacos频繁报错:找不到节点 send request fail,client not connected。
1234 0
解决集群环境nacos频繁报错:找不到节点 send request fail,client not connected。
|
Kubernetes 监控 Java
k8s下的eureak服务注册失败(cannot execute request on any known server)解决
k8s下的eureak服务注册失败(cannot execute request on any known server)解决
704 0