编程知识 cdmana.com

(17) Integrating spring cloud cloud Architecture - message driven spring cloud stream

In the use of spring cloud When it comes to Cloud Architecture , We have to use Spring cloud Stream, Because the use of message middleware is everywhere in the project , We do entertainment in the back of our company APP, In the use of spring cloud When doing Architecture , The asynchronous notification of the message , The asynchronous processing of business needs message middleware mechanism .spring cloud Integration advice from the official ( Use rabbit mq and kafka), I took a look at the source code and configuration , Just put rabbit mq Integrate ,kafka It's just another one pom To configure jar It's just a bag , Gossip , Let's go straight to configuration implementation :

  1. brief introduction :

Spring cloud Stream Data flow operation development package , It encapsulates and Redis,Rabbit、Kafka Wait for sending and receiving messages .

  1. Using tools :

rabbit, I will not explain the details of download and installation here , There are too many examples on the Internet

  1. establish commonservice-mq-producer The sender project of the message , stay pom It's equipped with stream-rabbit Dependence
1.  <span style="font-size: 16px;"><!--  introduce MQ Message driven microservice packages , introduce stream Just configure it , It's right rabbit、kafka Good packaging  --> 
2.  <dependency> 
3.   <groupId>org.springframework.cloud</groupId> 
4.   <artifactId>spring-cloud-starter-stream-rabbit</artifactId> 
5.  </dependency></span>
  1. stay yml File configuration rabbit mq
1.  <span style="font-size: 16px;">server: 
2.   port: 5666 
3.  spring: 
4.   application: 
5.   name: commonservice-mq-producer 
6.   profiles: 
7.   active: dev 
8.   cloud: 
9.   config: 
10.   discovery: 
11.   enabled: true 
12.   service-id: commonservice-config-server 
13.   <span style="color: #ff0000;"># rabbitmq and kafka There are default values for the relevant configuration , If modified , You can configure it again  
14.   stream: 
15.   bindings: 
16.   mqScoreOutput: 
17.   destination: honghu_exchange 
18.   contentType: application/json 

20.   rabbitmq: 
21.   host: localhost 
22.   port: 5672 
23.   username: honghu 
24.   password: honghu</span> 
25.  eureka: 
26.   client: 
27.   service-url: 
28.   defaultZone: http://honghu:123456@localhost:8761/eureka 
29.   instance: 
30.   prefer-ip-address: true</span>
  1. Defining interfaces ProducerService
1.  <span style="font-size: 16px;">package com.honghu.cloud.producer; 

3.  import org.springframework.cloud.stream.annotation.Output; 
4.  import org.springframework.messaging.SubscribableChannel; 

6.  public interface ProducerService { 

8.   String SCORE_OUPUT = "mqScoreOutput"; 

10.   @Output(ProducerService.SCORE_OUPUT) 
11.   SubscribableChannel sendMessage(); 
12.  }</span>
  1. Define binding
1.  <span style="font-size: 16px;">package com.honghu.cloud.producer; 

3.  import org.springframework.cloud.stream.annotation.EnableBinding; 

5.  @EnableBinding(ProducerService.class) 
6.  public class SendServerConfig { 

8.  }</span>
  1. Define the sending message service ProducerController
1.  <span style="font-size: 16px;">package com.honghu.cloud.controller; 

4.  import org.springframework.beans.factory.annotation.Autowired; 
5.  import org.springframework.integration.support.MessageBuilder; 
6.  import org.springframework.messaging.Message; 
7.  import org.springframework.web.bind.annotation.PathVariable; 
8.  import org.springframework.web.bind.annotation.RequestBody; 
9.  import org.springframework.web.bind.annotation.RequestMapping; 
10.  import org.springframework.web.bind.annotation.RequestMethod; 
11.  import org.springframework.web.bind.annotation.RestController; 

13.  import com.honghu.cloud.common.code.ResponseCode; 
14.  import com.honghu.cloud.common.code.ResponseVO; 
15.  import com.honghu.cloud.entity.User; 
16.  import com.honghu.cloud.producer.ProducerService; 

18.  import net.sf.json.JSONObject; 

20.  @RestController 
21.  @RequestMapping(value = "producer") 
22.  public class ProducerController { 

24.   @Autowired 
25.   private ProducerService producerService; 

28.   /** 
29.   *  adopt get Mode sending </span> object <span > 
30.   * @param name  Path parameter  
31.   * @return  success | Failure  
32.   */ 
33.   @RequestMapping(value = "/sendObj", method = RequestMethod.GET) 
34.   public ResponseVO sendObj() { 
35.   User user = new User(1, "hello User"); 
36.   <span style="color: #ff0000;">Message<User> msg = MessageBuilder.withPayload(user).build();</span> 
37.   boolean result = producerService.sendMessage().send(msg); 
38.   if(result){ 
39.   return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_SUCCESS, false); 
40.   } 
41.   return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_FAILURE, false); 
42.   } 

