Detailed explanation of spring cloud application implementation configuration automatic refresh process
This article mainly introduces the detailed explanation of the automatic refresh process of spring cloud application configuration. It is introduced in great detail through the example code, which has certain reference value for everyone's study or work. Friends in need can refer to it
Through the message bus of spring cloud, notify all components of spring cloud of changes in source code warehouses such as GitHub configuration.
Spring bus needs rabbitmq, so you need to prepare rabbitmq message queue environment in advance
Configuration center adjustment
1. Configure reference POM in configuration center
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency>
2. Configuration center configuration
spring: application: name: spring-config cloud: config: server: git: uri: https://github.com/halouprogramer/spring-config-repository.git # username: *** # password: *** basedir: ~/temp/gitlab rabbitmq: #增加rabbitmq的配置 host: 192.168.114.129 port: 5672 username: admin password: admin eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true server: port: 3636 management: endpoints: web: exposure: include: "*"
Modifications to be made in business microservices
1. Add POM
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
2. Configuration file
spring: application: name: spring-school cloud: config: discovery: enabled: true service-id: SPRING-CONfig profile: dev bus: #bus id 不能使用默认,否则不能刷新 id: ${spring.application.name}:${spring.cloud.config.profile}:${random.value} profiles: active: dev rabbitmq: host: 192.168.114.129 port: 5672 username: admin password: admin eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true #配置超时时间 feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 # logger-level: bus
Spring cloud bus will use the bus ID to match the application, and the configuration will be refreshed on the matching
3. Write test code
package com.lvlvstart.spring.demo.school.service; import com.lvlvstart.spring.demo.school.dao.SchoolRepository; import com.lvlvstart.spring.demo.school.entity.School; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; /** * @author zishu.lv@baodanyun-inc.com * @description 类描述 * @create 2019/12/9 15:53 */ @Service @RefreshScope public class SchoolService { @Value("${env}") private String env; @Autowired private SchoolRepository repository; public List<School> findAll(){ return repository.findAll(); } public School findById(String choolId){ Optional<School> school = repository.findById(choolId); if(school.isPresent()){ return school.get(); } return null; } public String getEnv(){ return env; } }
package com.lvlvstart.spring.demo.school.web; import com.lvlvstart.spring.demo.school.service.SchoolService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("config-demo") public class ConfigDemoController { @Autowired private SchoolService schoolService; @GetMapping("get-env") private String getEnv(){ return schoolService.getEnv(); } }
@The configuration will only be refreshed on the refreshscope tag
@Refreshscope is used in the controller layer and cannot get a value
Automatic refresh configuration with githbu webhook:
The payload URL needs to add a monitor opened by config server (the monitor is the spring's own address). If it is on the intranet, you can search the intranet penetration tool configuration. After modifying the warehouse configuration, access the address: http://localhost:8081/config -The demo / get env address will also change
Full code access: https://github.com/halouprogramer/spring-cloud-demo
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.