rabbitMQ使用

1.pom.xml

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>

2.application.properties

spring.application.name=spirng-boot-rabbitmq

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3.配置rabbitmq

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue Queue() {
        return new Queue("hello");
    }

} 

配置hello这个队列

4.生产者

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
@Component
public class HelloSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context);
    }
}

5.消费者

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver  : " + hello);
    }

}

注意hello队列的对应


广播机制,订阅模式

1.配置

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutRabbitConfig {

    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }

}

将上面3个队列绑定在fanoutExchange这个消息队列上。Queue Amessage这个是有对应的,不要乱写

2.生产者

public void send1() {
        String context = "hi, fanout msg ";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
    }

3.消费者

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "fanout.A")
public class AMessageReceiver {
    @RabbitHandler
    public void process(String hello) {
        System.out.println("AMessageReceiver  : " + hello);
    }
}
这个其中的一个AMessage队列,其他的自己模仿写


RabbitMQ是一个使用Erlang实现的高并发高可靠AMQP消息队列服务器。它支持消息的持久化、事务、拥塞控制、负载均衡等特性,使得RabbitMQ在各种应用场景下被广泛使用RabbitMQ与Erlang和AMQP密切相关。 Erlang是一种编程语言,它特别适合构建高并发、分布式、实时的系统。RabbitMQ使用Erlang作为其底层开发语言,这使得它能够充分利用Erlang在并发处理和容错性方面的优势。 AMQP(Advanced Message Queuing Protocol)是一个开放标准的消息队列协议,用于在应用程序之间进行可靠的消息传递。RabbitMQ实现了AMQP协议,这意味着它可以与其他遵循AMQP协议的应用程序进行通信,实现可靠的消息传递机制。 要使用RabbitMQ,可以通过Docker安装,使用以下命令运行一个带有管理界面的RabbitMQ容器: ``` docker run -itd --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management ``` 在编写RabbitMQ程序时,可以利用其支持的多种消息模型,例如发布-订阅模型、工作队列模型、路由模型等,根据具体需求选择合适的模型来实现消息传递。 在配置RabbitMQ环境时,可以设置RABBITMQ_SERVER环境变量,并将RabbitMQ的安装路径添加到系统的path变量中,以方便在命令行中直接使用RabbitMQ命令。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [RabbitMQ使用详解](https://blog.csdn.net/qq_43410878/article/details/123656765)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值