震惊!【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

目录
相关文章
|
Java Linux Maven
SpringBoot多环境的yml或properties配置,生产环境和开发环境分离(超详细)
SpringBoot多环境的yml或properties配置,生产环境和开发环境分离(超详细)
1241 0
Web server failed to start. Port XXX was already in use.【完美解决方案】
Web server failed to start. Port XXX was already in use.【完美解决方案】
Web server failed to start. Port XXX was already in use.【完美解决方案】
|
7月前
|
安全 Java API
Spring Boot 4 升级实战:从3.x到4.0的分步升级保姆级指南
Spring Boot 4.0于2025年11月发布,基于Spring Framework 7.0,实现模块化(47个轻量自动配置)、JSpecify空安全校验、原生API版本控制等重大升级。镜像减19%、启动快33%,迁移平滑,3.5.x支持至2026年11月。(239字)
6174 1
Web server failed to start. Port XXX was already in use.原因分析-解决方案
Web server failed to start. Port XXX was already in use.原因分析-解决方案
2574 1
Web server failed to start. Port XXX was already in use.原因分析-解决方案
|
Java 微服务
Java错误:微服务报错Cannot execute request on any known serve
Java错误:微服务报错Cannot execute request on any known serve
|
JavaScript API 容器
Vue3气泡卡片(Popover)
这是一个基于Vue的气泡卡片组件(Popover)介绍,提供了在线预览链接及详细API参数说明,包括maxWidth、title、content等,并支持自定义样式。
887 0
Vue3气泡卡片(Popover)
|
SpringCloudAlibaba Nacos 负载均衡
SpringCloud Alibaba - Nacos 作为配置中心 & 读取Properties配置信息
SpringCloud Alibaba是阿里巴巴致力于对微服务的管理、配置、注册等一整套的解决方案。 简介 Nacos 提供用于存储配置和其他元数据的 K-V 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。
|
存储 安全 Java
在CentOS 7上安装和配置Elasticsearch的方法
在CentOS 7上安装和配置Elasticsearch的方法
1446 0