Service configuration Config
Problem: microservice means to split the business in a single application into one sub service. The granularity of each service is relatively small, so there will be a large number of services in the system. Because each service needs the necessary configuration information to run, a centralized and dynamic configuration management facility is essential
Spring cloud provides ConfigServer to solve this problem. A micro service carries an application.yml and manages hundreds of configuration files
SpringCloud Config provides centralized external configuration support for microservices in the microservice Architecture Center. The configuration server provides a centralized external configuration for each environment of different microservice applications
Integrate configuration with GitHub, use Git to store configuration files, and use http/https access mode
Config server configuration and test
Create a new Repository named springcloud config on GitHub (it needs to be a public Repository, and private repositories cannot be accessed)
- Create a new module cloud-config-center-3344
- pom
<dependencies> <!--config server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--eureka client(Dynamic routing through microservice name)--> <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> <!--Hot deployment--> <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>
- yml
server: port: 3344 spring: application: name: cloud-config-center #Microservice name registered into Eureka server cloud: config: server: git: uri: https://Warehouse address of gitee.com/pikaqiu/springcloud-config.git #git search-paths: #search for directory - springcloud-config label: master #Read branch eureka: client: service-url: defaultZone: http://Localhost: the eureka address to which the 7001 / eureka # service is registered
- Main startup class
@EnableEurekaClient @SpringBootApplication public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run(ConfigClientMain3355.class, args); } }
- Modify the hosts file and add mapping
127.0.0.1 config-3344.com
- Add some text data to the three files in GitHub
Start 70013344 and enter in the browser http://config-3344.com:3344/master/config-dev.yml (successfully obtained the configuration file data on github)
Configured read rules
Dynamic refresh of Config client
- pom file
Modify the first one as above
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- YML file bootstrap.yml
server: port: 3355 spring: application: name: config-client cloud: config: #config client configuration label: master #Branch name name: config #The configuration file name is a combination of these three: the configuration file of config-dev.yml on the master branch profile: dev #Read suffix name is read to http://config-3344.com:3344/master/config/dev uri: http://localhost:3344 # configuration center address eureka: client: service-url: defaultZone: http://Localhost: the eureka address to which the 7001 / eureka # service is registered
-
Main startup class
@EnableEurekaClient @SpringBootApplication public class ConfigClientMain3355 { public static void main(String[] args) { SpringApplication.run(ConfigClientMain3355.class, args); } }
-
- controller (read GitHub's configuration file)
@RestController public class ConfigClientController { @Value("${config.info}") //spring @ Value annotation private String configInfo; @GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } }
- Testing http://localhost:3355/configInfo
Dynamic refresh problem
- Modify the version number of config-dev.yml file on GitHub to 2.
- Refresh http://config-3344.com:3344/master/config-dev.yml, the version number has changed.
- Refresh http://localhost:3355/configInfo , no change.
- Restart the 3355 service and refresh again to update the data
Config client dynamic refresh
- Add to config client 3355 in pom
<!--Monitoring in addition to gateway--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- Then add in bootstrap.yml
#Exposure monitoring endpoint management: endpoints: web: exposure: include: "*"
-
Add the @ RefreshScope annotation to the ConfigClientController class.
-
Restart 3355.
-
Modify the version number of the file on GitHub, and then visit 3344 and 3355. (modify after startup)
http://config-3344.com:3344/master/config-dev.yml -
http://localhost:3355/configInfo (not read, you need to send a post request to refresh 3355 to take effect)
-
Open the terminal and enter curl - x post“ http://localhost:3355/actuator/refresh "
-
Refresh http://localhost:3355/configInfo Post success
If there are multiple micro service clients, each micro service needs to execute a post request. The distributed automatic refresh configuration requires a Bus