Spring Cloud Config
- summary
- Config server configuration and test
- Config client configuration and testing
- Dynamic refresh of Config client
summary
Configuration problems faced by distributed systems
What is it?
What can I do
-
Centrally manage profiles
-
Different environments have different configurations, dynamic configuration updates, and deployment by environment, such as dev/test/prod/beta/release
-
The configuration is dynamically adjusted during operation. It is no longer necessary to write configuration files on each service deployed machine. The service will agree to pull its own configuration information from the configuration center
-
When the configuration changes, the service can sense the configuration changes and apply new fields without restarting
-
Expose the configuration information in the form of REST interface
Integrated configuration with GitHub
Spring Config uses git to store configuration files by default (there are other ways, such as supporting svn and local files), but Git is recommended and is accessed in the form of http/https
Config server configuration and test
-
Create a new Repository named springcloud config on GitHub
-
Get the newly created git address from the previous step
-
Create a new git warehouse and clone in the local hard disk directory
-
Create a new Module cloud-config-center-3344, that is, the configuration center Module CloudConfig Center of microservices
-
pom
newly added:
<!--config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
-
configuration file
server: port: 3344 spring: application: name: cloud-config-center #Register to eureka microservice name cloud: config: server: git: uri: git@github.com:kisshotlirs/springcloud-config.git #git warehouse name search-paths: - springcloud-config #search for directory label: master #Read branch #Service registration to eureka eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/
-
Main startup class
Add annotation @ enableconfig server
-
Modify the hosts file under windows and add mapping: 127.0.0.1 config-3344 com
-
Test whether the configuration content can be obtained from GitHub through Config microservice
- Start microservice
- visit http://config-3344.com:3344/master/config-dev.yml
-
Configure read rules:
-
/{label}/{application}-{profile}.yml
Instance: Master branch: http://config-3344.com:3344/master/config-dev.yml
Dev branch: http://config-3344.com:3344/dev/config-dev.yml
-
/{application}-{profile}.yml
example: http://config-3344.com:3344/config-dev.yml
-
-
Successfully achieved the use of SpringCloud Config to obtain configuration information through GitHub
Config client configuration and testing
-
Create cloud-config-client-3355
-
pom
<!--config client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
-
bootstrap.yml
The configuration is as follows:
server: port: 3355 spring: application: name: config-client cloud: config: #config client configuration label: main #Branch name name: config #Profile name profile: dev #Read the suffix name and integrate the above three into the config-dev.yml configuration file on the main branch uri: http://localhost:3344 # profile address eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/
-
Main startup class
Register to enter the service center: @ EnableEurekaClient
-
Business class:
@RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @RequestMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
-
test
Start the config configuration center ConfigServer3344 and self test:
- http://config-3344.com:3344/master/config-prod.yml
Start 3355 as a Client to prepare for access:
- http://localhost:3355/configInfo
-
The client 3355 accesses the config configuration center config-3344 and obtains the configuration information through github
-
Problem: dynamic refresh of distributed configuration:
- Linux operation and maintenance modifies the content of the configuration file on Github to make adjustments
- Refresh 3344 and find that the ConfigServer configuration center responds immediately
- Refresh 3355 and find no response from ConfigClient client
- Unless you restart or reload 3355
Dynamic refresh of Config client
Avoid restarting the client micro service 3355 every time the configuration is updated
Dynamic refresh
-
pom introduces actor service monitoring
<!--actuator Service monitoring--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
Modify yml and expose the monitoring port
#Exposure monitoring endpoint management: endpoints: web: exposure: include: "*"
-
Business class Controller modification
Add @ RefreshScope annotation
-
At this time, modify the configuration file on github and test 3344 and 3355
-
At this time, the client 3355 does not refresh dynamically. The operation and maintenance personnel need to send a Post request to refresh the client 3355
Must be a post request: curl -X POST“ http://localhost:3355/actuator/refresh ”
-
Visit the client 3355 again and successfully refresh the client 3355 to the latest configuration content to avoid service restart
Remaining problems:
Suppose there are multiple microservice clients, and each microservice must execute a post request to refresh manually? (script can be used)
Can you broadcast? One notice will take effect everywhere
Can you accurately notify and select a specific micro service to make its configuration effective
The above problems cannot be realized, so the message bus needs to be introduced to cooperate with spring cloud Bus