The last article introduced the highly available configuration center. How to update the configuration file without restarting the server? Please see the introduction below
1. Install rabbitMq
1. Download address
http://www.rabbitmq.com/download.html
2. Run
Enter http://localhost:15672 , and the following interface appears, indicating that the installation is successful
3. Cloud integration configuration instructions
Default port: 5672
Default username: guest
Default password: guest
Second, the code implementation
1. Add dependencies
config server: commonservice-config add the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
In config client:bussnessservice-user add
<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.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2. Modify the configuration
Modify the yml files of the two projects and add the following configuration:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
Configuration instructions:
rabbitmq configuration
The bus request url is closed by default, so it needs to be opened to use
Add an attribute server.time=update version1 to the configuration file of the client bussnessservice-user to test the change of the attribute
3. Test
Start the three projects of eureka server, config server and config client in turn (start two ports to test batch update configuration files). When
starting the configuration center, you can see the following logs:
- /actuator/refresh : refresh a single node
- /actuator/bus-refresh: refresh all nodes
Startup complete:
Test with postman:
send Post request http://localhost:8801/getTestValue and http://localhost:8802/getTestValue
Modify the configuration file:
server:
port: 8802
time: update version100
Send post request http://localhost:8801/actuator/bus-refresh
Note: Adding spring-boot-starter-amqp dependencies sometimes results in errors. This is because there are multiple versions in maven, which need to be deleted, and then re-maven - update project
github: https://github.com/tianyana/springcloud
Next, I will update the articles in turn until the whole structure is completed. If you are interested, please follow the author or add me on WeChat and pull you into the spring cloud community group.
WeChat public account: java architect practice
This official account will publish a complete architecture article according to the route of JAVA senior software architect actual combat training. The difficulty is from shallow to deep. It is suitable for those who have a certain development foundation and want to switch to architecture and are doing primary architecture development.