ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot2.x整合rabbitMQ

2019-07-07 09:52:29  阅读:183  来源: 互联网

标签:String springframework annotation springboot2 rabbitMQ 整合 org import public


一,相关依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>    

 

二、添加配置

spring:
    rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: admins
        password: admins
        virtual-host: /test_rabbitMq

 

 

三、创建队列和交换机并进行绑定

package com.liangjian.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
  
//队列名
  public static String FANOUT_SMS_QUEUE="fanout_sms_queue";
  //创建队列
    @Bean
    public Queue fanoutSmsQueue(){
        return new Queue(FANOUT_SMS_QUEUE) ;
    }

  //创建交换机 @Bean public FanoutExchange fanoutExchange1(){ return new FanoutExchange("fanoutExchange1"); }
  
  //队列与交换机进行绑定 @Bean Binding bindingSms(){ return BindingBuilder.bind(fanoutSmsQueue()).to(fanoutExchange1()); } }

 

四、producer生产者发送消息

package com.liangjian.producer;

import com.liangjian.config.RabbitMQConfig;
import com.liangjian.util.SmsUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class FanoutProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String massage){
        String msg = "生成者发布消息:"+massage;
        rabbitTemplate.convertAndSend(RabbitMQConfig.FANOUT_SMS_QUEUE,msg);
    }

}

 

五、consumer消费者获取消息

package com.liangjian.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component

@Slf4j
public class FanoutSmsConsumer {
  
  @RabbitListener(queues = "fanout_sms_queue")
  @RabbitHandler
  public void process(Message massage) throws UnsupportedEncodingException {
      String id = massage.getMessageProperties().getMessageId();
   String msg =new String( massage.getBody(),"UTF-8");
  log.info(id+">>>>>>>>>>"+msg);
  JSONObject jsonObject = JSONObject.parseObject(msg);
  Integer filmID = jsonObject.getInteger("userID");
  String nums = jsonObject.getString("phone");
  log.info("filmID="+filmID+">>>>>>>>>>>>>>>>>>>>>>>> nums="+nums);
  }
 }

 

六、controller调用生产者发布消息

package com.liangjian.controller;

import com.alibaba.fastjson.JSONObject;
import com.liangjian.producer.FanoutProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@RequestMapping("/ticket")
@Controller
@Slf4j
public class TicketController {

    @Autowired
    private FanoutProducer fanoutProducer;


    @GetMapping("/getTicket")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public String getTicket(Integer userID,String phone){

           JSONObject jsonObject=new JSONObject();
            jsonObject.put("userID",userID));
            jsonObject.put("phone",phone);
            fanoutProducer.sendTicketMsg(jsonObject.toJSONString());
               
        return  "发送消息成功!";
    }

}

 

七、总结

对于不同的交换机类型,只需创建相应的交换机。

 @Bean
    DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }

    @Bean
    Binding bindingSms() {
        return BindingBuilder.bind(directSmsQueue()).to(directExchange()).with("key.sms");
    }
 

发送消息时,携带routingKey

 public void send(String queueName){
        String msg="my_fanout_msg:"+ new Date();
        System.out.println("生产者发布消息msg:"+msg);
        rabbitTemplate.convertAndSend(queueName,"key.sms",msg);
    }

 

Topic交换机routingkey支持通配符匹配:

String routingkey = “testTopic.#”;
String routingkey = “testTopic.*”;

    • *表示只匹配一个词
    • #表示匹配多个词

标签:String,springframework,annotation,springboot2,rabbitMQ,整合,org,import,public
来源: https://www.cnblogs.com/castlechen/p/11145134.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有