spring boot 2.4.3 下的spring cloud 2020.0.1 之 注册中心Eureka
version
- spring-boot-starter-parent 2.4.3
- spring-cloud-dependencies 2020.0.1
spring boot 和spring cloud 版本关系依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
<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>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--项目启动会给出黄色警告,建议使用caffeine-->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>${caffeine.version}</version>
</dependency>
打包的build
<build>
<plugins>
<!--spring boot 项目打包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类 和旧的无差别
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件
spring boot 2.4.x 版本的配置文件的加载机制发生了变化,此处不进行详述.自行百度.
- 配置文件需要是 application.properties 或者是 application.yml文件
- 注意: 是application. bootstrap.[properties | yml] 文件无法加载
- 可能需要引入XXXXX依赖
- 注意: 是application. bootstrap.[properties | yml] 文件无法加载
- application.yml
spring:
application:
name: eureka-server
profiles:
active: dev
config:
import: # 目前使用 spring.profiles.include 进行配置文件包含,也是可以的,建议使用 import,毕竟是新的嘛.
- classpath:application-actuator.yml
- classpath:application-eureka.yml
- application-actuator.yml
management:
endpoint:
shutdown:
enabled: true
endpoints:
web:
exposure:
include: "*"
- application-eureka.yml
eureka:
instance:
prefer-ip-address: true # 将自己的ip注册到eureka server
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 20
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
register-with-eureka: true # 将自己注册到注册中心
fetch-registry: true # 从注册中心获取注册表信息
server:
enable-self-preservation: true # 禁用自我保护模式
eviction-interval-timer-in-ms: 5000
---
spring:
config:
activate:
on-profile: dev # 旧的spring.profiles 指定环境的方式已被忽略
server:
port: 9091
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:9091/eureka/
---
spring:
config:
activate:
on-profile: peer1
eureka:
client:
service-url:
defaultZone: http://eureka.cn:9092/eureka/
server:
port: 9091
---
spring:
config:
activate:
on-profile: peer2
server:
port: 9092
eureka:
client:
service-url:
defaultZone: http://eureka.cn:9091/eureka/
...
启动命令
nohub 等后台运行自行填充
# 上述配置 默认激活的dev环境.访问[http://127.0.0.1:9091](http://127.0.0.1:9091)
java -jar youProjectJarName.jar --spring.profile.active=dev
# 启用注册中心的高可用启动命令
java -jar youProjectJarName.jar --spring.profile.active=peer1
java -jar youProjectJarName.jar --spring.profile.active=peer2
展示一下 有图有真相

如有问题请发送 代码 问题描述 至17610759700@163.com