45.   /** 
46.   *  adopt get Method to send a string message  
47.   * @param name  Path parameter  
48.   * @return  success | Failure  
49.   */ 
50.   @RequestMapping(value = "/send/{name}", method = RequestMethod.GET) 
51.   public ResponseVO send(@PathVariable(value = "name", required = true) String name) { 
52.   Message msg = MessageBuilder.withPayload(name.getBytes()).build(); 
53.   boolean result = producerService.sendMessage().send(msg); 
54.   if(result){ 
55.   return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_SUCCESS, false); 
56.   } 
57.   return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_FAILURE, false); 
58.   } 

60.   /** 
61.   *  adopt post Mode sending </span>json object <span > 
62.   * @param name  Path parameter  
63.   * @return  success | Failure  
64.   */ 
65.   @RequestMapping(value = "/sendJsonObj", method = RequestMethod.POST) 
66.   public ResponseVO sendJsonObj(@RequestBody JSONObject jsonObj) { 
67.   Message<JSONObject> msg = MessageBuilder.withPayload(jsonObj).build(); 
68.   boolean result = producerService.sendMessage().send(msg); 
69.   if(result){ 
70.   return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_SUCCESS, false); 
71.   } 
72.   return ResponseCode.buildEnumResponseVO(ResponseCode.RESPONSE_CODE_FAILURE, false); 
73.   } 
74.  } 
75.  </span>
  1. establish commonservice-mq-consumer1 Consumer item of message , stay pom It's equipped with stream-rabbit Dependence
1.  <!--  introduce MQ Message driven microservice packages , introduce stream Just configure it , It's right rabbit、kafka Good packaging  --> 
2.  <dependency> 
3.   <groupId>org.springframework.cloud</groupId> 
4.   <artifactId>spring-cloud-starter-stream-rabbit</artifactId> 
5.  </dependency>
  1. stay yml Configuration in file :
1.  server: 
2.   port: 5111 
3.  spring: 
4.   application: 
5.   name: commonservice-mq-consumer1 
6.   profiles: 
7.   active: dev 
8.   cloud: 
9.   config: 
10.   discovery: 
11.   enabled: true 
12.   service-id: commonservice-config-server 

14.   <span style="color: #ff0000;">stream: 
15.   bindings: 
16.   mqScoreInput: 
17.   group: honghu_queue 
18.   destination: honghu_exchange 
19.   contentType: application/json 

21.   rabbitmq: 
22.   host: localhost 
23.   port: 5672 
24.   username: honghu 
25.   password: honghu</span> 
26.  eureka: 
27.   client: 
28.   service-url: 
29.   defaultZone: http://honghu:123456@localhost:8761/eureka 
30.   instance: 
31.   prefer-ip-address: true
  1. Defining interfaces ConsumerService
1.  package com.honghu.cloud.consumer; 

3.  import org.springframework.cloud.stream.annotation.Input; 
4.  import org.springframework.messaging.SubscribableChannel; 

6.  public interface ConsumerService { 

8.   <span style="color: #ff0000;">String SCORE_INPUT = "mqScoreInput"; 

10.   @Input(ConsumerService.SCORE_INPUT) 
11.   SubscribableChannel sendMessage();</span> 

13.  }
  1. Define startup classes and message consumption
1.  package com.honghu.cloud; 

3.  import org.springframework.boot.SpringApplication; 
4.  import org.springframework.boot.autoconfigure.SpringBootApplication; 
5.  import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
6.  import org.springframework.cloud.stream.annotation.EnableBinding; 
7.  import org.springframework.cloud.stream.annotation.StreamListener; 

9.  import com.honghu.cloud.consumer.ConsumerService; 
10.  import com.honghu.cloud.entity.User; 

12.  @EnableEurekaClient 
13.  @SpringBootApplication 
14.  @EnableBinding(ConsumerService.class) // You can bind multiple interfaces  
15.  public class ConsumerApplication { 

17.   public static void main(String[] args) { 
18.   SpringApplication.run(ConsumerApplication.class, args); 
19.   } 

21.   <span style="color: #ff0000;">@StreamListener(ConsumerService.SCORE_INPUT) 
22.   public void onMessage(Object obj) { 
23.   System.out.println(" consumer 1, Messages received :" + obj); 
24.   }</span> 

26.  }
  1. To start, respectively, commonservice-mq-producer、commonservice-mq-consumer1
  2. adopt postman To verify the sending and receiving of messages

 

 

 

 

 

You can see that the message has been received , In the next chapter we introduce mq The cluster scheme of .

Here we are , The integration of the whole message center solution is completed ( 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 .

版权声明
本文为[Black Knight 1203]所创,转载请带上原文链接,感谢

Scroll to Top