您的位置:首页 > 编程语言 > Java开发

Springboot+RabbitMQ消息队列

2019-01-31 16:19 435 查看

第一步:安装对应的RabbitMQ安装和配置

RabbitMQ是基于Erlang语言开发的。所以安装RabbitMQ之前需要先下载安装配置Erlang,下载地址:http://www.erlang.org/downloads 
并将安装后的......\erl9.0\bin的bin目录配置到path环境变量中。然后下载安装RabbitMQ。下载地址:http://www.rabbitmq.com/download.html 
安装完成之后在开始菜单中找到RabbitMQ Command Promt,打开控制台,输入命令:

rabbitmq-plugins enable rabbitmq_management

第二步:springboot对应的配置:

pom.xml中添加

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

application.properties文件添加配置

[code]#队列
spring.rabbitmq.host=localhost      //队列地址
spring.rabbitmq.port=5672           //端口号
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

第三步:配置队列

[code]@Configuration
public class RabbitMQconfig {
public static final String QUEUE = "direct_queue";
@Bean
public Queue directQueue(){
return new Queue(QUEUE,true);
}

}

第四步:发送端

[code]@SpringBootApplication
@MapperScan("com.csv.dao")
public class CsvApplication implements CommandLineRunner {
@Autowired
private AmqpTemplate amqpTemplate;
public static void main(String[] args) {
SpringApplication.run( CsvApplication.class, args );
}

@Override
public void run(String... args) {
String massage="哈喽,帅哥";
amqpTemplate.convertSendAndReceive( RabbitMQconfig.QUEUE,massage);
System.out.println("ok");
}
}

第五步:接收端

[code]public class Receiver {
@RabbitListener(queues = RabbitMQconfig.QUEUE)
public  void  ReceiveMesaage(String  message){
System.out.println("接受到"+message);
}
}

接收到的效果

这个是自己学习的一个简单的练习,如有不对,请指教。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: