消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)

一、异步处理

场景:

用户注册,写入数据库成功以后,发送邮件和短信。

准备工作:

1)安装RabbitMQ,参考前面的文章

2)新建一个名为RabbitMQAsyncProc的maven web工程,在pom.xml文件里面引入如下依赖

复制代码


  4.0.0
  com.study.demo
  RabbitMQAsyncProc
  war
  0.0.1-SNAPSHOT
  RabbitMQAsyncProc Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    

    
      javax
      javaee-web-api
      7.0
      provided
    

    
      org.springframework
      spring-webmvc
      4.3.11.RELEASE
    

    
      javax.servlet
      jstl
      1.2
    

    
    
      org.slf4j
      slf4j-api
      1.7.5
    

    
      ch.qos.logback
      logback-classic
      1.0.13
    
    
      ch.qos.logback
      logback-core
      1.0.13
    
    
      ch.qos.logback
      logback-access
      1.0.13
    

    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    
    
      org.codehaus.jackson
      jackson-core-asl
      1.9.13
    

    
    
      com.rabbitmq
      amqp-client
      5.0.0
    
    
      org.springframework.amqp
      spring-rabbit
      2.0.0.RELEASE
    

      
      
          org.aspectj
          aspectjrt
          1.6.12
      
      
      
          org.aspectj
          aspectjweaver
          1.6.12
      
  
  
    RabbitMQAsyncProc
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.8
                    1.8
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.6
                
                    false
                
            

        
    
      
        ${basedir}/src/main/java
        
          **/*.xml
        
      
    
  

复制代码

1. 新建一个用户信息实体

复制代码

package com.study.demo.vo;

import java.util.UUID;

/**
 * 
 * @Description: 用户信息实体
 * @author liguangsheng
 * @date 2018年9月18日
 *
 */
public class User {

    private final String userId;
    private final String userName;
    private final String email;
    private final String phoneNumber;

    public User(String userId, String userName, String email, String phoneNumber) {
        this.userId = userId;
        this.userName = userName;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

    public String getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }

