首页 > 其他分享 >sc stream-rabbit 优化版、绑定器-自定20230112

sc stream-rabbit 优化版、绑定器-自定20230112

时间:2023-01-16 16:37:05浏览次数:51  
标签:stream 20230112 springframework rabbit import org cloud

 

一、生产者【2062】

        1、pom.xml

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  <version>3.0.7.RELEASE</version>
</dependency>

 

       2、application.yml

sc stream-rabbit 优化版、绑定器-自定20230112_ide

 

server:
  port: 2062
  spring:
   application:
  name: providerstream2062
  rabbitmq:
  addresses: 127.0.0.1
  username: guest
  password: guest
  cloud:
  stream:
  bindings:
   input: #(默认邦定器:output)内置的获取信息的通道,从交换机(rabbit-exchang01)获取
  destination: rabbit-exchang01
  binders: #默认邦定器
  defaultRabbit:
  type: rabbit

 


         3、MessageSender 发送者

import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.cloud.stream.messaging.Source;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.stereotype.Component;

    /**
     * 负责向中间件发送数据
    */
    @Component
    @EnableBinding(Source.class)
    public class MessageSender {
     @Autowired
     private MessageChannel output;

     //发信息
     public void send(Object obj){
     output.send(MessageBuilder.withPayload(obj).build());
     }
    }

 

          4、Providerstream2062Application 

import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class Providerstream2062Application {
    public static void main(String[] args) {
     SpringApplication.run(Providerstream2062Application.class, args);
     }
     }

 

 5、Controller发送信息

           1)、

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

         2)、

import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class ControllerTest {
    @Autowired
    private MessageSender messageSender;
    @GetMapping(value = "/send")
    public void test(String msg){

     messageSender.send(msg);
     }    }

 

sc stream-rabbit 优化版、绑定器-自定20230112_spring_02

 

sc stream-rabbit 优化版、绑定器-自定20230112_ide_03

 

 

二、消费者【2058】

        1、pom.xml

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
  <version>3.0.7.RELEASE</version>
</dependency>

 

        2、MessageListener监听

import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.cloud.stream.annotation.StreamListener;
    import org.springframework.cloud.stream.messaging.Sink;
    import org.springframework.stereotype.Component;

    @Component
    @EnableBinding(Sink.class)
    public class MessageListener {
     //监听binding中的消息,绑定器input
    @StreamListener(Sink.INPUT)
    public void input(String message){
    System.out.println("获取到信息:"+message);
    }
    }

 

     3、application.yml

sc stream-rabbit 优化版、绑定器-自定20230112_ide_04


server:
  port: 2058
  spring:
   application:
  name: consumberstream2058
  rabbitmq:
  addresses: 127.0.0.1
  username: guest
  password: guest
  cloud:
  stream:
  bindings:
   input: #(默认邦定器:input)内置的获取信息的通道,从交换机(rabbit-exchang01)获取
  destination: rabbit-exchang01
  binders: #默认邦定器
  defaultRabbit:
  type: rabbit

 

      4、Consumberstream2058Application 

import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  @SpringBootApplication
  public class Consumberstream2058Application {
  public static void main(String[] args) {
  SpringApplication.run(Consumberstream2058Application.class, args);
  }
  }


三、Rabbit[15671]   web客户端

    #交换机rabbit-exchange01 直接发送信息

sc stream-rabbit 优化版、绑定器-自定20230112_ide_05


四、绑定器(自定义)

  

1、生产者【2062】

       2)、application.yml

sc stream-rabbit 优化版、绑定器-自定20230112_ide_06


server:
  port: 2062
  spring:
   application:
  name: providerstream2062
  rabbitmq:
  addresses: 127.0.0.1
  username: guest
  password: guest
  cloud:
  stream:
  bindings:
   output: #(默认邦定器:output)内置的获取信息的通道,从交换机(rabbit-exchang01)获取
  destination: rabbit-exchang01outputMy: #配置绑定器outputMy(自定义)
destination: rabbit-exchang01
  binders: #默认邦定器
  defaultRabbit:
  type: rabbit

 

         3)、MessageSender 发送者

import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.stereotype.Component;

    /**
    * 负责向中间件发送数据
    */
    @Component
    //@EnableBinding(Source.class)
    @EnableBinding(MyProcessor.class)
    public class MessageSender {
    @Autowired
    private MessageChannel outputMy;

    //发信息
    public void send(Object obj){
    outputMy.send(MessageBuilder.withPayload(obj).build());
     }
    }

 

       5)、Controller发送信息

           i)、

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

         ii)、

import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class ControllerTest {
    @Autowired
    private MessageSender messageSender;     @GetMapping(value = "/send")
    public void test(String msg){
     messageSender.send(msg);
     }    }


sc stream-rabbit 优化版、绑定器-自定20230112_spring_07


sc stream-rabbit 优化版、绑定器-自定20230112_ide_08


 

  6)、MyProcessor  绑定器(自定义)

