The book follows the above and continues to learn SpringCloud Config from Mr. Zhou Yang. It is hereby sorted out as follows
1, Config configuration center
In the distributed system, due to the huge number of services, in order to facilitate the unified management and real-time update of service configuration files, distributed configuration center components are required. The Spring Cloud Config project is such a configuration management solution for distributed systems. It includes two parts: client and server. The server provides the storage of configuration files and provides the contents of configuration files in the form of interface. The client obtains data through the interface and initializes its own application according to this data.
2, Config attribute
Spring Cloud Config puts the configuration of the application originally placed in the local file into the github, so as to provide better management and publishing capabilities.
Spring Boot Config provides configuration management based on application, environment and Version (other configuration centers lack).
2.1 advantages of spring cloud config
Based on the three dimensions of application, environment and version management, each code submission has a version number, and you can jump to the specified version
The configuration storage supports Git, and the back-end is based on Git storage. It has good version support and supports other storage, such as local files, SVN, etc
It integrates seamlessly with Spring, supports the Environment and PropertySource interfaces in Spring, and has low migration cost
2.2Spring Cloud Config disadvantages
Weak dynamic configuration capability, redeployment is required to adjust the configuration, and more code is added
Weak governance ability and weak safety audit ability
Not strictly enterprise level, applicable to small projects
3, Project practice
3.1 create a public project springcloud config on GitHub or Gitee
This article creates springcloud config on Gitee
3.2 create a new configuration center module cloud config center3344
The local hosts file is configured as follows
application.yml
server: port: 3344 spring: application: name: cloud-config-center cloud: config: server: git: uri: https://gitee.com/123/springcloud-config search-paths: - springcoud-config #gitee has its own warehouse address on it label: master #15672 is the management interface port and 5672 is the MQ access port rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest #The service is registered to the eurake address eureka: client: service-url: defaultZone: http://localhost:7001/eureka #rabbitmq related configuration, exposing the endpoint of bus refresh configuration management: endpoints: #Exposed bus refresh configured endpoint web: exposure: include: "bus-refresh" #By accessing curl - x post“ http://localhost:3344/actuator/bus -Refresh "realize 3344 broadcast notifications 3355 and 3366
ConfigCenterMain3344
@SpringBootApplication @EnableConfigServer public class ConfigCenterMain3344 { public static void main(String[] args) { //springcloud config provides centralized resource configuration support for microservices, and configures servers for all environments of different microservice applications //Provide a centralized external configuration //Here you have configured the mapping of windows hosts //Start 7001 and 3344, access http://config-3344.com:3344/master/config-dev.yml SpringApplication.run(ConfigCenterMain3344.class, args); //By accessing curl - x post“ http://localhost:3344/actuator/bus -Refresh "realize 3344 broadcast notifications 3355 and 3366 //Bus refresh is the bus exposure point configured in bootstrap.yml //Post via curl - x“ http://localhost:3344/actuator/bus -Refresh / config client: 3355 realize fixed-point notification 3355 } }
Start 7001 and 3344, access http://config-3344.com:3344/master/config-dev.yml, and the result is consistent with that of Gitee.
3.3 create cloud-config-client-3355
After we update the configuration on github, our service must be restarted to use the new configuration, otherwise the configuration will not take effect.
The solution is to modify the configuration dynamically. Use the tag: @ RefreshScope
bootstrap.yml
server: port: 3355 #bootstrap.yml belongs to the system level and has higher priority than application spring: application: name: config-client cloud: #config client configuration config: label: master #Branch name name: config #Profile name profile: dev #Read the suffix name and the above three combinations: config-dev.yml configuration file on the master branch #Read http://config-3344.com:3344/master/config-dev.yml uri: http://localhost:3344 # configuration center address #15672 is the management interface port and 5672 is the MQ access port rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest eureka: client: service-url: defaultZone: http://localhost:7001/eureka # Expose the monitoring endpoint, and the operation and maintenance personnel modify the configuration file, such as the configuration information on gitee and frequent restart of client 3355 management: endpoints: web: exposure: include: "*"
ConfigClientMain3355
@EnableEurekaClient @SpringBootApplication public class ConfigClientMain3355 { /**test * Launch Euraka registry 7001 * Start Config configuration center 3344 to access http://config-3344.com:3344/master/config-dev.yml * Start the configuration center 3355 as a Client to prepare for access http://localhost:3355/configInfo * * Get configuration information through restful style * Access eurake address http://eureka7001.com:7001/ * The client 3355 accesses 3344 to obtain configuration information through gitee * * Conclusion: findings http://localhost:3355/configInfo and http://config-3344.com:3344/master/config-dev.yml results are consistent * */ public static void main(String[] args) { SpringApplication.run(ConfigClientMain3355.class,args); } }
ConfigClientController
@RestController @Slf4j //Manual refresh function @RefreshScope //This annotation RefreshScope solves the problem of frequent restart of configuration files such as configuration information on gitee and client 3355 after each operation and maintenance modification public class ConfigClientController { //Note: config.info here is https://gitee.com/123/springcloud-config Info content in config-dev.yml of @Value("${config.info}") private String configInfo; @GetMapping("/configInfo") public String getConfigInfo(){ return configInfo; } //After modifying the gitee configuration information, the operation and maintenance personnel manually send a post request (it must be post), so that the 3355 client does not need to restart to obtain the latest configuration //curl -X POST "http://lcoalhost:3355/actuator/refresh" }
Start Euraka registration center 7001, start Config configuration centers 3344 and 3355,
visit http://config-3344.com:3344/master/config-dev.yml
Start the configuration center 3355 as a Client to prepare for access,
visit http://localhost:3355/configInfo
Get configuration information through restful style
Access eurake address http://eureka7001.com:7001/
The client 3355 accesses 3344 to obtain configuration information through gitee
Conclusion: findings http://localhost:3355/configInfo and http://config-3344.com:3344/master/config-dev.yml results are consistent
After modifying the gitee configuration information, the operation and maintenance personnel manually send a post request (it must be post), so that the 3355 client does not need to restart to obtain the latest configuration
curl -X POST "http://lcoalhost:3355/actuator/refresh"