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

引入依赖:

java 复制代码
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>

配置application.yml

XML 复制代码
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

编写配置类消息转化器:

java 复制代码
@Configuration
public class RabbitmqConfig {

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

创建生产者:

java 复制代码
@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==>发送成功!";
    }
}

创建消费者:

java 复制代码
@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:

java 复制代码
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private String phone;
    private Integer age;
}

测试结果:

相关推荐
_oP_i24 分钟前
Pinpoint 是一个开源的分布式追踪系统
java·分布式·开源
攻心的子乐2 小时前
Kafka可视化工具 Offset Explorer (以前叫Kafka Tool)
分布式·kafka
小林想被监督学习3 小时前
RabbitMQ 的7种工作模式
分布式·rabbitmq
初晴~4 小时前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
有一个好名字5 小时前
zookeeper分布式锁模拟12306买票
分布式·zookeeper·云原生
yukai080088 小时前
【最后203篇系列】002 - 两个小坑(容器时间错误和kafka模块报错
分布式·kafka
老猿讲编程9 小时前
OMG DDS 规范漫谈:分布式数据交互的演进之路
分布式·dds
C++忠实粉丝9 小时前
服务端高并发分布式结构演进之路
分布式
洛神灬殇10 小时前
彻底认识和理解探索分布式网络编程中的SSL安全通信机制
网络·分布式·ssl
wy02_11 小时前
Linux下载RabbitMQ,并解决Github拒绝访问443的问题
linux·rabbitmq·github