    public String getEmail() {
        return email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public static User makeUser(String userName,String email,String phoneNumber){
        String userId = UUID.randomUUID().toString();
        return new User(userId,userName,email,phoneNumber);
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                ", email='" + email + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                '}';
    }
}

复制代码

2. 新建一个用户注册接口

复制代码

package com.study.demo.service;

import com.study.demo.vo.User;

/**
 * 
 * @Description: 用户注册接口
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
public interface IUserReg {

    public boolean userRegister(User user);

}

复制代码

3. 新建三个业务类

1)保存用户数据到数据库

复制代码

package com.study.demo.service.busi;

import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Service;

import com.study.demo.vo.User;

/**
 * 
 * @Description: 保存用户数据到数据库
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Service
public class SaveUser {

    private ConcurrentHashMap userData =
            new ConcurrentHashMap();

    public void saveUser(User user){
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        userData.putIfAbsent(user.getUserId(),user);
    }

    public User getUser(String userId){
        return userData.get(userId);
    }


}

复制代码

2)发送邮件的服务

复制代码

package com.study.demo.service.busi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: 发送邮件的服务
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Service
public class SendEmail {
    private Logger logger = LoggerFactory.getLogger(SendEmail.class);

    public void sendEmail(String email){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("-------------Already Send email to "+email);
    }

}

复制代码

3)发送短信的服务

复制代码

package com.study.demo.service.busi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: 发送短信的服务
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Service
public class SendSms {

    private Logger logger = LoggerFactory.getLogger(SendSms.class);

    public void sendSms(String phoneNumber){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("-------------Already Send Sms to "+phoneNumber);
    }

}

复制代码

4. 新建/RabbitMQAsyncProc/src/main/java/applicationContext.xml配置文件

复制代码





    
     
     
     
         
     

    
    
        
        
        
        
        
    
    

    
    
    
    

    
    
        
            
            
        
    

    
    
        
    


  

复制代码

5. 新建一个串行的用户注册实现类

复制代码

package com.study.demo.service.impl;

import com.study.demo.service.IUserReg;
import com.study.demo.service.busi.SaveUser;
import com.study.demo.service.busi.SendEmail;
import com.study.demo.service.busi.SendSms;
import com.study.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: 串行的用户注册
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Service
@Qualifier("serial")
public class SerialProcess implements IUserReg {

    @Autowired
    private SaveUser saveUser;
    @Autowired
    private SendEmail sendEmail;
    @Autowired
    private SendSms sendSms;

    public boolean userRegister(User user) {
        try {
            saveUser.saveUser(user);
            sendEmail.sendEmail(user.getEmail());
            sendSms.sendSms(user.getPhoneNumber());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

复制代码

6. 新建一个并行的用户注册实现类

复制代码

package com.study.demo.service.impl;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.study.demo.service.IUserReg;
import com.study.demo.service.busi.SaveUser;
import com.study.demo.service.busi.SendEmail;
import com.study.demo.service.busi.SendSms;
import com.study.demo.vo.User;

/**
 * 
 * @Description: 并行的用户注册
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Service
@Qualifier("para")
public class ParalllerProcess implements IUserReg {

    private static Logger logger = LoggerFactory.getLogger(ParalllerProcess.class);

    @Autowired
    private SaveUser saveUser;
    @Autowired
    private SendEmail sendEmail;
    @Autowired
    private SendSms sendSms;

    //发送邮件的线程
    private static class SendEmailThread implements Callable{

        private SendEmail sendEmail;
        private String email;

        public SendEmailThread(SendEmail sendEmail, String email) {
            this.sendEmail = sendEmail;
            this.email = email;
        }

        public Boolean call() throws Exception {
            sendEmail.sendEmail(email);
            logger.info("SendEmailThread send mail to"+email);
            return true;
        }
    }

    //发送短信的线程
    private static class SendSmsThread implements Callable{

        private SendSms sendSms;
        private String phoneNumber;

        public SendSmsThread(SendSms sendSms, String phoneNumber) {
            this.sendSms = sendSms;
            this.phoneNumber = phoneNumber;
        }

        public Boolean call() throws Exception {
            sendSms.sendSms(phoneNumber);
            logger.info("SendSmsThread send mail to"+phoneNumber);
            return true;
        }
    }

    public boolean userRegister(User user) {
        FutureTask sendEmailFuture =
                new FutureTask(new SendEmailThread(sendEmail,user.getEmail()));
        FutureTask sendSmsFuture =
                new FutureTask(new SendSmsThread(sendSms,user.getPhoneNumber()));
        try {
            saveUser.saveUser(user);
            new Thread(sendEmailFuture).start();
            new Thread(sendSmsFuture).start();
            sendEmailFuture.get();//获取邮件发送的结果
            sendSmsFuture.get();//获取短信发送的结果
            return true;
        } catch (Exception e) {
            logger.error(e.toString());
            return  false;
        }

    }
}

复制代码

7. 新建一个RabbitMQ实现的异步用户注册

复制代码

package com.study.demo.service.impl;

import com.study.demo.service.IUserReg;
import com.study.demo.service.busi.SaveUser;
import com.study.demo.vo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


/**
 * 
 * @Description: RabbitMQ实现的异步用户注册
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Service
@Qualifier("async")
public class AsyncProcess implements IUserReg{

    private Logger logger = LoggerFactory.getLogger(AsyncProcess.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SaveUser saveUser;


    public boolean userRegister(User user) {
        try {
            saveUser.saveUser(user);
            rabbitTemplate.send("user-reg-exchange","email",
                    new Message(user.getEmail().getBytes(),new MessageProperties()));
            rabbitTemplate.send("user-reg-exchange","sms",
                    new Message(user.getEmail().getBytes(),new MessageProperties()));
        } catch (AmqpException e) {
            logger.error(e.toString());
            return  false;
        }

        return true;
    }
}

复制代码

8.新建一个RabbitMQ消息消费端监听邮件消息类

复制代码

package com.study.demo.service.mq;

import com.study.demo.service.busi.SendEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 
 * @Description: RabbitMQ消息消费端监听邮件消息类
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Component
public class ProcessUserEmail implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(ProcessUserEmail.class);

    @Autowired
    private SendEmail sendEmail;

    public void onMessage(Message message) {
        logger.info("accept message,ready process......");
        sendEmail.sendEmail(new String(message.getBody()));

    }
}

复制代码

 在/RabbitMQAsyncProc/src/main/java/applicationContext.xml添加邮件消息监听类配置:

    
        
    

9. 新建一个RabbitMQ消息消费端监听sms消息类

复制代码

package com.study.demo.service.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.study.demo.service.busi.SendSms;

/**
 * 
 * @Description: RabbitMQ消息消费端监听sms消息类
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Component
public class ProcessUserSms implements MessageListener {

    private Logger logger = LoggerFactory.getLogger(ProcessUserSms.class);

    @Autowired
    private SendSms sendSms;

    public void onMessage(Message message) {
        logger.info("accept message,ready process......");
        sendSms.sendSms(new String(message.getBody()));

    }
}

复制代码

  在/RabbitMQAsyncProc/src/main/java/applicationContext.xml添加短信消息监听类配置:

    
        
        
    

10. 新建一个统计花费时间工具类

复制代码

package com.study.demo.tools;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.study.demo.vo.User;

/**
 * 
 * @Description: 统计花费时间工具类
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Aspect
@Service
public class StatTime {

    private Logger logger = LoggerFactory.getLogger(StatTime.class);
    private User user;

    public StatTime() {
        logger.info("************Aop开启");
    }

    @Pointcut("execution(* com.study.demo.service.impl.*.*Register(..))")
    public void stat(){}

    @Around("stat()&&args(user)")
    public Object statTime(ProceedingJoinPoint proceedingJoinPoint,User user){
        this.user = user;
        long start = System.currentTimeMillis();
        Object result = null;
        try {
            result = proceedingJoinPoint.proceed(new Object[]{this.user});
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("************spend time : "+(System.currentTimeMillis()-start)+"ms");
        return result;

    }

}

复制代码

11. 新建一个用户注册控制器

复制代码

package com.study.demo.controller;

import com.study.demo.service.IUserReg;
import com.study.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @Description: 用户注册控制器
 * @author leeSmall
 * @date 2018年9月18日
 *
 */
@Controller
public class UserRegController {

