• 周日. 10 月 6th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

(16) Integrating spring cloud cloud Architecture – refresh configuration with spring cloud bus

King Wang

1 月 3, 2022

We use spring cloud Distributed microservice Cloud Architecture has done b2b2c The e – commerce system , In addition to the system services provided by the architecture itself , We will b2b2c A fine-grained split of business services for , Different business microservices have been created .

As our business systems become larger and more complex , There will be more and more configurations . The configuration file only needs to be modified , Would be right commonservice-config Configuration center stops service first , Then restart , Finally, make the configuration work .

If there is little service , We can start it manually , But it certainly has an impact on business and system stability .

If it’s hundreds or thousands of services that are manually operated , I think the operation and maintenance personnel or technical personnel will go crazy .

For the above problems ,commonservice-config The server and business microservices are configured respectively , The server is responsible for git(svn Or local file system ) Configure the configuration file stored in ( We are using a local configuration scheme , It is convenient to update the configuration file directly to linux On ),

Business microservices obtain relevant configuration from the server configuration center through configuration , If the configuration file changes , By refreshing business microservices , Get the latest configuration information .

spring cloud Bus Connecting nodes in distributed systems through a lightweight message broker . This can be used to broadcast state changes ( If the configuration changes ) Or other management instructions .

Next , We’re going to implement it through spring cloud Bus programme , Dynamically refresh server configuration , The specific steps are as follows :

1. commonservice-config Please refer to the previous link for service configuration :
http://2147775633.iteye.com/admin/blogs/2396692

2. Business microservice configuration ( With honghu-member-servcie For example, membership services ):

pom File configuration :

1. <span style="font-size: 16px;"> <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId><span style="font-size: 16px;">spring-boot-starter-actuator</span></artifactId>
4. </dependency>
6. <dependency>
7. <groupId>org.springframework.cloud</groupId>
8. <artifactId><span style="font-size: 16px;">spring-cloud-starter-bus-amqp</span></artifactId>
9. </dependency></span>

yml File configuration :

1. <span style="font-size: 16px;">server:
2. port: 5012
3. spring:
4. application:
5. name: honghu-member-client
6. profiles:
7. active: dev,discoveryClient
8. cloud:
9. config:
10. discovery:
11. enabled: true
12. service-id: commonservice-config-server
13. <span style="font-size: 16px;"><strong>name: honghu-member
14. profile: dev
15. bus:
16. trace:
17. enabled: true # Turn on message tracking   </strong>
18. <strong>rabbitmq:
19. host: 192.168.1.254
20. port: 5672
21. username: honghu
22. password: honghu</strong>  </span>
23. eureka:
24. client:
25. serviceUrl:
26. defaultZone: http://honghu:123456@localhost:8761/eureka/
27. instance:
28. prefer-ip-address: true
29. logging:
30. level:
31. root: INFO
32. org.springframework.security: INFO
33. management:
34. security:
35. enabled: false
36. security:
37. basic:
38. enabled: false</span>

Write a test class (MemberController.java), Used to get configuration items

1. <span style="font-size: 16px;">package com.honghu.cloud.controller;
3. import org.springframework.beans.factory.annotation.Value;
4. import org.springframework.cloud.context.config.annotation.RefreshScope;
5. import org.springframework.web.bind.annotation.GetMapping;
6. import org.springframework.web.bind.annotation.RestController;
8. <strong>@RefreshScope</strong>
9. @RestController
10. public class MemberController {
12. @Value("${profile}")
13. private String profile;
15. @GetMapping("/profile")
16. public String getProfile() {
17. return this.profile;
18. }
19. }</span>

3. Check out the Registration Center ,commonservice-config、honghu-member-service Whether the service has been registered successfully

4. Visit profile, obtain profile Corresponding configuration information ( Original configuration ):

visit http://localhost:7071/profile  ==》 Access to the results :123456

5. modify config Configuration center configuration file , take profile=123456 It is amended as follows honghu123456

Revisit http://localhost:7071/profile  ==》 Access to the results :123456

6. Use spring cloud bus Refresh scheme ( Use post man Test tools to test )

http://localhost:7071/bus/refresh

Revisit http://localhost:7071/profile  ==》 Access to the results :honghu123456

Here we are , Whole commonservice-config The dynamic refresh scheme of configuration center is finished ( Enterprise architecture source code can be added ball : Thirty five three six two four seven two fifty nine )!!

Welcome to study with me spring cloud Build micro service Cloud Architecture , I’m going to develop it in the near future spring cloud The process and essence of building microservice cloud architecture are recorded , Help more interested in R & D spring cloud Frame friends , Let’s discuss spring cloud The process of building the framework and how to apply it to enterprise projects .

发表回复