Spring Cloud, this family bucket framework throughout the small and medium sized Internet companies unusual hot, then the corresponding Spring Cloud Stream will gradually be everyone pay attention to it, the main lesson to describe how Spring Cloud Stream to integrate with RabbitMQ.
Architecture Introduction
Destination Binder: Application Application contains its own is left RabbitMQ, the right is Kafka. Production and represents message transmission, the message may be different middleware. This is the Spring Cloud Stream the top of an abstract. Very good place.
Two important areas: inputs (input) message receiving terminal, Outputs (output) end of a message sent Spring Cloud Stream applied to core messaging middleware, applications Spring Cloud Stream injected through input / output channel to communicate with the outside channels . Binder achieved through specific channels to communicate with an external messaging middleware.
Yellow : represents RabbitMQ Green : input and output plug, a layer of the message are set plug, the plug can be used for a variety of different message can also be used to replace the message middleware.
Core concepts:Barista接口 : Barista interfaces is defined as a parameter back class interface definition for this type of channel and the channel name, the channel name is used as a configuration, the channel type determines the channel app will use this message is transmitted from or received messages.
How to define the channel interface: @Output: output annotation that defines the interface to send a message @Input: annotation input, for defining a message consumer interfaces @StreamListener: for annotation defines listener methods
Use Spring Cloud Stream is very simple, just use good these three notes can be very appropriate in the scene to achieve the production and consumption of high-performance message, but using SpringCloudStream framework has a very big problem that they can not achieve reliability of delivery 100% reliability, which is not guaranteed message, there will be a small amount of message loss problems. Currently SpringCloudStream integration of RabbitMQ and Kafka, we all know that Kafka was unable to deliver the message reliability, the reason is because SpringCloudStream framework for both Kafka and so the purpose of using it in practical work is aimed at high-performance message communication! This point is located at the current version of SpringCloudStream. Therefore, in the actual work, may be employed SpringCloudStream, if necessary to ensure the reliability of delivery, may be used alone RabbitMQ, it is also possible.
After starting the project, view the control station is generated and Exchange Queue
After starting the project, SpringCloudStream generated Exchange and Queue.
Production end test code running 3.2
@RunWith(SpringRunner.class)
@SpringBootTestpublicclassApplicationTests{
@Autowiredprivate RabbitmqSender rabbitmqSender;
@TestpublicvoidsendMessageTest1()throws InterruptedException {
for(int i = 0; i < 1; i ++){
try {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("SERIAL_NUMBER", "12345");
properties.put("BANK_NUMBER", "abc");
properties.put("PLAT_SEND_TIME", DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
rabbitmqSender.sendMessage("Hello, I am amqp sender num :" + i, properties);
} catch (Exception e) {
System.out.println("--------error-------");
e.printStackTrace();
}
}
TimeUnit.SECONDS.sleep(2000);
}
}
复制代码
Running the test method: sendMessageTest1 (), see the printed results.
End print production results:
Consumer-side printing results:
summary
By these chapters, we learned knowledge of Spring AMQP through learning, we inherit Spring RabbitMQ team has an in-depth understanding, so for our follow-up study, the use of work have laid a solid foundation, and finally we integrate SpringBoot and SpringCloudStream, more convenient and more efficient integrated into our apps go! Three chunks of learning, learning the RabbitMQ, statement messages, message templates, SimpleMessageListenerContainer, MessageListenerAdapter, MessageConverter. Support String converter, Json converter, convert Java objects, multiple objects conversion, flow conversion (pictures, documents). Integration with SpringBoot very, very simplified, only a few can be configured to send and receive messages. Add a layer of a multi-agency input and output on SpringCloudStream nature. To replace the MQ later, the channel level code can not be modified. The production side may be RabbitMQ, the consumer side is Kafka.
The end of the sentence
Welcome attention to personal micro-channel public number: Coder program for the latest original technical articles and free learning materials, a lot more boutique mind maps, interview data, PMP preparation materials waiting for you to lead, allowing you to learn technical knowledge anytime, anywhere! Create a qq group: 315 211 365, we welcome into the group exchange study together. Thank you! Can also be introduced to the side of a friend in need.