    private static final String SUCCESS = "suc";
    private static final String FAILUER = "failure";

    @Autowired
    @Qualifier("serial")
    private  IUserReg userReg;

    @RequestMapping("/userReg")
    public String userReg(){
        return "index";
    }

    @RequestMapping("/saveUser")
    @ResponseBody
    public String saveUser(@RequestParam("userName")String userName,
                           @RequestParam("email")String email,
                           @RequestParam("phoneNumber")String phoneNumber){
        try {
            if (userReg.userRegister(User.makeUser(userName,email,phoneNumber)))
                return SUCCESS;
            else
                return FAILUER;
        } catch (Exception e) {
            return FAILUER;
        }
    }


}

复制代码

12. 新建/RabbitMQAsyncProc/src/main/java/spring-mvc.xml配置文件

复制代码

  




    
    

    

    


    
        
            
                
                    
                    
                    
                
            
        
    
    

    
        
            
                
                
            
        
    

    
        
            
                
                
                
                
                
            
        
        
    

    
        
        

        
            
                
                
                    
                    
                    
                
            
        

        
            
                
                    
                
            
        
    
    
  

复制代码

13. 新建一个/RabbitMQAsyncProc/src/main/webapp/WEB-INF/views/index.jsp页面

复制代码

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    System.out.println(path);
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    System.out.println(basePath);
%>




    

    用户注册-异步模式

    
    
    
    
    
    



用户注册-异步模式

用户姓名:
用户邮件:
用户手机:

复制代码

14. 新建/RabbitMQAsyncProc/src/main/webapp/WEB-INF/web.xml配置文件

复制代码


  RabbitMqSpringProducerDemo

  
    default
    *.js
  

  
  
    characterEncoding
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    characterEncoding
    /*
  
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  


  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
  
  
    SpringMVC
    
    /
  
  

复制代码

到此代码别写完成!

15. 在Tomcat v8.5 8080里面启动RabbitMQAsyncProc

首次启动时用户注册接口时串行的

    @Autowired
    @Qualifier("serial")
    private  IUserReg userReg;

在浏览器输入地址http://localhost:8080/RabbitMQAsyncProc/userReg访问进行用户注册

消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第1张图片

 

 查看控制台状态:

 修改用户注册接口为并行的:

    @Autowired
    @Qualifier("para")
    private  IUserReg userReg;

 在用户界面注册 查看控制台状态:

 消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第2张图片

消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第3张图片

 修改用户注册接口为RabbitMQ异步实现的的:

    @Autowired
    @Qualifier("async")
    private  IUserReg userReg;

  在用户界面注册 查看控制台状态:

 消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第4张图片

 

 消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第5张图片

总结:

串行模式  ************spend time : 251ms

并行模式  ************spend time : 153ms

消息队列模式:************spend time : 59ms

所以RabbitMQ异步处理的性能最快

二、应用解耦

场景:

用户下订单买商品,订单成功了,去扣减库存,库存必须扣减完成,没有库存,库存低于某个阈值,可以扣减成功,要通知其他系统(如采购系统尽快采购,用户订单系统我们尽快调货)

RPC实现。库存系统失败,订单系统也无法成功,订单系统和库存系统耦合了。所以要缓存消息中间件来解耦。发送一个扣减库存的消息,保证消息必须被库存系统处理。

三个问题要解决:

1)订单系统发给MQ服务器的消息,必须被MO服务器接收到(事物、发送者确认)

2)MQ服务器拿到消息以后,消息被正常处理以前必须保存住(持久化)

3)某个库存服务出现了异常,消息要能够被其他库存系统处理(消费者确认,消息监听类要实现ChannelAwareMessageListener)

 

订单系统一定要知道库存系统是否处理成功怎么办?

库存系统和订单系统之间建立一个消息通道,库存系统去通知订单系统

准备工作:

1)安装RabbitMQ,参考前面的文章

2)分别新建名为RabbitMQOrder和RabbitMQDepot的两个maven web工程,在pom,xml文件里面引入一些依赖

复制代码


    4.0.0
    com.study.demo
    RabbitMQOrder
    war
    0.0.1-SNAPSHOT
    RabbitMQOrder Maven Webapp
    http://maven.apache.org
    
        
            junit
            junit
            3.8.1
            test
        

        
            javax
            javaee-web-api
            7.0
            provided
        

        
            org.springframework
            spring-webmvc
            4.3.11.RELEASE
        

        
            javax.servlet
            jstl
            1.2
        

        
        
            org.slf4j
            slf4j-api
            1.7.5
        
        
            log4j
            log4j
            1.2.16
        
        
            org.slf4j
            jcl-over-slf4j
            1.7.5
        
        
            ch.qos.logback
            logback-classic
            1.0.13
        
        
            ch.qos.logback
            logback-core
            1.0.13
        
        
            ch.qos.logback
            logback-access
            1.0.13
        

        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            org.codehaus.jackson
            jackson-core-asl
            1.9.13
        

        
            com.google.code.gson
            gson
            2.8.2
        

        
        
            com.rabbitmq
            amqp-client
            5.0.0
        
        
            org.springframework.amqp
            spring-rabbit
            2.0.0.RELEASE
        

        
        
            org.aspectj
            aspectjrt
            1.6.12
        
        
        
            org.aspectj
            aspectjweaver
            1.6.12
        
    
    
        RabbitMQOrder
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.8
                    1.8
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.6
                
                    false
                
            

        
        
            
                ${basedir}/src/main/java
                
                    **/*.xml
                
            
        
    

复制代码

 编写订单系统RabbitMQOrder代码:

1. 在工程RabbitMQOrder新建一个商品实体

复制代码

package com.study.demo.vo;

import java.io.Serializable;

/**
 * 
 * @Description: 商品实体
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
public class GoodTransferVo  implements Serializable {

    
    private static final long serialVersionUID = 7702481109435751937L;
    
    /**
     * 商品id
     */
    private String goodsId;
    
    /**
     * 改变的库存量
     */
    private int changeAmount;
    
