Spring Cloud Config is a distributed configuration management solution, including the server side and the client side.
Table of contents
Introduction to Config
- Server side : Provides storage of configuration files, provides the contents of configuration files in the form of interfaces, and
@EnableConfigServer
embeds them in Spring boot applications by using annotations.
- The naming rules are as follows:
{application}-{profile}.yml
or{application}-{profile}.properties
where
application is the application name and profile refers to the environment (used to distinguish development environment, test environment, production environment, etc.)
- Client : Obtain configuration data through the interface and initialize its own application
Build config-server
- Create a new public warehouse and create a new configuration file on the gitee product
m-service-resume-dev.yml
server:
port: 8080
spring:
application:
name: m-service-resume
#注册发现
eureka:
client:
service-url:
defaultZone: http://CloudEurekaServerA:8761/eureka,http://CloudEurekaServerB:8762/eureka
# 每隔多久拉取⼀次服务列表
registry-fetch-interval-seconds: 30
instance:
#显示ip
prefer-ip-address: true
# 192.168.28.22:m-service-autodeliver:8090:1.0-SNAPSHOT
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
#元数据
metadata-map:
A: A1
B: A1
# 租约续约间隔时间,默认30秒
lease-renewal-interval-in-seconds: 30
# 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发⽣⼼跳,EurekaServer会将服务从列表移除
lease-expiration-duration-in-seconds: 90
my:
test:
num: 100
- Build module
m-cloud-config-server
- Import dependencies
<!--eureka client 客户端依赖引⼊-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--config配置中⼼服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- Startup class
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer //开启Config配置
public class McloudConfigServer {
public static void main(String[] args) {
SpringApplication.run(McloudConfigServer.class,args);
}
}
- Configuration file application.yml
- Add config configuration
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/vipMygz/m-cloud-config-server.git
username: XXXXXX
password: XXXXXX
search-paths:
- m-cloud-config-server
label: master
server:
port: 9006
spring:
application:
name: m-cloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/vipMygz/m-cloud-config-server.git
username: XXXXXX
password: XXXXXX
search-paths:
- m-cloud-config-server
label: master
#注册发现
eureka:
client:
service-url:
defaultZone: http://CloudEurekaServerA:8761/eureka,http://CloudEurekaServerB:8762/eureka
# 每隔多久拉取⼀次服务列表
registry-fetch-interval-seconds: 30
instance:
prefer-ip-address: true
instance-id: ${
spring.cloud.client.ip-address}:${
spring.application.name}:${
server.port}:@project.version@
# 租约续约间隔时间,默认30秒
lease-renewal-interval-in-seconds: 30
# 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发⽣⼼跳,EurekaServer会将服务从列表移除
lease-expiration-duration-in-seconds: 90
-
Start the config service
-
Verify access configuration http://localhost:9006/master/m-service-resume-dev.yml
Client transformation
? ? ? Since it is the configuration center, we register the client service to the Eureka center, and put that part of the configuration in the configuration center. Take a look at the effect.
- Transformation
m-service-resume-8080
- Introduce dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
- Config client configuration
spring:
cloud:
config:
uri: http://localhost:9006/
name: m-service-resume
profile: dev
label: master
- Modify application.yml to bootstrap.yml configuration file
server:
port: 8080
spring:
application:
name: m-service-resume
cloud:
config:
uri: http://localhost:9006/
name: m-service-resume
profile: dev
label: master
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=UTC
username: root
password: root
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
- Start
m-service-resume-8080
and observe the registration center
- Observe the registration center.
The registration was successful, but there was a problem. Some of our configurations did not take effect. No IP is displayed. We found the configuration
and the results obtained by the config configuration service are sorted alphabetically by ABCD.
This is not the focus of the problem. The problem should be that some configurations do not take effect.
Config configuration manual refresh
question
- The client service adds a controller to obtain my.test.num
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${my.test.num}")
private String myTest;
@GetMapping("/viewconfig")
public String viewconfig() {
return "msg==>" + myTest + " num=>" + myTest;
}
}
- Modify the configuration num of gitee to 200 and find that the client is not
没有获取到最新的配置
Configure manual refresh
- Add configuration
management:
endpoints:
web:
exposure:
include: refresh
##### 或者
management:
endpoints:
web:
exposure:
include: "*"
-
Client Add to the class used by the client to configure the information
@RefreshScope
-
Manually initiate a POST request to the Client, http://localhost:8080/actuator/refresh , to refresh the configuration information
-
Restart the service and access the interface
. Note: The manual refresh method avoids service restart (process: Git configuration change -> for loop script manually refreshes each microservice)
Config configuration automatic update
In the microservice architecture, we can combine the message bus (Bus) to realize automatic update of distributed configuration (Spring CloudConfig+Spring Cloud Bus) to achieve a notification that takes effect everywhere.
Spring Cloud Bus (based on MQ, supports RabbitMq/Kafka) is the message bus solution in Spring Cloud. The
combination of Spring Cloud Config + Spring Cloud Bus can realize automatic updating of configuration information.
- Config Server server adds message bus support
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- ConfigServerAdd configuration
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
- Microservice exposed port
management:
endpoints:
web:
exposure:
include: bus-refresh
# 暴露端口
management:
endpoints:
web:
exposure:
include: "*"
- Restart each service, change the configuration, and send a post request to the configuration center server.
http://localhost:9003/actuator/bus-refresh //各个客户端配置即可⾃动刷新
http://localhost:9006/actuator/bus-refresh/m-service-resume:8081 //定向刷新