Getting started with Config and using it
1. Introduction and Introduction to Config
SpringCloud Config provides centralized external configuration support for microservices in the microservice architecture, and Configuration Server provides a centralized external configuration for all environments of various microservice applications.
Core: SpringCloud Config is divided into two parts: server and client.
The server, also known as the Distributed Configuration Center, is a stand-alone microservice application that connects to the configuration server and provides clients with access interfaces to obtain configuration information, encrypt/decrypt information, and so on.
Clients manage application resources and business-related configuration content through a designated configuration center, and obtain and load configuration information from the configuration center at startup Configuration Server uses git by default to store configuration information, which helps with versioning of environmental configuration and facilitates the management and access of configuration content through git client tools.
Config action
- Centrally Manage Profiles
- Centrally Manage Profiles
- Different environments different configurations, dynamic configuration updates, sub-environment deployments such as dev/test/prod/beta/release
- Configuration is dynamically adjusted during runtime, and configuration files no longer need to be written on the machines where each service is deployed. Services pull configuration information from the configuration center system
- When a configuration changes, the service does not need to restart to be aware of the configuration changes and apply the new configuration
- Exposing configuration information as a REST interface
Config Server Configuration
Config server is used to connect to remote warehouse
1. Rewrite the pom file
<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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2. Write Yml file
server: port: 3344 spring: application: name: cloud-config-center #The name of the microservice registered with the Eureka server cloud: config: server: git: uri: https://github.com/xy19980319/springcloud-config #git repository name on GitHub ####search for directory search-paths: - springcloud-config ####Read Branch label: master #Service registered to eureka address eureka: client: service-url: defaultZone: http://localhost:7001/eureka
3. Write the main method
@SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args){ SpringApplication.run(ConfigCenterMain3344.class,args); } }
4. Modify system configuration
The hosts file in the C:\Windows\System32\drivers\etc path will map the name of the cluster to 127.0.0.1
127.0.0.1 config-3344.com
5. Configuration Read
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
These methods of reading are all possible, and the teacher recommended the third **/{label}/{application}-{profile}.yml**
There are pits in reading here, and the web page directly maps the file name on github, as detailed in Blog
Config Client Configuration
1. Rewrite Pom
<dependencies> <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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2. Write yml files
Write here that uses system priority firstBootstrap.ymlAnd then load theApplication.yml
server: port: 3355 spring: application: name: config-client cloud: #Config Client Configuration config: label: master #Branch name name: config #Profile Name profile: dev #Read suffix names Three of the above synthesizations: config-on master branchDev.ymlConfiguration file readHttp://config-3344.com: 3344/master/config-dev.yml uri: http://localhost:3344 #Configuration Center Address k #Service registered to eureka address eureka: client: service-url: defaultZone: http://localhost:7001/eureka
3. Write the main method
@SpringBootApplication @EnableEurekaClient public class ConfigClientMain3355 { public static void main(String[] args){ SpringApplication.run(ConfigClientMain3355.class,args); } }
4. Write rest style controller
@RestController public class ConfigController { @Value("${config.info}") private String ConfigInfo; @GetMapping("/configInfo") public String getConfigInfo() { return ConfigInfo; } }
5.github modifies yml
After github modified the yml configuration, if you use port 3344 to log in, that is, to log in directly and interactively, you can get the modified information, but 3355 is not the Config client, how can you make the modified configuration file update directly on the server side too? The method is to modify 3355 client to improve the client configuration
1. Modification of POM file
Join actuator dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. Modify the yml file
Exposure Monitoring Endpoint
#Exposure Monitoring Endpoint management: endpoints: web: exposure: exclude: "*"
3. Add @RefreshScope to the client controller
Implement refresh
4. Notification refresh
Send a post request telling the client to update the interface to refresh
curl -X POST "http://localhost:3355/actuator/refresh"
4. Summary
1. Learning Summary
The SpringCloud Config Configuration Server provides a centralized external configuration for all environments for various microservice applications.
The service center is connected to the outside, and the client that gets the configuration needs to be connected to the service center to get the external configuration.
2. Notes
Configuring read while accessing external configurations requires attention to name settings
3. Confusion
Is there a framework that integrates post requests so that multiple servers receive messages to update at once?