RabbitMq创建队列绑定交换机

声明交换机

@Bean
public TopicExchange exchange() {
    return new TopicExchange(exchange);
}

声明队列

@Bean
public Queue delayedQueue(){
    return new Queue(queue);
}
@Bean
public Queue delayedQueue1(){
    return new Queue(queue1);
}

队列绑定交换机

@Bean
public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue queue,
                                    @Qualifier("exchange") Exchange exchange){
    return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
}

配置文件:

rabbitmq:
  exchange: 交换器名称
  host: ip地址
  port: 端口号 默认5672
  username: 用户名
  password: 密码
  virtualHost: /
  queue: 队列名称

完整代码:

@Configuration
public class RabbitmqConfig {
  
    @Value("${rabbitmq.exchange}")
    private String exchange;

    @Value("${rabbitmq.host}")
    private String host;

    @Value("${rabbitmq.port}")
    private Integer port;

    @Value("${rabbitmq.username}")
    private String username;

    @Value("${rabbitmq.password}")
    private String password;

    @Value("${rabbitmq.virtual-host}")
    private String virtualHost;

    @Value("${rabbitmq.queue}")
    private String queue;


    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(exchange);
    }


    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

    @Bean
    public Queue delayedQueue(){
        return new Queue(queue);
    }
 
    @Bean
    public Binding queueBindingExchange(@Qualifier("delayedQueue") Queue queue,
                                        @Qualifier("exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("#").noargs();
    }
   
}

不管是本地还是虚拟机还是服务器部署了rabbitmq之后,启动服务:

访问:http://ip地址:15672/#/queues

查看队列:

查看交换机:

 

 查看队列和交换机绑定情况:

猜你喜欢

转载自blog.csdn.net/weixin_43005845/article/details/119907261