首页 > 其他分享 >SpringCloud中Rabbitmq

SpringCloud中Rabbitmq

时间:2022-12-04 15:13:21浏览次数:37  
标签:String SpringCloud factory rabbitmq Rabbitmq spring new channel

1.pom配置,添加以来jar包

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-bus-amqp</artifactId>
4 </dependency>

2.配置文件配置

   

1 #RabbitMq
2 spring.rabbitmq.host=${dev.spring.rabbitmq.host}
3 spring.rabbitmq.port=${dev.spring.rabbitmq.port}
4 spring.rabbitmq.username=${dev.spring.rabbitmq.username}
5 spring.rabbitmq.password=${dev.spring.rabbitmq.password}
6
7 spring.rabbitmq.exchange=${dev.spring.rabbitmq.exchange}
8 spring.rabbitmq.routingkey=${dev.spring.rabbitmq.routingkey}
9 spring.rabbitmq.queue=${dev.spring.rabbitmq.queue}

setting.properties

 

3.rabbitmq消息发送一

 

1 @Autowired 
2 private AmqpTemplate amqpTemplate;
3 //key:消息名称;content:消息内容
4 public void send(String key,String content) {
5     amqpTemplate.convertAndSend(key, content); 
6 }


4.rabbitmq消息接收一

 

1 @Component
2 @EnableScheduling
3 public class ClmNeedPaperMqReciever extends ClmBaseMqReciever {
4 /**
5 *RabbitListener:rabbitmq监听;queues = "queues1":监听的队列名*称;@Payload String orderXML:接收到的消息内容
6 */
7 @RabbitHandler
8 @RabbitListener(queues = "queues1", containerFactory = "rabbitListenerContainerFactory")
9 public void process(@Payload String orderXML) {
10
11 //处理内容
12 }

接受消息

 

 

5.rabbitmq发送二

 

1 String MQname ="ClmClose";
2 ConnectionFactory factory = new ConnectionFactory();
3 factory.setHost("129.1.9.193");
4 factory.setUsername("admin");
5 factory.setPassword("admin");
6 factory.setPort(5672);
7 Connection connection;
8 try {
9 connection = factory.newConnection();
10 Channel channel = connection.createChannel();
11 channel.queueDeclare(MQname, true, false, false, null);
12 channel.basicPublish("", MQname, null, returnJSON.getBytes("UTF-8"));
13 channel.close();
14 connection.close();

发送mq消息

 

 

6.rabbitmq接收二

 

1 /* 建立连接 */
2 ConnectionFactory factory = new ConnectionFactory();
3 factory.setHost("129.1.9.193");// MQ的IP
4 factory.setPort(5672);// MQ端口
5 factory.setUsername("admin");// MQ用户名
6 factory.setPassword("admin");// MQ密码
7 Connection connection = factory.newConnection();
8 Channel channel = connection.createChannel();
9
10 /* 声明要连接的队列 */
11 channel.queueDeclare(QUEUE_NAME, true, false, false, null);
12 System.out.println("等待消息产生:");
13
14 /* 创建消费者对象,用于读取消息 */
15 QueueingConsumer consumer = new QueueingConsumer(channel);
16 channel.basicConsume(QUEUE_NAME, true, consumer);
17
18 /* 读取队列,并且阻塞,即在读到消息之前在这里阻塞,直到等到消息,完成消息的阅读后,继续阻塞循环 */
19 while (true) {
20 QueueingConsumer.Delivery delivery = consumer.nextDelivery();
21 String message = new String(delivery.getBody(),"UTF-8");
22 System.out.println("收到消息'" + message + "'");
23 }

接受mq消息

 

 

 

7.用接收一的方法去接收发送二的内容时需要转换,否则会出现乱码

 

1 @Component
2 @EnableScheduling
3 public class ClmNeedPaperMqReciever extends ClmBaseMqReciever {
4
5 /**
6 *RabbitListener:rabbitmq监听;queues = "queues1":监听的队
7 *列名称;@Payload String orderXML:接收到的消息内容
8 */
9 @RabbitHandler
10 @RabbitListener(queues = "queues1", containerFactory = "rabbitListenerContainerFactory")
11 public void process(@Payload String orderXML) {
12
13 //处理内容
14
15 // 解析报文对象,将byte数组还原
16 String[] listStr = orderXML.split(",");
17 byte[] listByte = new byte[listStr.length];
18 for (int i = 0; i < listStr.length; i++) {
19 listByte[i] = new Byte(listStr[i]);
20 }
21 orderXML = new String(listByte, "UTF-8");
22
23 }
24
25

接收消息

 




标签:String,SpringCloud,factory,rabbitmq,Rabbitmq,spring,new,channel
From: https://www.cnblogs.com/codeLearn/p/16949906.html

相关文章

  • SpringCloud中Redis
    1.引入redis相关jar包   pom配置<dependency>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-data-redis</artifa......
  • SpringCloud中Rabbitmq的使用
    1.pom配置,添加以来jar包 pom配置2.配置文件配置setting.properties3.rabbitmq消息发送一 发送消息4.rabbitmq消息接收一 接受消息5.rabbitmq发送二 发送mq消......
  • SpringCloud中Rabbitmq的使用
    1#RabbitMq2spring.rabbitmq.host=${dev.spring.rabbitmq.host}3spring.rabbitmq.port=${dev.spring.rabbitmq.port}4spring.rabbitmq.username=${dev.spring.ra......
  • SpringCloud中Redis的使用
    1.引入redis相关jar包 pom配置2.配置Redis相关信息 config.properties3.读取Redis链接信息RedisConn redis链接pojo4.RedisConfig RedisConfig5.RedisUtils......
  • rabbitmqctl set_permissions 报错 invalid command
    执行的命令如下:rabbitmqctlset_permissions-pmyvhostuser1'.*''.*''.*'结果报错:Error:invalidcommand'set_permissions.*.*.*'试了这些命令,都是同样的错......
  • SpringCloud+MyBatis+Redis整合—— 超详细实例(一)
    1、SpringCloud+MyBatisMyBatis是一款优秀的轻量级半自动持久层框架,与之相对应的还有hibernate框架。①  话不多说,接下来搭建SpringCloud+MyBatis环境:第一步......
  • SpringCloud+MyBatis+Redis整合—— 超详细实例
    SpringCloud+MyBatis+Redisredis①是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写与硬盘读写的差别,所以常常用作缓存,用......
  • SpringCloud游戏平台改造-Day1
    Day1今天主要的工作是有,新建项目结构(后期可能会根据实际情况修改),实现了登录注册API项目思路目前的项目思路为以下几部分:GameAuth:用来提供用户登录注册接口,认证接口......
  • SpringCloud游戏平台改造-Day2
    Day2今天主要目的是接入SpringSecurity和JWT,不多说开干!Day1Day2接入SpringSecurityStep1实现来自SpringSecurity的UserDetailService接口,实现它的loaduserByUser......
  • SpringCloud Alibaba(七) - JWT(JSON Web Token)
    原文链接:JWT详解:https://blog.csdn.net/weixin_45070175/article/details/1185592721、什么是JWT通俗地说,JWT的本质就是一个字符串,它是将用户信息保存到一个Json字符串......