    /**
     * 入库或者出库
     */
    private boolean inOrOut;

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public int getChangeAmount() {
        return changeAmount;
    }

    public void setChangeAmount(int changeAmount) {
        this.changeAmount = changeAmount;
    }

    public boolean isInOrOut() {
        return inOrOut;
    }

    public void setInOrOut(boolean inOrOut) {
        this.inOrOut = inOrOut;
    }
}

复制代码

2. 在工程RabbitMQOrder新建一个处理库存接口

复制代码

package com.study.demo.service;

/**
 * 
 * @Description: 处理库存接口
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
public interface IProDepot {

    public void processDepot(String goodsId,int amount);

}

复制代码

3. 在工程RabbitMQOrder新建一个/RabbitMQOrder/src/main/java/applicationContext.xml配置文件,配置RabbitMQ

复制代码





     
     
         
     

    
    
        
        
        
        
        
    
    

    

    
    
        
            
        
    

    
    
        
    


复制代码

4. 在工程RabbitMQOrder新建一个RabbitMQ处理库存接口的实现类

复制代码

package com.study.demo.service;

import com.study.demo.vo.GoodTransferVo;
import com.google.gson.Gson;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: RabbitMQ处理库存接口的实现类
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
@Qualifier("mq")
public class MqMode  implements IProDepot {

    private final static String DEPOT_RK = "amount.depot";
    private final static String DEPOT_EXCHANGE = "depot-amount-exchange";

    @Autowired
    RabbitTemplate rabbitTemplate;

    private static Gson gson = new Gson();

    public void processDepot(String goodsId, int amount) {
        GoodTransferVo goodTransferVo = new GoodTransferVo();
        goodTransferVo.setGoodsId(goodsId);
        goodTransferVo.setChangeAmount(amount);
        goodTransferVo.setInOrOut(false);
        String goods = gson.toJson(goodTransferVo);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        rabbitTemplate.send(DEPOT_EXCHANGE, DEPOT_RK,
                new Message(goods.getBytes(), messageProperties));
    }
}

复制代码

5. 在工程RabbitMQOrder新建一个处理订单业务类

复制代码

package com.study.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: 处理订单业务类
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
public class ProcessOrder {
    private Logger logger = LoggerFactory.getLogger(ProcessOrder.class);

    @Autowired
    @Qualifier("mq")
    private IProDepot proDepot;

    public void processOrder(String goodsId,int amount){
        try {
            Thread.sleep(80);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("--------------------["+goodsId+"]订单入库完成,准备变动库存!");
        proDepot.processDepot(goodsId,amount);

    }

}

复制代码

6. 在工程RabbitMQOrder新建一个RabbitMQ发送者确认

复制代码

package com.study.demo.service.callback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: RabbitMQ发送者确认
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
public class ConfirmCallback implements RabbitTemplate.ConfirmCallback {
    private Logger logger = LoggerFactory.getLogger(ConfirmCallback.class);

    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            logger.info("消息确认发送给mq成功");
        } else {
            //处理失败的消息
            logger.info("消息发送给mq失败,考虑重发:"+cause);
        }
    }
}

复制代码

7. 在工程RabbitMQOrder新建一个RabbitMQ返回确认

复制代码

package com.study.demo.service.callback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: RabbitMQ返回确认-RabbitMQ服务内部出错时回调
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
public class SendReturnCallback implements RabbitTemplate.ReturnCallback {

    private Logger logger = LoggerFactory.getLogger(SendReturnCallback.class);

    public void returnedMessage(Message message, int replyCode,
                                String replyText, String exchange,
                                String routingKey) {
        logger.info("Returned replyText:"+replyText);
        logger.info("Returned exchange:"+exchange);
        logger.info("Returned routingKey:"+routingKey);
        String msgJson  = new String(message.getBody());
        logger.info("Returned Message:"+msgJson);
    }
}

复制代码

8. 在/RabbitMQOrder/src/main/java/applicationContext.xml配置文件新增RabbitMQ发送者确认和RabbitMQ返回确认配置

复制代码





     
     
         
     

    
    
        
        
        
        
        
        
        
    
    

    

    
    
        
            
        
    

    
    
        
        
        
        
    


复制代码

9. 在工程RabbitMQOrder新建一个订单业务控制器

复制代码

package com.study.demo.controller;

import com.study.demo.service.ProcessOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @Description: 订单业务控制器
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Controller
public class OrderController {

    private Logger logger = LoggerFactory.getLogger(OrderController.class);
    private static final String SUCCESS = "suc";
    private static final String FAILUER = "failure";

    @Autowired
    private ProcessOrder processOrder;

    @RequestMapping("/order")
    public String order(){
        return "index";
    }

    @RequestMapping("/confirmOrder")
    @ResponseBody
    public String confirmOrder(@RequestParam("goodsId")String goodsId,
                           @RequestParam("amount")int amount){
        try {
            processOrder.processOrder(goodsId,amount);
            return SUCCESS;
        } catch (Exception e) {
            logger.error("订单确认异常!",e);
            return FAILUER;
        }
    }


}

复制代码

10. 新增/RabbitMQOrder/src/main/java/spring-mvc.xml配置文件

复制代码

  




    
    

    

    


    
        
            
                
                    
                    
                    
                
            
        
    
    

    
        
            
                
                
            
        
    

    
        
            
                
                
                
                
                
            
        
        
    

    
        
        

        
            
                
                
                    
                    
                    
                
            
        

        
            
                
                    
                
            
        
    
    
  

复制代码

11. 新增/RabbitMQOrder/src/main/webapp/WEB-INF/views/index.jsp页面模拟下订单

复制代码

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    System.out.println(path);
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    System.out.println(basePath);
%>




    

    订单提交

    
    
    
    
    
    



确认提交订单

商品编号:
订单数量:

复制代码

12.新增 /RabbitMQOrder/src/main/webapp/WEB-INF/web.xml配置文件

复制代码


  RabbitMQOrder

  
    default
    *.js
  

  
  
    characterEncoding
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    characterEncoding
    /*
  
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  


  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
  
  
    SpringMVC
    
    /
  
  

复制代码

到此订单系统代码编写完成!

13. 在Tomcat v8.5 8080里面启动RabbitMQOrder,输入地址http://localhost:8080/RabbitMQOrder/order访问

消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第6张图片

 

 

编写库存系统RabbitMQDepot代码:

1. 在工程RabbitMQDepot 新建一个商品实体

复制代码

package com.study.demo.vo;

import java.io.Serializable;

/**
 * 
 * @Description: 商品实体
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
public class GoodTransferVo  implements Serializable {

    
    private static final long serialVersionUID = 7702481109435751937L;
    
    /**
     * 商品id
     */
    private String goodsId;
    
    /**
     * 改变的库存量
     */
    private int changeAmount;
    
    /**
     * 入库或者出库
     */
    private boolean inOrOut;

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public int getChangeAmount() {
        return changeAmount;
    }

    public void setChangeAmount(int changeAmount) {
        this.changeAmount = changeAmount;
    }

    public boolean isInOrOut() {
        return inOrOut;
    }

    public void setInOrOut(boolean inOrOut) {
        this.inOrOut = inOrOut;
    }
}

