使用注解的方式进行配置RabbitMQ

引入依赖:

        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>

配置application.yml 

server:
  port: 8082
spring:
  rabbitmq:
    host: 192.168.37.105
    port: 5672
    username: admin
    password: admin
    virtual-host: /

shop:
  exchange: shop.exchange.direct
  queue: shop.queue
  routingKey: shop.routingKey

 编写配置类消息转化器:

@Configuration
public class RabbitmqConfig {

    /**
     * 消息转化器
     * 将对象转化为 Json 字符串
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

创建生产者:

@RestController
@RequiredArgsConstructor
@RequestMapping("producer03")
public class Producer03Controller {

    private final RabbitTemplate rabbitTemplate;

    @GetMapping("/send01")
    public String sendMsgFanout(){
        rabbitTemplate.convertAndSend("exchange2.fanout","",new Student(2,"Alice","178",20));
        return "Producer02==>发送成功!";
    }

    @GetMapping("/send02")
    public String sendFanout(){
        rabbitTemplate.convertAndSend("exchange2.fanout","","Fanout发送消息!");
        return "Producer02==>发送成功!";
    }

    @GetMapping("/send03")
    public String sendMsgDirect(){
        rabbitTemplate.convertAndSend("exchange2.DIRECT","monian",new Student(2,"Alice","178",20));
        return "Producer02==>发送成功!";
    }

    @GetMapping("/send04")
    public String sendMsgDirectMsg(){
        rabbitTemplate.convertAndSend("exchange2.DIRECT","monian","发送的数据Direct!");
        return "Producer02==>发送成功!";
    }
}

创建消费者:

@Component
public class MsgListener02 {
    /**
     * 使用注解的方式,对交换机和队列进行装配
     * 如果存在就不创建
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue02"),
            exchange = @Exchange(value = "exchange2.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void readMsg(Student stu) {
        System.out.println("annotationListener::Fanout => :" + stu);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue02"),
            exchange = @Exchange(value = "exchange2.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void readMsg(String msg) {
        System.out.println("annotationListener::Fanout => :" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue03"),
            exchange = @Exchange(value = "exchange2.DIRECT", type = ExchangeTypes.DIRECT),
            key = "monian"
    ))
    public void readMsgDirect(Student stu) {
        System.out.println("annotationListener::Direct => :" + stu);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue03"),
            exchange = @Exchange(value = "exchange2.DIRECT", type = ExchangeTypes.DIRECT),
            key = "monian"
    ))
    public void readMsgDirect(String msg) {
        System.out.println("annotationListener::Direct => :" + msg);
    }

}

编写Student:

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
}

测试结果: 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值