springboot整合RabbitMq

springboot整合RabbitMq

direct方式

发送端
1.添加pom依赖

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

        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2.application.yml配置

server:
  port: 8091
spring:
  application:
    name: rabbitmq-provider
  rabbitmq:
    host: 47.110.157.82
    port: 5672
    username: liu
    password: 123456

3.RabbitMQ配置类DirectRabbitConfig

@Configuration
public class DirectRabbitConfig {

    //队列
    @Bean
    public Queue directQueue() {
        return new Queue("directQueue",true);
    }

    //Direct交换机
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    //绑定  将队列和交换机绑定
    @Bean
    Binding bindingDirect(Queue directQueue,DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with("routingkey001");
    }
}

4.发送消息

@Controller
public class SendController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("sendMessage")
    @ResponseBody
    public String sendMessage(String message){
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",message);
        map.put("createTime",createTime);
        //将消息携带绑定键值:routingkey001发送到交换机directExchange
        rabbitTemplate.convertAndSend("directExchange", "routingkey001", map);
        return "ok";
    }
}

接收端
1.添加pom.xml依赖

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

2.application.yml配置

server:
  port: 8092
spring:
  application:
    name: rabbitmq-consumer
  rabbitmq:
    host: 47.110.157.82
    port: 5672
    username: liu
    password: 123456

3.为了防止队列不存在报错,这里我们也添加RabbitMQ配置类DirectRabbitConfig

@Configuration
public class DirectRabbitConfig {

    //队列
    @Bean
    public Queue directQueue() {
        return new Queue("directQueue",true);
    }

    //Direct交换机
    @Bean
    DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    //绑定  将队列和交换机绑定
    @Bean
    Binding bindingDirect(Queue directQueue,DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with("routingkey001");
    }
}

4.接收消息

@Component
@RabbitListener(queues = "directQueue")//监听的队列名称 directQueue
public class ReceiveMessage {
    /**
     * 如果这个方法正常结束,spring会自动确认消息
     * 如果这个方法报错,消息不会确认,并且进入死循环
     * 因此在处理消息之前需要防重复处理
     * @param testMessage
     */
    @RabbitHandler
    public void process(Map testMessage) {
        System.out.println("准备接收消息了");
        //1.防重复处理代码

        //2.消息处理代码
        System.out.println("directReceiver消费者收到消息  : " + testMessage.toString());
    }
}

fanout方式

发送端
1.添加pom依赖

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

        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2.application.yml配置

server:
  port: 8091
spring:
  application:
    name: rabbitmq-provider
  rabbitmq:
    host: 47.110.157.82
    port: 5672
    username: liu
    password: 123456

3.RabbitMQ配置类FanoutRabbitConfig

@Configuration
public class FanoutRabbitConfig {

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

}

4.发送消息

@Controller
public class SendController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("sendMessage")
    @ResponseBody
    public String sendMessage(String message){
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",message);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", "", map);
        return "ok";
    }
}

接收端
1.添加pom依赖

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

2.application.yml配置

server:
  port: 8092
spring:
  application:
    name: rabbitmq-consumer
  rabbitmq:
    host: 47.110.157.82
    port: 5672
    username: liu
    password: 123456

3.RabbitMQ配置类FanoutRabbitConfig

@Configuration
public class FanoutRabbitConfig {

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

}

4.接收消息

@Component
public class FanoutReceive {

    @RabbitListener(bindings = {
            @QueueBinding(value =@Queue(),//创建一个随机队列
                    exchange = @Exchange(name = "fanoutExchange",type = "fanout"))
    })
    public void fanoutReceive(Map messgae){
        System.out.println("fanoutReceiver01消费者收到消息"+messgae.toString());
    }


    @RabbitListener(bindings = {
            @QueueBinding(value =@Queue(),//创建一个随机队列
                    exchange = @Exchange(name = "fanoutExchange",type = "fanout"))
    })
    public void fanoutReceive02(Map messgae){
        System.out.println("fanoutReceiver02消费者收到消息"+messgae.toString());
    }
}

topic方式

发送端
1.添加pom依赖

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

        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2.application.yml配置

server:
  port: 8091
spring:
  application:
    name: rabbitmq-provider
  rabbitmq:
    host: 47.110.157.82
    port: 5672
    username: liu
    password: 123456

3.RabbitMQ配置类FanoutRabbitConfig

@Configuration
public class TopicRabbitConfig {

    @Bean
    TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }
}

4.发送消息

    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("sendMessage")
    @ResponseBody
    public String sendMessage(String message){
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",message);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("topicExchange", "aa.bb", map);
        return "ok";
    }
}

接收端
1.添加pom依赖

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

2.application.yml配置

server:
  port: 8092
spring:
  application:
    name: rabbitmq-consumer
  rabbitmq:
    host: 47.110.157.82
    port: 5672
    username: liu
    password: 123456

3.接收消息

@Component
public class TopicReceive {

    @RabbitListener(bindings = {
            @QueueBinding(value =@Queue("topicQueue001"),//创建一个随机队列
                    key = {"aa"},
                    exchange = @Exchange(name = "topicExchange",type = "topic"))
    })
    public void topicReceive01(Map messgae){
        System.out.println("topicReceive01消费者收到消息"+messgae.toString());
    }

    @RabbitListener(bindings = {
            @QueueBinding(value =@Queue("topicQueue002"),//创建一个随机队列
                    key = {"aa.*"},
                    exchange = @Exchange(name = "topicExchange",type = "topic"))
    })
    public void topicReceive02(Map messgae){
        System.out.println("topicReceive02消费者收到消息"+messgae.toString());
    }

    @RabbitListener(bindings = {
            @QueueBinding(value =@Queue("topicQueue003"),//创建一个随机队列
                    key = {"aa.#"},
                    exchange = @Exchange(name = "topicExchange",type = "topic"))
    })
    public void topicReceive03(Map messgae){
        System.out.println("topicReceive03消费者收到消息"+messgae.toString());
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值