传统的项目开发中业务流程以串行方式,执行了模块1—》模块2–》模块3

  而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点:

  (1)整个流程的执行响应等待时间比较长;

  (2)如果某一个模块发生异常,可能会影响其他 模块甚至整个系统的执行流程与结果;

  (3)程序的代码上造成冗余,模块与模块需要进行强通信以及数据的交互,出现问题时难以定位与维护。耦合度过高!

  因此需要进行优化,将强关联的业务模块解耦以及某些模块之间实行异步通信!

  下面我将通过一些实际案例来进行一个介绍

  一、异步记录用户操作日志

  用户操作日志对于每一个系统来说是不可或缺的,并且操作日志应该单独抽取为一个模块业务,不应该与主业务系统之间耦合在一起。

  故而我们需要将其单独抽出并以异步的方式与主模块进行异步通信交互数据。

  要求:采用 RabbitMQ 的 DirectExchange+RoutingKey 消息模型来实现【异步记录用户操作日志】

  前提:SpringBoot与RabbitMQ的整合前面章节已经讲述过了,就不再重述了。

  第一步:创建消息模型:包括 Queue、Exchange、RoutingKey 等的建立

  package com.springboot.rabbitmq.example.demo1.config;

  import org.springframework.amqp.core.Binding;

  import org.springframework.amqp.core.BindingBuilder;

  import org.springframework.amqp.core.DirectExchange;

  import org.springframework.amqp.core.Queue;

  import org.springframework.context.annotation.Bean;

  import org.springframework.context.annotation.Configuration;

  /**

  *

  * Direct Exchange(路由模式)—异步记录用户操作日志

  * @author Mr yi

  * @time 2019年6月19日

  */

  @Configuration

  public class RabbitConfigDemo1 {

  //消息队列名称

  final static String queue = "queue_demo1";

  /**

  * 交换机名称

  */

  final static String exchange = "deom1Exchange";

  @Bean

  public Queue queueDemo1() {

  return new Queue(RabbitConfigDemo1.queue);

  }

  /**

  *

  * @method 声明一个direct类型的交换机

  * @author Mr yi

  * @time 2019年6月19日

  * @return

  */

  @Bean

  DirectExchange exchangeDemo1() {

  return new DirectExchange(RabbitConfigDemo1.exchange);

  }

  /**

  *

  * @method 绑定Queue队列到交换机,并且指定routingKey

  * @author Mr yi

  * @time 2019年6月19日

  * @param queueDemo1 对应注入queueDemo1()方法

  * @param exchangeDemo1 对应exchangeDemo1()

  * @return

  */

  @Bean

  Binding bindingDirectExchangeDemo1(Queue queueDemo1, DirectExchange exchangeDemo1) {

  return BindingBuilder.bind(queueDemo1).to(exchangeDemo1).with("keyDemo1");

  }

  }

  第二步:创建生产者,这里的生产者负责发送用户日志信息到rabbitmq

  其中需要建立User 用户实体类、UserLog用户日志实体类,这里就不介绍了。

  注意一点:rabbitmq消息队列可以接受任何形式的消息数据。

  package com.springboot.rabbitmq.example.demo1.producers;

  import org.springframework.amqp.core.AmqpTemplate;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.stereotype.Component;

  import com.springboot.rabbitmq.example.demo1.entity.UserLog;

  /**

  *

  * @method 生产者

  * @author Mr yi

  * @time 2019年6月19日

  */

  @Component

  public class ProducersDemo1 {

  @Autowired

  private AmqpTemplate amqpTemplate;

  /**

  *

  * @method 生产者发送消息,direct模式下需要传递一个routingKey

  * @author Mr yi

  * @time 2019年6月19日

  * @throws Exception

  */

  public void send(UserLog userLog) throws Exception {

  System.out.println("发送用户日志信息到rabbitmq: " + userLog.toString());

  this.amqpTemplate.convertAndSend("deom1Exchange", "keyDemo1", userLog);

  }

  }

  第三步:创建消费者,即接受到rabbitmq传过来的消息后进行操作(如持久化用户日志信息)

  我这里只提供了核心代码,如持久化操作模拟执行即可。

  package com.springboot.rabbitmq.example.demo1.consumers;

  import java.util.Date;

  import org.springframework.amqp.rabbit.annotation.RabbitHandler;

  import org.springframework.amqp.rabbit.annotation.RabbitListener;

  import org.springframework.stereotype.Component;

  import com.springboot.rabbitmq.example.demo1.entity.UserLog;

  /**

  *

  * @method 抽离日志系统为一个消费者

  * @author Mr yi

  * @time 2019年6月19日

  */

  @Component

  @RabbitListener(queues = "queue_demo1")

  public class ConsumersLogDemo1 {

  @RabbitHandler

  public void process(UserLog userLog) {

  System.out.println("log_name="+userLog.getLog_name());

  System.out.println("user_name="+userLog.getUser_name());

  System.out.println("log_type="+userLog.getLog_type());

  //将日志信息进行持久化操作(这里没有写操作数据库方法,模拟执行)

  //save(userLog);无锡人流多少钱 http://www.bhnfkyy.com/

  System.out.println("用户日志成功录入!");

  }

  }

  第四步:在 Controller 中执行用户登录逻辑

  我们这里模拟用户登录操作,用户登录后,会发送用户登录操作日志到rabbitmq队列中,由绑定了此队列的消费者(即用户日志模块系统)接受消息

  package com.springboot.rabbitmq.example.demo1.controller;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.stereotype.Controller;

  import org.springframework.web.bind.annotation.RequestMapping;

  import com.springboot.rabbitmq.example.demo1.entity.UserLog;

  import com.springboot.rabbitmq.example.demo1.producers.ProducersDemo1;

  import lombok.extern.slf4j.Slf4j;

  @Controller

  @Slf4j

  @RequestMapping("/userdemo1")

  public class UserController {

  @Autowired

  private ProducersDemo1 producers;

  @RequestMapping("/login")

  public String login() throws Exception {

  String user_name = "admin";

  String pwd = "123456";

  if(user_name=="admin" && pwd=="123456") {

  log.info("用户登录成功!");

  UserLog userLog = new UserLog();

  userLog.setLog_name("用户登录").setLog_type("login").setUser_name(user_name);

  producers.send(userLog);

  log.info("异步记录用户操作日志!");

  }

  return "success";

  }

  }

  控制台数据结果

  队列

  队列绑定的routing key

  exchange交换机

