赞
踩
然后点击Add queue 添加,就会发现nbpuls已经添加成功
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- <version>2.0.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <version>2.0.1.RELEASE</version>
- </dependency>
- </dependencies>
- server:
- port: 8002
- spring:
- rabbitmq:
- host: 192.168.63.131
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class RabbitApplication {
-
- public static void main(String[] args) {
- SpringApplication.run( RabbitApplication.class, args);
- }
- }
- import com.zhao588.rabbit.RabbitApplication;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = RabbitApplication.class)
- public class ProductTest
- {
-
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- /*
- * 直接模式
- * */
- @Test
- public void sendMsg1(){
-
- rabbitTemplate.convertAndSend("nbplus","直接模式测试");
- }
- }
(5)运行测试这个方法
(6)运行完毕之后查看浏览器,已经接收到发放发出的消息
.4 代码实现-消息消费者
- import org.springframework.amqp.rabbit.annotation.RabbitHandler;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- @Component
- @RabbitListener(queues = "nbplus")
- public class Custmer1
- {
-
- @RabbitHandler
- public void getMsg(String msg){
- System.out.println("直接模式输出:"+msg);
- }
- }
已经成功输出
浏览器rabbitmq 消息数量归零,说明已经被消费。
再次运行时,发现又有一个第二个消费者工程可以接收 到消息。第三次运行也和第二种情况一样,说明这里遵循了负载均衡的规律。
1 什么是分列(Fanout)模式
点击boss添加绑定 team1和team2
绑定后效果如下:
- import org.springframework.amqp.rabbit.annotation.RabbitHandler;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- @Component
- @RabbitListener(queues = "team1")
- public class Custmer1
- {
-
- @RabbitHandler
- public void getMsg(String msg){
- System.out.println("team1分裂模式输出:"+msg);
- }
- }
创建消息监听类,用于监听team2的消息
- import org.springframework.amqp.rabbit.annotation.RabbitHandler;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- @Component
- @RabbitListener(queues = "team2")
- public class Custmer2
- {
- @RabbitHandler
- public void getMsg(String msg){
- System.out.println("team2分裂模式输出:"+msg);
- }
- }
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = RabbitApplication.class)
- public class ProductTest
- {
-
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- /*
- * 分裂模式
- * */
- @Test
- public void sendMsg2(){
-
- rabbitTemplate.convertAndSend("boss","","分裂模式测试");
- }
- }
启动
启动后结果如下
1.浏览器rabbitmq上创建主题模式交换器
创建成功topicmodel后
点击topicmodel,绑定队列team1 这个里的Rounting key可 随意写,不过得记住,后面用。
就行绑定team2
一共就绑定这三个。
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = RabbitApplication.class)
- public class ProductTest
- {
-
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- /*
- * 主题模式测试
- * */
- @Test
- public void sendMsg3(){
-
- rabbitTemplate.convertAndSend("topicmodel","good.abc","主题模式测试");
- }
- }
结果:
测试二 启动sendMsg3方法
启动:
结果:
测试三启动sendMsg3方法
启动:
结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。