SpringCloud Stream消息驱动之消费者_TOP__ONE的博客-程序员宅基地

技术标签: RabbitMQ  springcloud  MQ  stream  SpringCloud  消息驱动  Springboot  消息驱动消费者  

所有代码都在github上:https://github.com/demonruin/cloud2020/tree/master

 

本文演示是的SpringCloud Stream的消息生产者,和RabbitMQ进行配合,基本上与生产者一样

1、构建生产者项目cloud-stream-rabbitmq-consumer8802,添加pom文件依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>
        <!--基础配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2、构建application.yml文件,与生产者不同的地方就是需要将bindings的output在消费端修改成input

server:
  port: 8802

spring:
  application:
    name: cloud-stream-rabbitmq-consumer
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: admin
                password: admin
                virtual_host: my_vhost
      bindings: # 服务的整合处理
        input: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit # 设置要绑定的消息服务的具体设置

eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: receive-8802.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址

3、构建主启动类

/**
 * created by king on 2020/4/28 3:50 下午
 */
@SpringBootApplication
public class StreamConsumerMain8002 {
    public static void main(String[] args) {
            SpringApplication.run(StreamConsumerMain8002.class,args);
        }
}

4、构建一个controller,来接收rabbitmq的产生的消息,需要用到注解@Component、@EnableBinding(Sink.class)、@StreamListener(Sink.INPUT)

package com.king.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * created by king on 2020/4/28 3:53 下午
 */
@Component
@EnableBinding(Sink.class)
public class StreamConsumerReceiveMessageListnerController {
    @Value("${server.port}")
    private String serverPort;
    
    @StreamListener(Sink.INPUT)
    public void input(Message<String>message){
        System.out.println("我是消费者1号,接收到的消息为:"+message.getPayload()+"\t 端口号为:"+serverPort);
    }
}

5、启动RabbitMQ、eureka7001、provider8801、consumer8802,然后访问8801,产生消息,而8802的控制台也会收到消息

 

到此,消息驱动的消费者也搭建完毕了,接下来下面这篇文章会从消息重复、分组消费和消息持久化方面来做几个测试~有兴趣的就移步下一篇

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/TOP__ONE/article/details/105815565

智能推荐

GIT分支合并命令之git-merge_git merge --quit_yaoshengting的博客-程序员宅基地

名称NAMEgit-merge -将两个或多个开发历史联系在一起概要SYNOPSISgit merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit] [--no-verify] [-s &lt;strategy&gt;] [-X &lt;strategy-option&gt;] [-S[&lt;keyid&gt;]] ...

grep -A -B -C -a -c -n -v -i等_grep -c_You丶小明快跑的博客-程序员宅基地

grep -A -B -C(大写) 后面都跟阿拉伯数字-A是显示匹配后和它后面的n行。after-B是显示匹配行和它前面的n行。 before-C是匹配行和它前后各n行。 context例如:grep -A 1 hello test.txt就是搜索test.txt,找到匹配“hello”字串的行,显示该行和后面的1行。例如:grep -B 1 hello test.tx...

【解题报告】SPOJ - SUBST1 New Distinct Substrings 后缀数组_Toothable的博客-程序员宅基地

求字符串不同子串的个数:不能字符串hash了后缀数组解法:考虑排名为i的后缀,其对子串的贡献为n-sa[i]+1,但是我们会发现这里面有和之前重复的,所以要减去这些重复的,而height[i]就是sa[i]和sa[i-1]之间重复的个数。所以最后的答案就是对(n-sa[i]+1-height[i])求和AC代码:#include &lt;cstdio&gt;#include &lt;cstring&gt;#include &lt;algorithm&gt;using namespace std

基于HBuilderX创建移动app项目并利用mui实现简单页面跳转_hbuild app内跳转窗口置顶内核_Mr.wu`的博客-程序员宅基地

刚开始学,听老师讲了一些介绍,要求我们做一个页面跳转,他的是使用什么icon的来实现的,但是看不懂,就使用了mui来做了。这是我项目的下载链接https://github.com/laowuniubi/androidApp.git。先利用HBuilderX创建一个基于mui的5+App的项目新建一个目录page,存储HTML代码,格局如下所示:效果图如下:接下来写代码了:...

【Facebook分享】网页针对Facebook分享做爬虫优化(开放图谱标签)_"property=\"fb:app_id"_snow_finland的博客-程序员宅基地

App分享到Facebook在2019年政策上有了变化,将无法再通过指定分享的标题、描述、图片等,而是提供了在被分享的网页链接上加上特定的标签供Facebook的爬虫抓取来(Facebook称为开放图谱标签)。&lt;meta property="og:url" content="https://dev.demo.com/test.php" /&gt;&lt;meta property="...

随便推点

Exclusive Group在以色列收购SecureWave,进一步推进全球VAD覆盖范围_sinat_41698914的博客-程序员宅基地

Exclusive在“硅溪”建立业务据点及取得其他主要欧洲、中东和非洲(EMEA)市场的新通路,为收购以色列领先的独立网络安全VAD之一增加全球优势 巴黎和以色列特拉维夫--(美国商业资讯)--增值服务和技术(VAST)集团Exclusive Group今天宣布收购以色列领先的独立网络安全VAD之一SecureWave。此举为Exclusive Group的全球市场渗透增加了又一个发达经济体,...

Linux-安装显卡驱动NVIDIA,CUDA和cuDNN及所遇问题_spring_willow的博客-程序员宅基地

1.安装显卡驱动删除旧驱动:sudo apt-get purge nvidia*打开禁用列表: sudo gedit /etc/modprobe.d/blacklist.conf在禁用列表中添加:blacklist nouveauoptions nouveau modeset=0更新:sudo update-initramfs -u|——报错:W: Possible mi...

为什么目前感觉大多数论文还是以resnet为主干网络而不是densenet?_浪子私房菜的博客-程序员宅基地

因为不像resnet那样简洁吧,但是理论贡献是绝对的。万法归宗,啥都是dense connect。shortcut ,fpn,aspp,unet++ 都是,只是dense的程度不一样而已。**训练一个完全dense的网络,然后在上面剪枝才是最好的方法,unet++如是说。首先谈这两个之间的区别和联系,DenseNet和ResNet都是基于分类任务提出来的网络模型架构,架构思想也基本一致,既从小模块堆积到layer,再堆积layer到网络。现如今的检测和分割模型都是基于分类的模型来做backb

Bash shell中bash、sh、source及“.”点等五种执行方式的区别与联系_晨曦蜗牛的博客-程序员宅基地

在众多Linux发行版中bash shell 可谓是随处可见。作为众多发行版的首选shell,对于bash shell的学习对我们来说,显得格外重要。在学习bash shell的过程中,bash、sh、source及英文输入状态下的点号经常交替出现,他们看起来作用都差不多,但是深究下去,他们也有着不小的区别与联系。下面就让我们以具体实验来看一下它们之间的区别与联系吧!一、查看当前系统支持的...

性能调优所使用的工具_aws c3.4xlarge_Lailikes的博客-程序员宅基地

使用 iostat 定位磁盘问题在一个性能测试集群,我们选择了 AWS c3.4xlarge 机型,主要是为了在一台机器的两块盘上面分别跑 TiKV。在测试一段时间之后,我们发现有一台 TiKV 响应很慢,但是 RocksDB 并没有相关的 Stall 日志,而且慢查询也没有。于是我登上 AWS 机器,使用iostat -d -x -m 5命令查看,得到如下输出:Device: ...

命令行的艺术,命令行技巧总结_hi_squirrel的博客-程序员宅基地

# 命令行的艺术[![Join the chat at https://gitter.im/jlevy/the-art-of-command-line](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jlevy/the-art-of-command-line?utm_source=badge&amp;amp;utm_medium=...

推荐文章

热门文章

相关标签