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

测试结果:

相关推荐
文艺倾年2 小时前
【八股消消乐】浅尝Kafka性能优化
分布式·性能优化·kafka
钟琛......2 小时前
Redisson 的分布式锁
分布式
CodeWithMe6 小时前
【Note】《Kafka: The Definitive Guide》第11章:Stream Processing
分布式·kafka
落霞的思绪9 小时前
使用云虚拟机搭建hadoop集群环境
大数据·hadoop·分布式
Bug退退退12319 小时前
RabbitMQ 高级特性之事务
java·分布式·spring·rabbitmq
CodeWithMe20 小时前
【Note】《Kafka: The Definitive Guide》第四章:Kafka 消费者全面解析:如何从 Kafka 高效读取消息
分布式·kafka
Gauss松鼠会1 天前
GaussDB应用场景全景解析:从金融核心到物联网的分布式数据库实践
数据库·分布式·物联网·金融·database·gaussdb
咖啡啡不加糖1 天前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
@Jackasher1 天前
Redisson是如何实现分布式锁的?
分布式
❀always❀1 天前
深入浅出分布式限流(更新中)
分布式·wpf