SpringBoot学习笔记9(消息)

消息

一、消息概述

消息概述.png
消息概述.png
image.png.png

  • JMS与AMQP的差异
    JMS与AMQP的差异.png
二、异步处理

异步处理.png

三、RabbitMQ简介

RabbitMQ简介.png
RabbitMQ简介.png
RabbitMQ简介.png

四、RabbitMQ运行机制

RabbitMQ运行机制.png
Exchange类型.png
Fanout Exchange.png

五、安装与使用RabbitMQ
  • Linux 安装
-- 下载:
docker pull rabbitmq:3-management
-- 运行:
docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq xxx
xxx 代表 IMAGE ID
注意:下载过程中涉及的镜像问题请自行百度
- p 5672端口:主机与客户端进行通信的接口
- p 15672端口:访问web管理页面的接口
  • 测试
    打开web浏览器,输入:
127.0.0.1:15672
也可以将127.0.0.1替换成你的服务器地址

会看到如下页面:输入默认的账号与密码:guest进入rabbit服务
rabbit登录界面.png
rabbit页面.png

  • 添加第一个direct类型的Exchanges
    添加第一个direct类型的Exchanges.png
  • 添加消息队列
    添加消息队列.png
  • 为Exchanges绑定Queues
    为Exchanges绑定Queues.png
  • exchange.topic需要模糊匹配
    exchange.topic模糊匹配.png
  • exchange发送消息

exchange.direct 只会想与key匹配成功的Queue发送消息
exchange.fanout会向所有的Queue发送消息
exchange.topic会向与匹配规则相匹配的Queue发送消息

下面以exchange.direct发送消息为例
exchange.direct页面发送消息.png

  • Queues之helloworld获取消息
    helloworld获取消息.png
六、SpringBoot整合RabbitMQ
  • 引入spring-boot-starter-amqp
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
  • properties文件配置
spring.rabbitmq.host=xxx    //xxx: rabbitmq运行的ip地址
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#spring.rabbitmq.virtual-host
  • 主类
/*
* 自动配置
*  1、RabbitAutoConfiguration
*  2.有自动配置的连接工厂ConnectionFactory;
*  3.RabbitProperties封装了RabbitMQ的配置
*  4、RabbitTemplate,给RabbitMQ发送和接收消息
*  5、AmqpAdmin:RabbitMQ系统管理功能组件
*  6.@EnableRabbit + @RabbitListener 监听消息队列的内容
* */

@EnableRabbit   //开启基于注解的Rabbit模式
@SpringBootApplication
public class Springboot1300AmqpApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot1300AmqpApplication.class, args);
	}

}
  • 首先定制序列化规则
@Configuration
public class MyAMPQConfig {
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
  • Book对象
public class Book {

    private String bookName;

    private String author;

    public Book(){
        super();
    }

    public Book(String bookName, String author) {
        this.bookName = bookName;
        this.author = author;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

}
  • 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot1300AmqpApplicationTests {

	@Autowired
	RabbitTemplate rabbitTemplate;

	/*
	* 1、单播(点对点)
	* */
	@Test
	public void contextLoads() {
//      Message需要自己构造一个;定义消息体内容和消息头
//		rabbitTemplate.send(exchange,routeKey,message);

//		object默认当成消息体,只需要传入要发送的对象,自动序列化发给rabbitmq;
//		rabbitTemplate.convertAndSend(exchange,routeKey,object);

		Map<String,Object> map = new HashMap<>();
		map.put("msg","这是第一个消息");
		map.put("data", Arrays.asList("helloworld",123,true));
		//对象被默认序列化以后发送出去
//		rabbitTemplate.convertAndSend("exchange.direct","helloworld.news",new Book("XXX","王圣嵩"));
		rabbitTemplate.convertAndSend("exchange.direct","helloworld",map);


	}

	//接收数据,如何将数据自动的转为json数据
	@Test
	public void receive(){
		Object o = rabbitTemplate.receiveAndConvert("helloworld");
		System.out.println(o.getClass());
		System.out.println(o);
	}

	/*
	* 2.广播
	* */
	@Test
	public void sendMsg(){
		rabbitTemplate.convertAndSend("exchange.fanout","",new Book("XXX","王圣嵩"));
	}
}
  • 使用监听模式
@Service
public class BookService {

    //想要RabbitListener起作用,需要开启基于注解的Rabbit模式(主程序中标注@EnableRabbit)
    @RabbitListener(queues = "helloworld.news")
    public void receive(Book book){
        System.out.println("收到消息"+book);
    }
    
    @RabbitListener(queues = "helloworld")
    public void receive02(Message message) {
        System.out.println(message.getBody());
        System.out.println(message.getMessageProperties());
    }

}
  • 使用SpringBoot管理Exchange与Queue
@Test
	public void creatExchange() {
		amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
		System.out.println("创建完成");

		amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));

		// 创建绑定规则
		amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

页川叶川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值