复制代码

2. 在工程RabbitMQDepot 新建一个库存数据服务

复制代码

package com.study.demo.service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: 库存数据服务
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
public class Depot {

    private static Logger logger = LoggerFactory.getLogger(Depot.class);

    private ConcurrentHashMap goodsData =
            new ConcurrentHashMap();

    @PostConstruct
    public void initDepot(){
        goodsData.put("001",1000);
        goodsData.put("002",500);
        goodsData.put("003",600);
        goodsData.put("004",700);
    }


    //增加库存
    public void inDepot(String goodsId,int addAmout){
        logger.info("+++++++++++++++++增加商品:"+goodsId+"库存,数量为:"+addAmout);
        int newValue = goodsData.compute(goodsId, new BiFunction() {
            public Integer apply(String s, Integer integer) {
                return integer == null ? addAmout : integer + addAmout;
            }
        });
        logger.info("+++++++++++++++++商品:"+goodsId+"库存,数量变为:"+newValue);
    }

    //减少库存
    public void outDepot(String goodsId,int reduceAmout){
        logger.info("-------------------减少商品:"+goodsId+"库存,数量为:"+reduceAmout);
        int newValue = goodsData.compute(goodsId, new BiFunction() {
            public Integer apply(String s, Integer integer) {
                return integer == null ? 0 : integer - reduceAmout;
            }
        });
        logger.info("-------------------商品:"+goodsId+"库存,数量变为:"+newValue);
    }

    public int getGoodsAmount(String goodsId){
        return goodsData.get(goodsId);
    }
}

复制代码

3. 在工程RabbitMQDepot 新建一个库存服务管理

复制代码

package com.study.demo.service;

import com.study.demo.vo.GoodTransferVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * @Description: 库存服务管理
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
public class DepotManager {

    @Autowired
    private Depot depot;

    public void operDepot(GoodTransferVo goodTransferVo){
        if(goodTransferVo.isInOrOut()){
            depot.inDepot(goodTransferVo.getGoodsId(),goodTransferVo.getChangeAmount());
        }else{
            depot.outDepot(goodTransferVo.getGoodsId(),goodTransferVo.getChangeAmount());
        }
    }



}

复制代码

4. 新建/RabbitMQDepot/src/main/java/applicationContext.xml配置文件,配置RabbitMQ

复制代码





     
     
         
     

    
    
        
        
        
        
        
    
    

    
    

    
    
        
            
        
    

    
    
   
    

  

复制代码

5. 在工程RabbitMQDepot 新建一个消息机制处理库存类

复制代码

package com.study.demo.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.study.demo.service.DepotManager;
import com.study.demo.vo.GoodTransferVo;

/**
 * 
 * @Description: 消息机制处理库存
 * @author leeSmall
 * @date 2018年9月19日
 *
 */
@Service
public class ProcessDepot  implements ChannelAwareMessageListener {

    private static Logger logger = LoggerFactory.getLogger(ProcessDepot.class);

    @Autowired
    private DepotManager depotManager;

    private static Gson gson = new Gson();

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            String msg = new String(message.getBody());
            logger.info(">>>>>>>>>>>>>>接收到消息:"+msg);
            GoodTransferVo goodTransferVo = gson.fromJson(msg,GoodTransferVo.class);
            try {
                depotManager.operDepot(goodTransferVo);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),
                        false);
                logger.info(">>>>>>>>>>>>>>库存处理完成,应答Mq服务");
            } catch (Exception e) {
                logger.error(e.getMessage());
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                        false,true);
                logger.info(">>>>>>>>>>>>>>库存处理失败,拒绝消息,要求Mq重新派发");
                throw e;
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }


}

复制代码

6. 在/RabbitMQDepot/src/main/java/applicationContext.xml配置消息机制处理库存

复制代码





     
     
         
     

    
    
        
        
        
        
        
    
    

    
    

    
    
        
            
        
    

    
    
        
    

  

复制代码

7. 新增/RabbitMQDepot/src/main/java/spring-mvc.xml配置文件

复制代码

  




    
    

    

    


    
        
            
                
                    
                    
                    
                
            
        
    
    

    
        
            
                
                
            
        
    

    
        
            
                
                
                
                
                
            
        
        
    

    
        
        

        
            
                
                
                    
                    
                    
                
            
        

        
            
                
                    
                
            
        
    
    
  

复制代码

8. 新增/RabbitMQDepot/src/main/webapp/WEB-INF/web.xml配置文件

复制代码


  RabbitMQDepot

  
    default
    *.js
  

  
  
    characterEncoding
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    characterEncoding
    /*
  
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  


  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
  
  
    SpringMVC
    
    /
  
  

复制代码

到此库存系统代码编写完成!

9. 在Tomcat v8.5 8081启动RabbitMQDepot

消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第7张图片

10. 在下订单页面模拟下订单,查看控制台状态

 消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第8张图片

 

 

 

 库存系统状态:

消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第9张图片

 

 订单系统状态:

消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第10张图片

 

 消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)_第11张图片

示例代码获取地址  https://github.com/leeSmall/MessageMiddleware

你可能感兴趣的:(消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦))