springboot 消息服务管理

springboot 消息服务管理学习目标 1 了解为什么要使用消息中间件 2 属性 RabbitMQ 消息中间件的基本概念和工作原理 3 属性 Spring Boot 与 RabbitMQ 的整合搭建 4 掌握 Spring Boot 与 RabbitMQ 整合实现常用的工作模式 一 为什么要用消息中间件 异步处理 应用解耦 流量削峰 分布式事务管理

大家好,我是讯享网,很高兴认识大家。

学习目标

1、 了解为什么要使用消息中间件

2、 属性RabbitMQ消息中间件的基本概念和工作原理

3、 属性Spring Boot与RabbitMQ的整合搭建

4、 掌握Spring Boot与RabbitMQ整合实现常用的工作模式

(一)为什么要用消息中间件

异步处理、应用解耦、流量削峰、分布式事务管理

消息中间件分别有:

ActiveMQ RabbitMQ kafka rocketMQ

(二)以RabbitMQ 为例深入学习

  1. 安装 erlang 、 RabbitMQ
  2. 配置

# ??RabbitMQ?????????

spring.rabbitmq.host=localhost

spring.rabbitmq.port=5672

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

#??RabbitMQ??????/???????

spring.rabbitmq.virtual-host=/

(三)发布订阅模式

新建User类

RabbitMQService 发布、订阅参考此类

@Service

public class RabbitMQService {

/

* 使用基于注解的方式实现消息服务

* 1.1Publish/Subscribe工作模式接收,处理邮件业务

* @param user

*/

@RabbitListener(bindings =@QueueBinding(value =@Queue("fanout_queue_email"), exchange =@Exchange(value = "fanout_exchange",type = "fanout")))

public void psubConsumerEmailAno(User user) {

System.out.println("邮件业务接收到消息: "+user);

}

/

* 1.2Publish/Subscribe工作模式接收,处理短信业务

* @param user

*/

@RabbitListener(bindings =@QueueBinding(value =@Queue("fanout_queue_sms"),exchange =@Exchange(value = "fanout_exchange",type = "fanout")))

public void psubConsumerSmsAno(User user) {

System.out.println("短信业务接收到消息: "+user);

}

//-----------------------------------------------------------------------------------------

/

* 2.1、路由模式消息接收,处理error级别日志信息

* @param message

*/

@RabbitListener(bindings =@QueueBinding(value =@Queue("routing_queue_error"),exchange =@Exchange(value = "routing_exchange",type = "direct"),key = "error_routing_key"))

public void routingConsumerError(String message) {

System.out.println("接收到error级别日志消息: "+message);

}

/

* 2.2、路由模式消息接收,处理infoerrorwarning级别日志信息

* @param message

*/

@RabbitListener(bindings =@QueueBinding(value =@Queue("routing_queue_all"),exchange =@Exchange(value = "routing_exchange",type = "direct"),key = {"error_routing_key","info_routing_key","warning_routing_key"}))

public void routingConsumerAll(String message) {

System.out.println("接收到info、error、warning等级别日志消息: "+message);

}

//------------------------------------------------------------------

/

* 3.1、通配符模式消息接收,进行邮件业务订阅处理

* @param message

*/


讯享网

@RabbitListener(bindings =@QueueBinding(value =@Queue("topic_queue_email"),exchange =@Exchange(value = "topic_exchange",type = "topic"),key = "info.#.email.#"))

public void topicConsumerEmail(String message) {

System.out.println("接收到邮件订阅需求处理消息: "+message);

}

/

* 3.2、通配符模式消息接收,进行短信业务订阅处理

* @param message

*/

@RabbitListener(bindings =@QueueBinding(value =@Queue("topic_queue_sms"),exchange =@Exchange(value = "topic_exchange",type = "topic"),key = "info.#.sms.#"))

public void topicConsumerSms(String message) {

System.out.println("接收到短信订阅需求处理消息: "+message);

}

}

RbbitMqConfig 配置类

@Configuration

public class RbbitMqConfig {

@Bean

public MessageConverter messageConverter(){

return new Jackson2JsonMessageConverter();

}

/

* 使用基于配置类的方式定制消息中间件

* @return

*/

// 1、定义fanout类型的交换器

@Bean

public Exchange fanout_exchange(){

return ExchangeBuilder.fanoutExchange("fanout_exchange").build();

}

// 2、定义两个不同名称的消息队列

@Bean

public Queue fanout_queue_email(){

return new Queue("fanout_queue_email");

}

@Bean

public Queue fanout_queue_sms(){

return new Queue("fanout_queue_sms");

}

// 3、将两个不同名称的消息队列与交换器进行绑定

@Bean

public Binding bindingEmail(){

return BindingBuilder.bind(fanout_queue_email()).to(fanout_exchange()).with("").noargs();

}

@Bean

public Binding bindingSms(){

return BindingBuilder.bind(fanout_queue_sms()).to(fanout_exchange()).with("").noargs();

}

}

(四)打包

1. jar包

2. war包

2.1 设置打包模式

<!-- 将打包方式声明为war-->
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>

2.2 配置引入tomcat依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

2.3 启动类的修改

启动类继承继承SpringBootServletInitializer,并重写configure

@SpringBootApplication
public class MqApplication extends SpringBootServletInitializer {

//继承SpringBootServletInitializer,并重写configure
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(MqApplication.class);
}

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

}

2.4 打包操作步骤同打jar。

 

小讯
上一篇 2025-04-02 18:11
下一篇 2025-03-09 13:15

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/59202.html