使用注解的方式进行配置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;
}

测试结果:

相关推荐
掘金-我是哪吒11 小时前
分布式微服务系统架构第156集:JavaPlus技术文档平台日更-Java线程池使用指南
java·分布式·微服务·云原生·架构
亲爱的非洲野猪11 小时前
Kafka消息积压的多维度解决方案:超越简单扩容的完整策略
java·分布式·中间件·kafka
活跃家族11 小时前
分布式压测
分布式
前端世界13 小时前
HarmonyOS开发实战:鸿蒙分布式生态构建与多设备协同发布全流程详解
分布式·华为·harmonyos
DavidSoCool13 小时前
RabbitMQ使用topic Exchange实现微服务分组订阅
分布式·微服务·rabbitmq
掘金-我是哪吒14 小时前
分布式微服务系统架构第158集:JavaPlus技术文档平台日更-JVM基础知识
jvm·分布式·微服务·架构·系统架构
东窗西篱梦15 小时前
Redis集群部署指南:高可用与分布式实践
数据库·redis·分布式
Acrel_Fanny15 小时前
Acrel-1000系列分布式光伏监控系统在湖北荆门一马光彩大市场屋顶光伏发电项目中应用
分布式
xufwind15 小时前
spark standlone 集群离线安装
大数据·分布式·spark
半新半旧16 小时前
Redis集群和 zookeeper 实现分布式锁的优势和劣势
redis·分布式·zookeeper