1、高可用
Server端
将上一篇的Server端拷贝两份,Server1和Server2,增加Eureka-client的依赖
(1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>config-server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)application.yml
server:
port: 8099
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/xxx/springcloud-config # git仓库的地址
# search-paths: # 仓库文件夹名称,多个以逗号分隔
username: #Git仓库用户名
password: #Git仓库密码
default-label: main
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
(3)启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServer1Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServer1Application.class, args);
}
}
Client端
将上一篇的Client端Copy过来,和Server端一样,增加Eureka-client的依赖
(1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>config-client1</artifactId>
<version>2.1.6.RELEASE</version>
<name>config-client1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)bootstrap.properties
这里我们需要先清空application.yml,所有的配置全都转移到bootstrap.properties中。
spring.application.name=spring-cloud-config-client
server.port=8091
spring.cloud.config.name=springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.label=main
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置:
- spring.cloud.config.discovery.enabled :开启Config服务发现支持
- spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
- eureka.client.serviceUrl.defaultZone :指向注册中心的地址
(3)启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClient1Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient1Application.class, args);
}
}
顺次启动Eureka,Server1,Server2,Client。访问http://localhost:8761/,可以看到两个config-serve都正常注册到注册中心,当某一台down掉之后另一台还能继续提供服务。
2、refresh
我们的客户端并不能主动去感知Git或者Svn的配置变化,从而主动获取最新的配置。那么,客户端如何去主动获取新的配置信息呢?springcloud已经给我们提供了解决方案,每个客户端通过POST方法触发各自的/refresh。
修改config-client项目已到达可以refresh的功能。
(1)添加依赖pom.xml
在我们原有的config-client项目的pom.xml的基础增加新的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。
(2)开启更新机制
需要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值。
@RestController
@RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。
public class HelloController {
@Value("${springcloud.hello}")
private String hello;
@RequestMapping("/hello")
public String from() {
return this.hello;
}
}
(3)配置文件bootstrap.properties
spring.application.name=spring-cloud-config-client
server.port=8091
spring.cloud.config.name=springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.label=main
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
management.security.enabled=false
management.endpoints.web.exposure.include=*
- management.security.enabled: springboot 1.5.X 以上默认开通了安全认证,所以需要添加这个配置
- management.endpoints.web.exposure.include: springboot 2.x 默认只开启了info、health的访问,*代表开启所有访问
如果修改了配置文件内容,通过cmd命令行执行:curl -X POST http://localhost:8091/actuator/refresh 就能获取到最新配置了
每次手动刷新客户端还是很麻烦,有没有什么办法只要提交代码就自动调用客户端来更新呢,可以用github的webhook