zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-03-14 22:46:45 时间

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

首先说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

image

对应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