1. Introduction
In microservice architecture systems, we usually use a lightweight message broker to build a common message topic to connect all microservice instances in the system. Since the messages generated in this topic will be listened to and consumed by all instances, we call it the message bus.
2. Message proxy
Message Broker is an architectural model of message verification, transmission and routing. It serves as a communication scheduling between applications and minimizes dependencies between applications, so that applications can efficiently decouple communication processes. The message broker is a middleware product. Its core is a message routing program used to receive and distribute messages, and forward them to the correct application based on the set message processing flow. It includes independent communication and messaging protocols, enabling network communication within and between organizations. The purpose of designing a proxy is to be able to pass messages from the application and perform some special operations. The following are scenarios in enterprise applications where we often need to use message proxy:
There are already many open source products for everyone to use, such as:
3. SpringCloud+RabbitMQ
(1) RabbitMQ introduction and installation will not be detailed.
(2) pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
(3) application.yml
spring: application: name: rabbitmq-hello rabbitmq: host: ***.***.***.***.*** port: 5672 username: guest password: guest
(4) Sender
@Component public class Sender { private static final Logger log = LoggerFactory.getLogger(Sender.class); @Autowired private AmqpTemplate amqpTemplate; public void send() { String context = "hello " + new Date(); log.info("Sender : " + context); this.amqpTemplate.convertAndSend("hello", context); } }(5) Receiver
@Component @RabbitListener(queues = "hello") public class Receiver { private static final Logger log = LoggerFactory.getLogger(Receiver.class); @RabbitHandler public void process(String hello) { log.info("Receiver : " + hello); } }(6) Create RabbitMQ configuration class RabbitConfig
@Configuration public class RabbitConfig { @Bean public Queue helloQueue(){ return new Queue("hello"); } } (7) Create a unit test class to call message production
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = SpringcloudbusrabbitmqApplication.class) public class HelloApplicationTests { @Autowired private Sender sender; @Test public void hello() throws Exception { sender.send(); } } (8) Test, execute HelloApplicationTests
(9) Visit host:15672
4. Transform Config-Client (integrate springcloud bus)
(1) pom.xml
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
(2) bootstrap.properties
spring.application.name=configspace spring.cloud.config.label=master spring.cloud.config.profile=dev spring.cloud.config.uri= http://localhost:5588/ eureka.client.serviceUrl.defaultZone=http://localhost:5555/eureka/ server.port=5589 spring.rabbitmq.host=118.89.237.88 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest management.security.enabled=false
(3) No need to change the others
V. Test
(1) Test preparation
A service registration center, EUREKASERVER, port 5555;
A distributed configuration center, ConfigServer, with port 5588;
Two distributed configurations, ConfigClient, ports are 5589 and 5590; (2) Visit http://localhost:5589/from
(3) Visit http://localhost:5590/from
RabbitMQ:
(4) Go to the warehouse to modify the password value
from=git-dev-v1.0 by springcloud config-server username=springcloud password=1234567890
(5) POST request http://localhost:5589/bus/refresh or http://localhost:5590/bus/refresh
After a successful request, config-client will reread the configuration file.
(6) Visit again
If the POST requests: http://localhost:5590/bus/refresh, please visit http://localhost:5589/from
In addition, the /bus/refresh interface can specify the service, that is, use the "username" parameter, such as "/bus/refresh?destination=username:**" to refresh all services whose service name is username, regardless of the IP address.
(7) Architecture
(8) Architecture adjustment
Since the /bus/refresh interface of SpringCloud Bus provides parameters for configuration updates for services and instances, our architecture can also make some adjustments accordingly. In the previous architecture, the configuration update of the service required to send a request to an instance in the specific service and then trigger the configuration update of the entire service cluster. Although functions can be implemented, the result is that the application instances we specify will be different from other application instances in the cluster, which will increase the complexity within the cluster and be detrimental to future operation and maintenance work. For example, if the service instance needs to be migrated, we have to modify the configuration in the Web Hook, etc. Therefore, we must make each node in the service cluster peer as much as possible.
Therefore, we have made some adjustments to the previous architecture, as shown in the figure below:
The following changes have been made:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.