当前位置:   article > 正文

Spring Cloud Stream完成简答应答 rabbitmq_spring cloud stream rabbitmq 确认应答

spring cloud stream rabbitmq 确认应答
  1.    简介

     Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架。它可以基于 Spring Boot 来创建独立的、可用于生产的 Spring 应用程序。Spring Cloud Stream 为一些供应商的消息中间件产品提供了个性化的自动化配置实现,并引入了发布-订阅、消费组、分区这三个核心概念。通过使用 Spring Cloud Stream,可以有效简化开发人员对消息中间件的使用复杂度,让系统开发人员可以有更多的精力关注于核心业务逻辑的处理,实现无感知中间件编程 使用的都是org.springframework包下的内容,却可以操作RabbitMQ 或者 Kafka。

注意

SubscribableChannel:定义输入通道时,需要返回 SubscribableChannel 接口对象,该接口集成自 MessageChannel 接口,它定义了维护消息通道订阅者的方法
MessageChannel:当定义输出通道的时候,需要返回 MessageChannel 接口对象,该接口定义了向消息通道发送消息的方法

2.搭建项目 

  •  搭建消费者  

pom文件

  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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.10.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.yinzi</groupId>
  12. <artifactId>rabbit-stream-receiver</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>rabbit-stream-receiver</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  32. <version>2.1.3.RELEASE</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. <exclusions>
  39. <exclusion>
  40. <groupId>org.junit.vintage</groupId>
  41. <artifactId>junit-vintage-engine</artifactId>
  42. </exclusion>
  43. </exclusions>
  44. </dependency>
  45. </dependencies>
  46. <dependencyManagement>
  47. <dependencies>
  48. <dependency>
  49. <groupId>org.springframework.cloud</groupId>
  50. <artifactId>spring-cloud-dependencies</artifactId>
  51. <version>${spring-cloud.version}</version>
  52. <type>pom</type>
  53. <scope>import</scope>
  54. </dependency>
  55. </dependencies>
  56. </dependencyManagement>
  57. <build>
  58. <plugins>
  59. <plugin>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-maven-plugin</artifactId>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

接收消息的接口

@input:

  1. public interface ReceiverService {
  2. //@Input定义输入通道
  3. @Input("test-exchange")
  4. SubscribableChannel receive();
  5. }

实现类

  1. @Service
  2. @EnableBinding({ReceiverService.class})
  3. public class ReceiverServiceImpl {
  4. //@StreamListener,主要定义在方法上,作用是将被修饰的方法注册为消息中间件上数据流的事件监听器,注解中的属性值对应了监听的消息通道名。
  5. @StreamListener(value = "test-exchange")
  6. public void onReceive(byte[] msg){
  7. //处理消息
  8. System.out.println("ReceiverServiceImpl.onReceive:"+new String(msg));
  9. }
  10. }

消息发送方:

  1. @Service
  2. public interface SenderService {
  3. // @Output定义输出通道
  4. @Output("test-exchange")
  5. MessageChannel send();
  6. }

启动类:

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableBinding(value = {SenderService.class})
  4. public class RabbitStreamSenderApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(RabbitStreamSenderApplication.class, args);
  7. }
  8. }

详细在码云:点我跳转到码云

 

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

闽ICP备14008679号