RabbitMQ实战场景(一):异步记录用户操作日志的更多相关文章

  1. springAOP记录用户操作日志

    项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用springAOP来完成比较合适. 注解工具类: @Retention(RetentionPolicy.RUNTIME) @Tar ...

  2. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  3. Spring AOP使用注解记录用户操作日志

    最后一个方法:核心的日志记录方法 package com.migu.cm.aspect; import com.alibaba.fastjson.JSON; import com.migu.cm.do ...

  4. linux 记录用户操作日志

    将以下加入到/etc/profile 最后 history USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]/ ...

  5. SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  6. mysql颠覆实战笔记(三)-- 用户登录(二):保存用户操作日志的方法

    版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...

  7. 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录

    在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...

  8. 基于SqlSugar的开发框架循序渐进介绍(8)-- 在基类函数封装实现用户操作日志记录

    在我们对数据进行重要修改调整的时候,往往需要跟踪记录好用户操作日志.一般来说,如对重要表记录的插入.修改.删除都需要记录下来,由于用户操作日志会带来一定的额外消耗,因此我们通过配置的方式来决定记录那些 ...

  9. [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理

    设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...

随机推荐

  1. 基于 Mathematica 的机器人仿真环境(机械臂篇)[转]

    完美的教程,没有之一,收藏学习. 目的 本文手把手教你在 Mathematica 软件中搭建机器人的仿真环境,具体包括以下内容(所使用的版本是 Mathematica 11.1,更早的版本可能缺少某些 ...

  2. linux系统备份恢复到本机或是别的机器上

    ubuntu系统备份还原到相同电脑.另一台电脑.(tar方式) 原创 laukal 发布于2018-07-28 15:35:14 阅读数 2205 收藏 展开 最近一直搞Xtion2,openni2, ...

  3. [LINUX] 快速回收连接

    i /etc/sysctl.conf 编辑文件,加入以下内容:net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_r ...

  4. 安装比特币区块链钱包API(Blockchain Wallet用户发送和接收比特币的简单API)

    区块链钱包API提供了一个简单的界面,商家可以用它以编程方式与钱包进行交互. 安装:要使用此API,您需要运行负责管理区块链钱包的小型本地服务. 您的应用程序通过HTTP API调用在本地与此服务进行 ...

  5. Docker打包部署前端项目与负载均衡

    设置淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org //在home/fn1 home/fn2放入项目和nginx ...

  6. 十三、postman导出java代码

    导出成java的OkHttp代码 使用Junit进行接口自动化测试 使用fastJSON解析json字符串 创建个实体类 package com.netease.AcFunTest; public c ...

  7. 命令mark

    for i in `sudo /usr/local/sbin/fping -g 10.181.37.0/26 -p 10 -r 1 | grep alive | awk '{print $1 }'`; ...

  8. 暚光科技定位系统数据解析-java

    暚光科技定位系统数据解析-java package com.ygkj.test; import java.io.DataInputStream; import java.io.IOException; ...

  9. 【LeetCode算法-28/35】Implement strStr()/Search Insert Position

    LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...

  10. 集群服务器+定时任务(Quartz) 重复执行的问题

    x StackExchange.Redis private readonly IDatabase _db; string key = string.Concat("{自己命名的Redis前缀 ...