当前位置:   article > 正文

Spring Cloud Stream + RabbitMQ 消息消费组与消息分区_springboot rabbitmq分区

springboot rabbitmq分区

在本 DEMO中有三个节点,stream-hello2 是消息生产端,stream-hello 和 stream-hello3 是消息消费端。

一、stream-hello2 节点(消息生产端)

1. pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.sande</groupId>
  6. <artifactId>stream-hello2</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>stream-hello2</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web-services</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-config-server</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  40. <version>2.0.1.RELEASE</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-amqp</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.cloud</groupId>
  48. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  49. </dependency>
  50. <dependency>
  51. <groupId>com.fasterxml.jackson.core</groupId>
  52. <artifactId>jackson-annotations</artifactId>
  53. <version>2.9.6</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>com.fasterxml.jackson.core</groupId>
  57. <artifactId>jackson-core</artifactId>
  58. <version>2.9.6</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>com.fasterxml.jackson.core</groupId>
  62. <artifactId>jackson-databind</artifactId>
  63. <version>2.9.6</version>
  64. </dependency>
  65. </dependencies>
  66. <dependencyManagement>
  67. <dependencies>
  68. <dependency>
  69. <groupId>org.springframework.cloud</groupId>
  70. <artifactId>spring-cloud-dependencies</artifactId>
  71. <version>${spring-cloud.version}</version>
  72. <type>pom</type>
  73. <scope>import</scope>
  74. </dependency>
  75. </dependencies>
  76. </dependencyManagement>
  77. <build>
  78. <plugins>
  79. <plugin>
  80. <groupId>org.springframework.boot</groupId>
  81. <artifactId>spring-boot-maven-plugin</artifactId>
  82. </plugin>
  83. </plugins>
  84. </build>
  85. </project>

2. src/main/resources/application.properties

  1. spring.cloud.stream.bindings.output.destination=greetings
  2. server.port=8081
  3. spring.rabbitmq.host=localhost
  4. spring.rabbitmq.port=5672
  5. spring.rabbitmq.username=guest
  6. spring.rabbitmq.password=guest

配置了spring.cloud.stream.bindings.output.destination=greetings 后会在RabbitMQ 中创建一个名为 greetings 交换器(exchange)。spring.cloud.stream.bindings.output.destination=greetings 的意思是把 spring cloud stream 的消息输出通道绑定到  RabbitMQ  的 greetings 交换器。

3. 消息生产类

  1. package com.sande.streamhello.ramp;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.cloud.stream.annotation.EnableBinding;
  7. import org.springframework.cloud.stream.annotation.Output;
  8. import org.springframework.cloud.stream.annotation.StreamListener;
  9. import org.springframework.cloud.stream.messaging.Sink;
  10. import org.springframework.cloud.stream.messaging.Source;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.integration.annotation.InboundChannelAdapter;
  13. import org.springframework.messaging.MessageChannel;
  14. import org.springframework.messaging.support.GenericMessage;
  15. import org.springframework.integration.annotation.Poller;
  16. import org.springframework.integration.core.MessageSource;
  17. @EnableBinding(value= {Source.class})
  18. public class SinkSender {
  19. private static Logger logger = LoggerFactory.getLogger(SinkSender.class);
  20. //private String format="yyyy-mm-dd HH:mm:ss";
  21. private Integer i=0;
  22. @Bean
  23. @InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "2000", maxMessagesPerPoll = "1"))
  24. public MessageSource<Integer> timerMessageSource() {
  25. System.out.println("MessageSource");
  26. //return () -> new GenericMessage<>(new SimpleDateFormat(format).format(new Date()));
  27. //return () -> new GenericMessage<>("wo ai ni wo de jia");
  28. //return () -> new GenericMessage<>("{\"name\":\"didi\",\"age\":30}");
  29. return () -> new GenericMessage<>(i++);
  30. }
  31. }

 

4. 应用主类

  1. package com.sande.streamhello;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class StreamHelloApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(StreamHelloApplication.class, args);
  8. }
  9. }

二、stream-hello 节点(消息消费端)

1. pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.sande</groupId>
  6. <artifactId>stream-hello</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>stream-hello</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web-services</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-config-server</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  40. <version>2.0.1.RELEASE</version>
  41. </dependency>
  42. </dependencies>
  43. <dependencyManagement>
  44. <dependencies>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-dependencies</artifactId>
  48. <version>${spring-cloud.version}</version>
  49. <type>pom</type>
  50. <scope>import</scope>
  51. </dependency>
  52. </dependencies>
  53. </dependencyManagement>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

2. src/main/resources/application.properties

  1. spring.cloud.stream.bindings.input.group=Service-A
  2. spring.cloud.stream.bindings.input.destination=greetings
  3. server.port=8080
  4. spring.rabbitmq.host=localhost
  5. spring.rabbitmq.port=5672
  6. spring.rabbitmq.username=guest
  7. spring.rabbitmq.password=guest

配置了 spring.cloud.stream.bindings.input.destination=greetings 后会在RabbitMQ 中创建一个名为 greetings 交换器(exchange)。spring.cloud.stream.bindings.input.destination=greetings 的意思是把 spring cloud stream 的输入通道绑定到  RabbitMQ 的 greetings 交换器。这样节点 stream-hello 的输入通道对应节点 stream-hello2 的输出通道,stream-hello 节点就配置成了 stream-hello2 节点的消费端。spring.cloud.stream.bindings.input.group=Service-A 配置 stream-hello 为消息组 Service-A 中的一个消费端。这两个配置项联合起来解释,就是把节点 stream-hello 的输入通道绑定到 RabbitMQ 的 greetings 交换器,并设置为greetings 交换器中 Service-A  消息消费组中的消费端节点。

3. 消息消费类

  1. package com.sande.streamhello.ramp;
  2. import java.util.Date;
  3. import java.util.stream.Stream;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.cloud.stream.annotation.EnableBinding;
  7. import org.springframework.cloud.stream.annotation.StreamListener;
  8. import org.springframework.cloud.stream.messaging.Sink;
  9. import org.springframework.integration.annotation.ServiceActivator;
  10. import com.sande.streamhello.StreamHelloApplication;
  11. @EnableBinding(value= {Sink.class})
  12. public class SinkReceiver {
  13. private static Logger logger = LoggerFactory.getLogger(StreamHelloApplication.class);
  14. @StreamListener(Sink.INPUT)
  15. public void receive(String payload) {
  16. logger.info("Received:" + payload);
  17. }
  18. }

4、应用主类

  1. package com.sande.streamhello;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class StreamHelloApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(StreamHelloApplication.class, args);
  8. }
  9. }

 

三、stream-hello3 节点(消息消费端)

1.pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.sande</groupId>
  6. <artifactId>stream-hello3</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>stream-hello3</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.4.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web-services</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-config-server</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  40. <version>2.0.1.RELEASE</version>
  41. </dependency>
  42. </dependencies>
  43. <dependencyManagement>
  44. <dependencies>
  45. <dependency>
  46. <groupId>org.springframework.cloud</groupId>
  47. <artifactId>spring-cloud-dependencies</artifactId>
  48. <version>${spring-cloud.version}</version>
  49. <type>pom</type>
  50. <scope>import</scope>
  51. </dependency>
  52. </dependencies>
  53. </dependencyManagement>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

2. src/main/resources/application.properties

  1. spring.cloud.stream.bindings.input.group=Service-A
  2. spring.cloud.stream.bindings.input.destination=greetings
  3. server.port=8082
  4. spring.rabbitmq.host=localhost
  5. spring.rabbitmq.port=5672
  6. spring.rabbitmq.username=guest
  7. spring.rabbitmq.password=guest

配置了 spring.cloud.stream.bindings.input.destination=greetings 后会在RabbitMQ 中创建一个名为 greetings 交换器(exchange)。spring.cloud.stream.bindings.input.destination=greetings 的意思是把 spring cloud stream 的输入通道绑定到  RabbitMQ 的 greetings 交换器。这样节点 stream-hello 的输入通道对应节点 stream-hello2 的输出通道,stream-hello 节点就配置成了 stream-hello2 节点的消费端。spring.cloud.stream.bindings.input.group=Service-A 配置 stream-hello 为消息组 Service-A 中的一个消费端。这两个配置项联合起来解释,就是把节点 stream-hello 的输入通道绑定到 RabbitMQ 的 greetings 交换器,并设置为greetings 交换器中 Service-A  消息消费组中的消费端节点。

3. 消息消费类

  1. package com.sande.streamhello.ramp;
  2. import java.util.stream.Stream;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.cloud.stream.annotation.EnableBinding;
  6. import org.springframework.cloud.stream.annotation.StreamListener;
  7. import org.springframework.cloud.stream.messaging.Sink;
  8. import com.sande.streamhello.StreamHelloApplication;
  9. @EnableBinding(value= {Sink.class})
  10. public class SinkReceiver {
  11. private static Logger logger = LoggerFactory.getLogger(StreamHelloApplication.class);
  12. @StreamListener(Sink.INPUT)
  13. public void receive(String payload) {
  14. logger.info("Received:" + payload);
  15. }
  16. }

4. 应用主类

  1. package com.sande.streamhello;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class StreamHelloApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(StreamHelloApplication.class, args);
  8. }
  9. }

四、测试

1. 启动RabbitMQ

2. 启动节点 stream-hello 和 stream-hello3

3. 启动节点 stream-hello2

3. 我们看到 RabbitMQ 中已经创建了 greetings 交换器

 

4. 我们看到 RabbitMQ 中已经创建了greetings.Service-A 消息队列

5. 消费节点接收到的消息

5.1 stream-hello 节点接收的消息

5.2 stream-hello3 接收到的消息

通过两个消费端输出的消息我们看到,每条消息只会被其中一个消费节点接收。

 

五、配置消息

1. 消息生产端 src/main/resources/application.properties

  1. spring.cloud.stream.bindings.output.destination=greetings
  2. spring.cloud.stream.bindings.output.producer.partition-count=2
  3. spring.cloud.stream.bindings.output.producer.partition-key-expression=1
  4. server.port=8081
  5. spring.rabbitmq.host=localhost
  6. spring.rabbitmq.port=5672
  7. spring.rabbitmq.username=guest
  8. spring.rabbitmq.password=guest

spring.cloud.stream.bindings.output.producer.partition-count=2 指定参与消息分区的消费端节点数量为2个。

spring.cloud.stream.bindings.output.producer.partition-key-expression=1 表示只有分区ID为1的消费端能接收到信息。

spring.cloud.stream.bindings.output.producer.partition-key-expression=0 表示只有分区ID为0的消费端能接收到信息。

2.消费端 stream-hello  src/main/resources/application.properties

  1. spring.cloud.stream.bindings.input.group=Service-A
  2. spring.cloud.stream.bindings.input.destination=greetings
  3. spring.cloud.stream.bindings.input.consumer.partitioned=true
  4. spring.cloud.stream.instance-count=2
  5. spring.cloud.stream.instance-index=0
  6. server.port=8080
  7. spring.rabbitmq.host=localhost
  8. spring.rabbitmq.port=5672
  9. spring.rabbitmq.username=guest
  10. spring.rabbitmq.password=guest

spring.cloud.stream.bindings.input.consumer.partitioned=true 表示启用消息分区功能

spring.cloud.stream.instance-count=2 表示消息分区的消费端节点数量为2个

spring.cloud.stream.instance-index=0 该参数设置消费端实例的索引号,索引号从0开始。这里设置该节点的索引号为0

 

3.消费端 stream-hello3  src/main/resources/application.properties

  1. spring.cloud.stream.bindings.input.consumer.partitioned=true
  2. spring.cloud.stream.instance-count=2
  3. spring.cloud.stream.instance-index=1

4. 测试

4.1 重启 stream-hello 、sream-hello2、stream-hello3

4.2 当消息生产端 设置 spring.cloud.stream.bindings.output.producer.partition-key-expression=1 时,只有实例索引号为1的 stream-hello3 节点可以接收到信息。从截图我们看到 stream-hello3 接收到的消息的数字都是连续的。

4.3 当消息生产端 设置 spring.cloud.stream.bindings.output.producer.partition-key-expression=0 时,只有实例索引号为0的 stream-hello 节点可以接收到信息。

 

查看 RabbitMQ Management 可以看到,其实在 RabbitMQ 中,针对两个消费端分别创建了消息队列。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/840769
推荐阅读
相关标签
  

闽ICP备14008679号