利用 Spring Boot RabbitMQ spring boot使用jetty spring boot cli 使用 spring boot 使用场景 spring boot 使用cxf springboot定时器使用 spring boot 使用jdbc spring boot 使用 spring boot aop 使用 spring boot 使用xml
好久没有写Spring Boot的内容了,正好比来在写Spring Cloud Bus的内容,因为内容会有一些相关性,所以先补一篇关于AMQP的整合。
AMQP是Advanced Message Queuing Protocol的简称,它是一个面向动静中心件的开放式尺度应用层和谈。AMQP界说了这些特征:
在Mac OS X中利用brew东西,可以很轻易的安装RabbitMQ的办事端,只需要按如下号令操作即可:
经由过程上面的号令,RabbitMQ Server的号令会被安装到/usr/local/sbin,并不会主动加到用户的情况变量中去,所以我们需要在.bash_profile或.profile文件中增添下面内容:
echo 'deb http://www.rabbitmq.com/debian/ testing main' |
sudo tee /etc/apt/sources.list.d/rabbitmq.list更新APT仓库的package list,执行sudo apt-get update号令安装Rabbit Server,执行sudo apt-get install rabbitmq-server号令
[/ol]
Rabbit治理
我们可以直接经由过程设置装备摆设文件的拜候进行治理,也可以经由过程Web的拜候进行治理。下面我们将介绍若何经由过程Web进行治理。
> rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@PC-201602152056... started 6 plugins.
打开浏览器并拜候:http://localhost:15672/,并利用默认用户guest登录,暗码也为guest。我们可以看到如下图的治理页面:
1447174-bfc9975ee2db624c.png
从图中,我们可以看到之前章节中提到的一些根基概念,好比:Connections、Channels、Exchanges、Queue等。第一次利用的读者,可以都点开看看都有些什么内容,熟悉一下RabbitMQ Server的办事端。
在Spring Boot中整合RabbitMQ是一件很是轻易的事,因为之前我们已经介绍过Starter POMs,此中的AMQP模块就可以很好的撑持RabbitMQ,下面我们就来具体说说整合过程:
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=spring
spring.rabbitmq.password=123456
建立动静出产者Sender。经由过程注入AmqpTemplate接口的实例来实现动静的发送,AmqpTemplate接口界说了一套针对AMQP和谈的根本操作。在Spring Boot中会按照设置装备摆设来注入其具体实现。在该出产者,我们会发生一个字符串,并发送到名为hello的队列中。
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "hello " + new Date();
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("hello", context);
}
}
建立动静消费者Receiver。经由过程@RabbitListener注解界说该类对hello队列的监听,并用@RabbitHandler注解来指定对动静的处置方式。所以,该消费者实现了对hello队列的消费,消费操作为输出动静的字符串内容。
@Component
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver : " + hello);
}
}
建立RabbitMQ的设置装备摆设类RabbitConfig,用来设置装备摆设队列、互换器、路由等高级信息。这里我们以入门为主,先以最小化的设置装备摆设来界说,以完成一个根基的出产和消费过程。
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}
建立应用主类:
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
建立单位测试类,用来挪用动静出产:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class HelloApplicationTests {
@Autowired
private Sender sender;
@Test
public void hello() throws Exception {
sender.send();
}
}
完成法式编写之后,下面最先测验考试运行。起首确保RabbitMQ Server已经最先,然后进行下面的操作:
同时,我们经由过程RabbitMQ的节制面板,可以看到Connection和Channels中包含当前毗连的条目。
Receiver : hello Sun Sep 25 11:06:11 CST 2016
经由过程上面的示例,我们在Spring Boot应用中引入spring-boot-starter-amqp模块,进行简单设置装备摆设就完成了对RabbitMQ的动静出产和消费的开辟内容。然而在现实应用中,我们还有良多内容没有演示,这里不做更多的讲解,读者可以自行查阅RabbitMQ的官方教程,有更周全的领会。
完整示例:Chapter5-2-1
开源中国:http://git.oschina.net/didispace/SpringBoot-Learning/tree/master/Chapter5-2-1GitHub:https://github.com/dyc87112/SpringBoot-Learning/tree/master/Chapter5-2-1
|