First, about Spring-cloud-stream
Spring Cloud Stream is essentially a combination of spring boot and spring integration, enabling a lightweight message-driven microservices framework. By using spring Cloud Stream, developers can effectively simplify the complexity of the use of message middleware, allowing system developers to focus more on the processing of core business logic.
Here I first put a picture of the official website:
Application injected into it through spring Cloud stream. Input and Output Channel communication with the outside world, the application does not care about the end and which message middleware communication, it mainly through the inputs and outputs to subscribe and send messages, according to this rule if we can easily implement message delivery, subscribe to messages and messages in transit, and when the need to switch message middleware, There is little need to modify the code, just change the configuration.
Where inputs represents the application listener message, the outputs representative sends the message, the binder can be understood as an abstraction to isolate the application from the message middleware, similar to the DAO pattern of DAO's masking service in the case of a database implementation. Of course Springcloud provides the implementation of RABBITMQ and Kafka by default.
Second, Springcloud integrated kafka1, add Gradle dependency:
dependencies{ Compile (' Org.springframework.cloud:spring-cloud-stream ') compile (' Org.springframework.cloud:spring-cloud-stream-binder-kafka ') compile (' Org.springframework.kafka: Spring-kafka ')}
View Code2. Define an interface:
Spring-cloud-stream has defined the most basic input and output interfaces, they are source,sink, Processor
Sink interface:
Package org.springframework.cloud.stream.messaging; Import Org.springframework.cloud.stream.annotation.Input; Import Org.springframework.messaging.SubscribableChannel; Public Interface Sink { = "input"; @Input ("input") Subscribablechannel input ();}
View Code
Source interface:
Package org.springframework.cloud.stream.messaging; Import Org.springframework.cloud.stream.annotation.Output; Import Org.springframework.messaging.MessageChannel; Public Interface Source { = "Output"; @Output ("Output") Messagechannel output ();}
View Code
Processor interface:
Package org.springframework.cloud.stream.messaging; Public Interface extends Source, Sink {}
View Code
Processor This interface defines both the input channel and the output channel. At the same time we can define the channel interface itself, the code is as follows:
PackageCom.bdqn.lyrk.shop.channel;ImportOrg.springframework.cloud.stream.annotation.Input;ImportOrg.springframework.cloud.stream.annotation.Output;ImportOrg.springframework.messaging.MessageChannel;ImportOrg.springframework.messaging.SubscribableChannel; Public InterfaceShopchannel {/*** The name of the channel that sent the message*/String shop_output= "Shop_output"; /*** The subscription channel name of the message*/String shop_input= "Shop_input"; /*** Send a message to the channel * *@return */@Output (shop_output) Messagechannel sendshopmessage (); /*** The channel to receive the message * *@return */@Input (shop_input) Subscribablechannel recieveshopmessage ();}
View Code
3. Define Service class
PackageCom.bdqn.lyrk.shop.server;ImportCom.bdqn.lyrk.shop.channel.ShopChannel;ImportOrg.springframework.cloud.stream.annotation.StreamListener;ImportOrg.springframework.messaging.Message;ImportOrg.springframework.messaging.MessageChannel;ImportOrg.springframework.messaging.support.MessageBuilder;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportJavax.annotation.Resource; @RestController Public classShopservice {@Resource (name=shopchannel.shop_output)PrivateMessagechannel Sendshopmessagechannel; @GetMapping ("/sendmsg") Publicstring Sendshopmessage (string content) {BooleanIssendsuccess =Sendshopmessagechannel. Send (Messagebuilder.withpayload (content). Build ()); returnIssendsuccess? "Send succeeded": "Send Failed"; } @StreamListener (Shopchannel.shop_input) Public voidReceive (message<string>message) {System.out.println (Message.getpayload ()); }}
View Code
Here's the note @StreamListener this annotation can listen to the message content in the input channel, where you specify the input channel name we just defined, and Messagechannel can
The output channel sends a message, and the output channel name we just defined is also specified when using @resource injection
4. Define the Startup class
PackageCom.bdqn.lyrk.shop;ImportCom.bdqn.lyrk.shop.channel.ShopChannel;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.stream.annotation.EnableBinding, @SpringBootApplication @enablebinding (shopchannel .class) Public classshopserverapplication { Public Static voidMain (string[] args) {Springapplication.run (shopserverapplication.class, args); }}
View Code
Note the @enablebinding annotation, which specifies the interface name of the channel that we have just defined, and of course it can pass multiple related interfaces.
5. Define APPLICATION.YML File
Spring: application: name:shop-server cloud: stream: bindings: # Configure which middleware interacts with your own defined channel shop_input: #ShopChannel里Input和Output的值 destination:zhibo #目标主题 shop_output: Destination:zhibo default-Binder:kafka #默认的binder是kafka Kafka: Bootstrap-servers:localhost:9092 #kafka服务地址 Consumer: Group-id:consumer1 producer: Key- Serializer:org.apache.kafka.common.serialization.ByteArraySerializer Value-serializer: Org.apache.kafka.common.serialization.ByteArraySerializer Client-id:producer1server: 8100
View Code
Here is the main play, we must specify the message topic for all the channels defined in the interface, and specify the default binder as Kafka, and then define the Spring-kafka's external configuration, The serialization class for the specified producer here is Bytearrayserializer
After launching the program successfully, we can get the following results when we visit http://localhost:8100/sendMsg?content=2
Springcloud Learning springcloudstream& Integration Kafka