一定要以来版本相对应
我在这个问题上花了一定的时间,官方文档中有相对应的版本说明
https://spring.io/projects/spring-cloud
我的在项目中的使用的SpringBoot版本是2.3.4
parent的POM依赖如下
<properties>
<spring.cloud-version>Hoxton.SR6</spring.cloud-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
服务端
<!--eureka服务端的配置文件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在启动类加上注解@EnableEurekaServer
application.yml配置:
server:
port: 7878
eureka:
client:
registerWithEureka: false #是否将自己注册到Eureka中,本身就是无需注册
fetch-registry: false #是否从Eureka中获取注册信息
serviceUrl:
defaultZone: http://127.0.0.1:${server.port}/eureka/ #Eureka客户端与服务端进行交互的地址
客户端
<!--eureka客户端的配置文件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
启动类加注解@EnableEurekaClient
application.yml配置:
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7878/eureka/
instance:
prefer-ip-address: true #是为了在线上时模块之间能够跨域访问
项目运行结果
上面的红色警告可以无视,出现的原因是:
Eureka进入了保护模式,Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,
如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告,保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。
一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。
之后就是用Feign实现服务间的调用
在之后的文章中会写道,这里就不写了