1:首先,您需要安装支持的 Windows 版Erlang。下载并运行Erlang for Windows 安装程序。
下载地址:http://www.erlang.org/downloads
2:运行RabbitMQ安装程序rabbitmq-server-3.7.3.exe(下载地址 http://www.rabbitmq.com/install-windows.html )注意版本;它将RabbitMQ安装为Windows服务并使用默认配置启动它。同样,一直nest就行
3:设置环境(与java环境配置一样,这里不详细介绍。。)配置完 检查是否成功 开始-运行-cmd,输入erl,返回版本号表示配置成功
4:RabbitMQ测试
测试地址 http://localhost:15672/
默认的用户名:guest
默认的密码为:guest
项目中实际应用
1:导入MQ包(可在项目构建时勾选RabbitMQ)
com.rabbitmq
amqp-client
5.1.2
2:新建配置文件(创建hello,ABC,两个队列)
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue(){
return new Queue("hello");
}
@Bean
public Queue tangQueue(){
return new Queue("ABC");
}
}
3:新建SendMQ类(向队列hello中发送“快点啊”,队列ABC中发送“今天天气很好,你好吗?”)
@Component
public class SendMQ{
@Autowired
private AmqpTemplate rabbitmqTemplate;
public void send() {
String content = "快点啊" + new Date();
System.out.println("Sender:" + content);
rabbitmqTemplate.convertAndSend("hello", content);
String a = "今天天气很好,你好吗?";
rabbitmqTemplate.convertAndSend("ABC", a);
}
}
4:新建消息接受类(用于监听并接受队列hello中消息)
@Component
@RabbitListener(queues = "hello") // 监听的队列hello
public class Receiver {
@RabbitHandler
public void process(String hello){
System.out.println("hello:" + hello);
}
}
用于监听并接受队列ABC中消息
@Component
@RabbitListener(queues = "ABC") // ABC
public class Receiver2 {
@RabbitHandler
public void process(String ABC){
System.out.println("ABC:"+ABC);
}
}
5:用junit测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test01ApplicationTests {
@Autowired
private SendMQ sender;
@Test
public void contextLoads() {
sender.send();
}
}
今天先到这里。。。。。