Introduction to SpringCloud
Spring Cloud is one of Spring's projects.
Spring Cloud is not a component but a collection of many components.
It integrates some of the very popular technologies together to achieve multiple important functions in distributed development and
coordinate each of the distributed environments System and provide template configuration for various services
The main components involved include:
- Eureka : Registration Center
- Zuul or Spring Cloud Gateway : service gateway
- Ribbon : load balancing
- Feign : service call
- Hystrix or Resilience4j: Fuse
In addition, there are some useful components such as Spring Cloud Config and Spring Cloud Bus
[In this article, the Spring Cloud Bus message bus will be introduced]
Spring Cloud Bus message bus
If you modify the configuration of Spring Cloud Config without restarting the service, you will not be able to read the latest modified configuration items.
Spring Cloud Bus can synchronize and update the configuration items in time without restarting the system.
Spring Cloud Bus uses a lightweight message agent to connect distributed nodes and can be used to broadcast configuration file changes or service monitoring management,
that is, the message bus can monitor microservices and can also realize mutual communication between applications
The optional message brokers of Spring Cloud Bus are RabbitMQ and Kafka
Steps for usage
The bottom layer of Spring Cloud Bus is based on RabbitMQ and uses the local message queue service by default. Therefore, you need to install RabbitMQ in advance and then start the local RabbitMQ service.
For the installation tutorial, please refer to my other blog: Hands-on demonstration of installing RabbitMQ in a Windows environment
1. First configure in [Configuration Center Project]:
Add related dependencies in the pom file of the configuration center project:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
Then make the relevant configuration:
spring:
application:
name: config-server
cloud:
config:
server:
# 指定git仓库的地址
git:
uri: https://gitee.com/zhangsan/spring-cloud-config.git
username: zhangsan
password: 123456789
# RabbitMQ配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
# 暴露触发消息总线的地址
# 访问该地址即可立即刷新配置文件
include: bus-refresh
2. [Service Provision Project] also needs to be configured accordingly
(The service provision project is the project that hosts the configuration file to git)
Also need to add related dependencies:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
The configuration file should also be modified:
spring:
cloud:
config:
name: user
profile: dev
label: master
discovery:
enabled: true
service-id: config-server
# RabbitMQ配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
Add @RefreshScope
annotations to the controller class to refresh the configuration when there is the latest configuration item:
@RestController
@RequestMapping("/user")
// 当有最新的配置项时 刷新配置
@RefreshScope
public class UserController {
@Autowired
private UserService userService;
@Value("${test.name}")
private String name;
@GetMapping("/{id}")
public User queryById(@PathVariable Long id)
{
System.out.println("name : "+name);
return userService.queryById(id);
}
}
Test:
After modifying the configuration file on git, send a Post request to localhost:12000/actuator/bus-refresh
(port 12000 is the port of my configuration center project, bus-refresh is the set path name and can be modified in the configuration file )
At this time, the latest data in the configuration file is displayed in the project
testing successfully