Spring boot actual e-commerce project mall (20k+star) address: https://github.com/macrozheng/mall
abstract
Spring Cloud Config can provide centralized external configuration support for applications in the microservice architecture. It is divided into two parts: the server and the client. This article will introduce its usage in detail.
Introduction to Spring Cloud Config
Spring Cloud Config is divided into two parts: server and client. The server is called the distributed configuration center. It is an independent application, which can obtain configuration information from the configuration warehouse and provide it to the client. The client can obtain the configuration information through the configuration center and load the configuration at startup. The configuration center of Spring Cloud Config uses Git to store configuration information by default, so it naturally supports version management of configuration information, and can use Git client to easily manage and access configuration information.
Prepare configuration information in Git warehouse
Since Spring Cloud Config needs a Git warehouse for storing configuration information, here we add configuration files to Git warehouse and demonstrate its functions. The address of Git warehouse is: https://gitee.com/macrozheng/springcloud-config.
Configure warehouse directory structure
Configuration information under the master branch
- config-dev.yml:
config: info: "config info for dev(master)"
- config-test.yml:
config: info: "config info for test(master)"
- config-prod.yml:
config: info: "config info for prod(master)"
Configuration information under dev branch
- config-dev.yml:
config: info: "config info for dev(dev)"
- config-test.yml:
config: info: "config info for test(dev)"
- config-prod.yml:
config: info: "config info for prod(dev)"
Create config server module
Here we create a config server module to demonstrate the function of Spring Cloud Config as the configuration center.
Add dependency in pom.xml
<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>
Configure in application.yml
server: port: 8901 spring: application: name: config-server cloud: config: server: git: #Configure Git warehouse to store configuration information uri: https://gitee.com/macrozheng/springcloud-config.git username: macro password: 123456 clone-on-start: true #Get configuration directly from git at startup eureka: client: service-url: defaultZone: http://localhost:8001/eureka/
Add @ EnableConfigServer annotation on startup class to enable configuration center function
@EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
Get configuration information through config server
Here we use config server to demonstrate how to get configuration information.
Get access format for profile information
# Get configuration information /{label}/{application}-{profile} # Get profile information /{label}/{application}-{profile}.yml
Placeholder related explanation
- Application: represents the application name, which is spring.application.name in the configuration file by default. If spring.cloud.config.name is configured, it is the name;
- Label: represents the branch name, corresponding to spring.cloud.config.label in the configuration file;
- Profile: represents the environment name, corresponding to spring.cloud.config.profile in the configuration file.
Get configuration information presentation
- Start Eureka server and config server services;
- Visit http://localhost:8901/master/config-dev To obtain the configuration information of the dev environment on the master branch;
- Visit http://localhost:8901/master/config-dev.yml To obtain the configuration file information of the dev environment on the master branch, compare the above information, we can see that the configuration information and the configuration file information are not the same concept;
- Visit http://localhost:8901/master/config-test.yml To get the profile information of the test environment on the master branch:
- Visit http://localhost:8901/dev/config-dev.yml To get the configuration file information of the dev environment on the dev branch:
Create config client module
We create a config client module to get the configuration from config server.
Add dependency in pom.xml
<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>
Configure in bootstrap.yml
server: port: 9001 spring: application: name: config-client cloud: config: #Config client configuration profile: dev #Enable configuration suffix name label: dev #Branch name uri: http://localhost:8901 - configuration center address name: config #Profile name eureka: client: service-url: defaultZone: http://localhost:8001/eureka/
Add ConfigClientController class to get configuration
/** * Created by macro on 2019/9/11. */ @RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
Show me getting configuration from configuration center
- Start the config client service;
- Visit http://localhost:9001/configInfo , you can get the configuration of the dev environment under the dev branch;
config info for dev(dev)
Get configuration under subdirectory
We can not only store the configuration of each item in a different Git warehouse, but also store the configuration of multiple items in a Git warehouse. At this time, we will use the configuration of searching configuration information in subdirectories.
- First of all, we need to add relevant configuration in config server to search the configuration in subdirectory. Here we use application placeholder to indicate that for different applications, we search configuration from subdirectory corresponding to application name, such as configuration in config subdirectory corresponding to config application;
spring: cloud: config: server: git: search-paths: '{application}'
- Visit http://localhost:9001/configInfo After testing, you can find that the configuration information obtained is under the config subdirectory.
config info for config dir dev(dev)
Refresh configuration
After the configuration information in Git warehouse is changed, we can refresh the client configuration information through the refresh endpoint of springboot activator. The following changes need to be made in config client.
- Add the dependency of activator in pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- Turn on the refresh endpoint in bootstrap.yml:
management: endpoints: web: exposure: include: 'refresh'
- Add @ RefreshScope annotation to the ConfigClientController class to refresh the configuration:
/** * Created by macro on 2019/9/11. */ @RestController @RefreshScope public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo() { return configInfo; } }
- After restarting the config client, call the refresh endpoint to refresh the configuration:
- Visit http://localhost:9001/configInfo After testing, you can find that the configuration information has been refreshed.
update config info for config dir dev(dev)
Add security authentication in configuration center
We can add security authentication for the configuration center by integrating spring security.
Create config security server module
- Add dependency in pom.xml:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
- Configure in application.yml:
server: port: 8905 spring: application: name: config-security-server cloud: config: server: git: uri: https://gitee.com/macrozheng/springcloud-config.git username: macro password: 123456 clone-on-start: true #Get configuration directly from git at startup security: #Configure user name and password user: name: macro password: 123456
- Start the config security server service.
Modify the configuration of config client
- Add the bootstrap-security.yml configuration file, mainly to configure the user name and password of the configuration center:
server: port: 9002 spring: application: name: config-client cloud: config: profile: dev #Enable configuration suffix name label: dev #Branch name uri: http://localhost:8905? Configuration center address name: config #Profile name username: macro password: 123456
- Use bootstrap-security.yml to start the config client service;
- Visit http://localhost:9002/configInfo Test and find that configuration information can be obtained.
config info for dev(dev)
Build config sever cluster
In the microservice architecture, all services get configuration from the configuration center. Once the configuration center goes down, there will be serious problems. Next, we build a two node configuration center cluster to solve this problem.
- Start two config servers to run on ports 8902 and 8903 respectively.
- Add the configuration file bootstrap-cluster.yml of config client, which is mainly to add the configuration of getting the configuration center address from the registry and remove the configuration of the configuration center uri:
spring: cloud: config: profile: dev #Enable environment name label: dev #Branch name name: config #Profile name discovery: enabled: true service-id: config-server eureka: client: service-url: defaultZone: http://localhost:8001/eureka/
- Start the config client service with bootstrap-cluster.yml. The information displayed in the registry is as follows:
- Visit http://localhost:9003/configInfo , found that config client can get configuration information.
config info for config dir dev(dev)
Modules used
springcloud-learning ├── eureka-server -- eureka Registry Center ├── config-server -- Configure Central Services ├── config-security-server -- Configuration center service with security certification └── config-client -- Get configured client services
Project source address
https://github.com/macrozheng/springcloud-learning
Public address
mall project In the series of the whole learning course, we pay attention to the first time to obtain the public account.