问题描述:
在正常启动Eureka服务端后,想要去连接Eureka客户端,但是客户端在启动时报错。
如图:
was unable to refresh its cache! status = Cannot execute request on any known server
由于Eureka服务端的正常开启,我认为问题应该不是出在springboot和springcloud的依赖版本上。
应该是配置文件出现了问题
原因分析:
在解决问题前,yaml配置文件如下:
1、Eureka服务端
server:
port: 7001
servlet:
context-path: /eureka
#Eureka配置
eureka:
instance:
hostname: localhost # Eureka服务端实例的名字
client:
register-with-eureka: false # 表示是否向Eureka注册中心注册自己
fetch-registry: false #如果为false,则表示自己为注册中心
service-url: # 监控页面地址,也就是到时候需要进入的地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2、Eureka客户端
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.lch.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
#Eureka的配置,服务注册到哪里
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:7001/eureka/
在网上找到的内容都是说Eureka服务端没有配置拒绝向配置中心配置自己和表示自己为注册中心,但是我修改后依然出错。
解决:
修改Eureka客户端yaml配置文件
客户端的访问注册中心的路径出错,要在路径上加上/eureka/eureka/ 第二个/eureka/是表示在注册中心根目录下的测试
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.lch.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
#Eureka的配置,服务注册到哪里
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:7001/eureka/eureka/
测试:
成功