概述
上篇文章介绍了rabbitmq的原理实现,如果不知道rabbitmq的原理实现的童鞋推荐先看下上篇文章在继续这章的实践。
windows本地安装rabbitmq介绍
源码下载地址 https://gitee.com/v_soul/boot-rabbitmq-produce,https://gitee.com/v_soul/boot-rabbitmq-consumer
springboot集成rabbitmq实战 ------ direct模式
首先创建两个springboot项目
一 生产者项目结构
消费者项目结构
1 pom文件添加spring-boot-starter-amqp支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2 配置文件配置rabbitmq
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
这里的用户名密码默认guest,spring.application.name如果生产者和消费者不是同一个项目不能相同(坑1)
3 配置队列
package com.rabbit.produce.config;
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");
}
}
4 创建发送者
package com.rabbit.produce.controller;
import com.rabbit.produce.entrty.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.Date;
@Controller
public class HelloSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "sender object " + new Date();
this.rabbitTemplate.convertAndSend("hello", context);
}
}
5 创建接受者(在当前项目创建一个,在消费者项目中同样创建一个接受者,pom,application同上)
当前项目
package com.rabbit.produce.controller;
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("Receiver1
: " + hello);
}
}
消费者项目
package com.rabbit.product.controller;
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("Receiver2
: " + hello);
}
}
测试结果
生产者项目打印
Receiver1
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver1
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver1
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver1
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver1
: sender object Tue Sep 25 15:55:54 CST 2018
消费者项目打印
Receiver2
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver2
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver2
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver2
: sender object Tue Sep 25 15:55:54 CST 2018
Receiver2
: sender object Tue Sep 25 15:55:54 CST 2018
注意事项
注意,发送者和接收者的queue name必须一致,不然不能接收
一个发送者,N个接受者,经过测试会均匀的将消息发送到N个接收者中
多对多发送
复制发送者
package com.rabbit.produce.controller;
import com.rabbit.produce.entrty.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.Date;
@Controller
public class HelloSender2 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "sender object2 " + new Date();
this.rabbitTemplate.convertAndSend("hello", context);
}
}
测试
package com.rabbit.produce;
import com.rabbit.produce.controller.HelloSender;
import com.rabbit.produce.controller.HelloSender2;
import com.rabbit.produce.entrty.User;
import org.junit.Test;
import org.junit.runner.RunWith;
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
public class BootRabbitmqProduceApplicationTests {
@Autowired
private HelloSender helloSender;
@Autowired
private HelloSender2 helloSender2;
@Test
public void hello() throws Exception {
for (int i = 0; i < 10; i++) {
helloSender.send();
helloSender2.send();
}
}
}
测试结果
生产者项目测试结果
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
Receiver1
: sender object2 Tue Sep 25 16:03:09 CST 2018
消费者项目测试结果
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
Receiver2
: sender object Tue Sep 25 16:03:09 CST 2018
有童鞋可能会问生产者项目打印的都是sender object2啊?嗯你说的没错但是看下打印数量10个,在看下消费者打印结果也是10个,嗯rabbit根据自身策略会平均转发到接受者的
高级使用传输对象
定义实体类
package com.rabbit.produce.entrty;
import java.io.Serializable;
import java.util.Date;
/**
* @author : lqf
* @description :
* @date : Create in 14:04 2018/9/25
*/
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
'}';
}
}
定义发送者
package com.rabbit.produce.controller;
import com.rabbit.produce.entrty.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.Date;
@Controller
public class HelloSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(User user) {
this.rabbitTemplate.convertAndSend("hello", user);
}
}
生产者项目定义接受者
package com.rabbit.produce.controller;
import com.rabbit.produce.entrty.User;
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(User user) {
System.out.println("Receiver1
: " + user);
}
}
消费者项目定义接受者
package com.rabbit.produce.controller;
import com.rabbit.produce.entrty.User;
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(User user) {
System.out.println("Receiver2
: " + user);
}
}
测试
package com.rabbit.produce;
import com.rabbit.produce.controller.HelloSender;
import com.rabbit.produce.controller.HelloSender2;
import com.rabbit.produce.entrty.User;
import org.junit.Test;
import org.junit.runner.RunWith;
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
public class BootRabbitmqProduceApplicationTests {
@Autowired
private HelloSender helloSender;
@Test
public void hello() throws Exception {
for (int i = 0; i < 10; i++) {
helloSender.send(new User(123, "测试", 18));
}
}
}
生产者项目测试结果
Receiver1
: User{id=123, name='测试', age=18}
Receiver1
: User{id=123, name='测试', age=18}
Receiver1
: User{id=123, name='测试', age=18}
Receiver1
: User{id=123, name='测试', age=18}
Receiver1
: User{id=123, name='测试', age=18}
消费者项目测试结果
Receiver2
: User{id=123, name='测试', age=18}
Receiver2
: User{id=123, name='测试', age=18}
Receiver2
: User{id=123, name='测试', age=18}
Receiver2
: User{id=123, name='测试', age=18}
Receiver2
: User{id=123, name='测试', age=18}
注意问题
上述代码中实体类的包名大家可能注意到了是一样的,包名如果不一样是不能同步接收到对象传递的(坑2)
最后
以上就是舒心糖豆为你收集整理的(十三)springboot实战rabbitmq --- direct模式的全部内容,希望文章能够帮你解决(十三)springboot实战rabbitmq --- direct模式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复