import org.springframework.cloud.stream.annotation.Input;
  import org.springframework.cloud.stream.annotation.Output;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.SubscribableChannel;    public interface MyProcessor {
    String INPUT_MY="inputMy";
    String OUPUT_MY="outputMy";

    @Input(INPUT_MY)
    SubscribableChannel inputMy();

    @Output(OUPUT_MY)
    MessageChannel outputMy();
   }

 

2、消费者【2058】    

 

        2)、MessageListener监听   

import org.springframework.cloud.stream.annotation.EnableBinding;
   import org.springframework.cloud.stream.annotation.StreamListener;
   import org.springframework.stereotype.Component;

   @Component
   //@EnableBinding(Sink.class)
   @EnableBinding(MyProcessor.class)
   public class MessageListener {
  //监听binding中的消息
  //@StreamListener(Sink.INPUT)
  @StreamListener(MyProcessor.INPUT_MY)
  public void input(String message){
  System.out.println("获取到信息:"+message);
   }  }

 

     3)、application.yml

sc stream-rabbit 优化版、绑定器-自定20230112_spring_09

 

server:
  port: 2058
  spring:
   application:
  name: consumberstream2058
  rabbitmq:
  addresses: 127.0.0.1
  username: guest
  password: guest
  cloud:
  stream:
  bindings:
   input: #(默认邦定器:input)内置的获取信息的通道,从交换机(rabbit-exchang01)获取
  destination: rabbit-exchang01inputMy: #配置绑定器inputMy(自定义)
  destination: rabbit-exchang01
  binders: #默认邦定器
  defaultRabbit:
  type: rabbit

 

     5)、MyProcessor  绑定器(自定义)

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
  import org.springframework.messaging.MessageChannel;
  import org.springframework.messaging.SubscribableChannel;

  public interface MyProcessor {
  String INPUT_MY="inputMy";
  String OUPUT_MY="outputMy";

  @Input(INPUT_MY)
  SubscribableChannel inputMy();

  @Output(OUPUT_MY)
  MessageChannel outputMy();
  }

 

3、Rabbit[15671]   web客户端

     #交换机rabbit-exchange01 直接发送信息

 

sc stream-rabbit 优化版、绑定器-自定20230112_自定义_10

sc stream-rabbit 优化版、绑定器-自定20230112_ide_11

 

标签:stream,20230112,springframework,rabbit,import,org,cloud
From: https://blog.51cto.com/smallfa/6010512

相关文章

  • RabbitMQ GUI客户端工具
    RabbitMQGUI客户端工具(RabbitMQAssistant)平时用控制台或者网页进行管理不免有点不方便,尤其在读取消息的时候不支持过滤和批量发送消息,在此推荐一个漂亮的GUI客户端工具。......
  • sc stream-rabbit笔记20230111
                    自定义通道   ......
  • Microsoft 解决方案:配置File Stream异常
    Blog链接:https://blog.51cto.com/u_13637423应客户需求,需要在SQLServer上部署FileStreamProvider,将blob存储到其他存储介质中,但在配置过程中,创建filetable时SQLServe......
  • RabbitMQ说明与安装
    一、RabbitMQ介绍1.RabbitMQ的相关概念2007年发布,是一个在AMQP(高级消息队列协议)基础上完成的,可复用的企业消息系统,是当前最主流的消息中间件之一。RabbitMQ是......
  • buuctf 变异凯撒 Quoted-printable 1 rabbit
    这题很明显要用凯撒解密,凯撒密码就是简单的位移操作,虽然用工具很容易就解出,但它是变异的,需要对照ASCII表,毕竟有特殊字符在里面,除了用工具,还可以写个小程序跑一下。afZ_r9VY......
  • RabbitMQ 高级消息队列协议
    https://blog.csdn.net/qq_46127735/article/details/120498245http://www.codebaoku.com/it-linux/it-linux-112105.htmlhttps://baike.baidu.com/item/rabbitmq/937214......
  • 内网Linux下安装Nginx1.23,添加stream模块实现tcp/udp代理转发
    环境:centos7.6ngx_stream_core_module这个模块在1.9.0版本后将被启用。但是并不会默认安装,需要在编译时通过指定--with-stream参数来激活这个模块,window下并不支持udp......
  • 编译打包rabbitmq然后一键部署的简单方法
    摘要之前总结过一版,但是感觉不太全面想着本次能够将使用中遇到的问题总结一下.所以本次是第二版介质下载rabbitmq不区分介质的打包文件rabbitmq-server-generic-unix-3.11......
  • C# 中的Stream流
    流就是一个类的对象,很多文件的输入输出操作都以类的成员函数的方式来提供;流其实是一种信息的转换,是有序的,有输入和输出流(IO);1.FileStream文件流,读取和保存文件操作使用;//写......
  • sc stream-rabbit笔记20230115
            ......