消息中间件系列五:RabbitMQ的使用场景(异步处理、应用解耦)
一、异步处理
场景:
用户注册,写入数据库成功以后,发送邮件和短信。
准备工作:
1)安装RabbitMQ,参考前面的文章
2)新建一个名为RabbitMQAsyncProc的maven web工程,在pom.xml文件里面引入如下依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study.demo</groupId> <artifactId>RabbitMQAsyncProc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RabbitMQAsyncProc Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <!-- <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency>--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.0.13</version> </dependency> <!--JSON--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <!-- RabbitMQ --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--使用AspectJ方式注解需要相应的包--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.12</version> </dependency> <!--使用AspectJ方式注解需要相应的包--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.12</version> </dependency> </dependencies> <build> <finalName>RabbitMQAsyncProc</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
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<String,User> userData = new ConcurrentHashMap<String, User>(); 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配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <aop:aspectj-autoproxy /> <!-- 配置扫描路径 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--邮件相关队列--> <rabbit:queue name="email_queue" durable="false"/> <!--短信相关队列--> <rabbit:queue name="sms_queue" durable="false"/> <!--将队列和交换器通过路由键绑定--> <rabbit:direct-exchange name="user-reg-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="email_queue" key="email" ></rabbit:binding> <rabbit:binding queue="sms_queue" key="sms" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 创建rabbitTemplate 消息模板类 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="rabbitConnectionFactory"></constructor-arg> </bean> </beans>
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<Boolean>{ 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<Boolean>{ 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<Boolean> sendEmailFuture = new FutureTask<Boolean>(new SendEmailThread(sendEmail,user.getEmail())); FutureTask<Boolean> sendSmsFuture = new FutureTask<Boolean>(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添加邮件消息监听类配置:
<rabbit:listener-container connection-factory="rabbitConnectionFactory"> <rabbit:listener queues="email_queue" ref="processUserEmail" method="onMessage"/> </rabbit:listener-container>
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添加短信消息监听类配置:
<rabbit:listener-container connection-factory="rabbitConnectionFactory"> <rabbit:listener queues="email_queue" ref="processUserEmail" method="onMessage"/> <rabbit:listener queues="sms_queue" ref="processUserSms" method="onMessage"/> </rabbit:listener-container>
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配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <mvc:default-servlet-handler />--> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <context:component-scan base-package="com.study.demo.controller"> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />--> </context:component-scan> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="pdf" value="application/pdf" /> <entry key="xsl" value="application/vnd.ms-excel" /> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="defaultContentType" value="text/html" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"></property> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> </list> </property> </bean> </beans>
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); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>用户注册-异步模式</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <script type="text/javascript" src="<%--<%=basePath%>--%>resources/js/jquery-1.11.0.min.js"></script> <style type="text/css"> .h1 { margin: 0 auto; } #producer{ width: 48%; border: 1px solid blue; height: 80%; align:center; margin:0 auto; } body{ text-align :center; } div { text-align :center; } textarea{ width:80%; height:100px; border:1px solid gray; } button{ background-color: rgb(62, 156, 66); border: none; font-weight: bold; color: white; height:30px; } </style> <script type="text/javascript"> function send(){ $.ajax({ type: 'get', url:'<%=basePath%>saveUser?userName='+$("#userName").val() +'&email='+$("#userEmail").val()+'&phoneNumber='+$("#userNumber").val(), dataType:'text', success:function(data){ if(data=="suc"){ alert("注册成功!"); }else{ alert("注册失败!"); } }, error:function(data){ alert("注册错误!"); } }); } </script> </head> <body> <h1>用户注册-异步模式</h1> <div id="producer"> 用户姓名:<input type="text" id="userName"/> <br> 用户邮件:<input type="text" id="userEmail"/> <br> 用户手机:<input type="text" id="userNumber"/> <br> <button onclick="send()">注 册</button> <br> </div> </body> </html>
14. 新建/RabbitMQAsyncProc/src/main/webapp/WEB-INF/web.xml配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>RabbitMqSpringProducerDemo</display-name> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring 编码过滤器 start --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 编码过滤器 End --> <!-- Spring Application Context Listener Start --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring Application Context Listener End --> <!-- Spring MVC Config Start --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Filter all resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> </web-app>
到此代码别写完成!
15. 在Tomcat v8.5 8080里面启动RabbitMQAsyncProc
首次启动时用户注册接口时串行的
@Autowired @Qualifier("serial") private IUserReg userReg;
在浏览器输入地址http://localhost:8080/RabbitMQAsyncProc/userReg访问进行用户注册
查看控制台状态:
修改用户注册接口为并行的:
@Autowired @Qualifier("para") private IUserReg userReg;
在用户界面注册 查看控制台状态:
修改用户注册接口为RabbitMQ异步实现的的:
@Autowired @Qualifier("async") private IUserReg userReg;
在用户界面注册 查看控制台状态:
总结:
串行模式 ************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文件里面引入一些依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.study.demo</groupId> <artifactId>RabbitMQOrder</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RabbitMQOrder Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.0.13</version> </dependency> <!--JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> <!-- RabbitMQ --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--使用AspectJ方式注解需要相应的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.12</version> </dependency> <!--使用AspectJ方式注解需要相应的包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.12</version> </dependency> </dependencies> <build> <finalName>RabbitMQOrder</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
编写订单系统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
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置扫描路径 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <rabbit:queue name="depot_queue" durable="true"/> <!--队列通过路由键和交换器绑定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 创建rabbitTemplate 消息模板类 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="rabbitConnectionFactory"></constructor-arg> </bean> </beans>
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返回确认配置
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置扫描路径 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> <!-- 发布确认必须配置在CachingConnectionFactory上 --> <property name="publisherConfirms" value="true"/> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <rabbit:queue name="depot_queue" durable="true"/> <!--队列通过路由键和交换器绑定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 创建rabbitTemplate 消息模板类 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="rabbitConnectionFactory"></constructor-arg> <!--消息确认回调 --> <property name="confirmCallback" ref="confirmCallback"/> <property name="returnCallback" ref="sendReturnCallback"/> </bean> </beans>
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配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <mvc:default-servlet-handler />--> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <context:component-scan base-package="com.study.demo.controller"> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />--> </context:component-scan> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="pdf" value="application/pdf" /> <entry key="xsl" value="application/vnd.ms-excel" /> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="defaultContentType" value="text/html" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"></property> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> </list> </property> </bean> </beans>
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); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>订单提交</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <script type="text/javascript" src="<%--<%=basePath%>--%>resources/js/jquery-1.11.0.min.js"></script> <style type="text/css"> .h1 { margin: 0 auto; } #producer{ width: 48%; border: 1px solid blue; height: 80%; align:center; margin:0 auto; } body{ text-align :center; } div { text-align :center; } textarea{ width:80%; height:100px; border:1px solid gray; } button{ background-color: rgb(62, 156, 66); border: none; font-weight: bold; color: white; height:30px; } </style> <script type="text/javascript"> function send(){ $.ajax({ type: 'get', url:'<%=basePath%>confirmOrder?goodsId='+$("#goods").val() +'&amount='+$("#amount").val(), dataType:'text', success:function(data){ if(data=="suc"){ alert("提交成功!"); }else{ alert("提交失败!"); } }, error:function(data){ alert("提交错误!"); } }); } </script> </head> <body> <h1>确认提交订单</h1> <div id="producer"> 商品编号:<input type="text" id="goods"/> <br> 订单数量:<input type="text" id="amount"/> <br> <button onclick="send()">确 认</button> <br> </div> </body> </html>
12.新增 /RabbitMQOrder/src/main/webapp/WEB-INF/web.xml配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>RabbitMQOrder</display-name> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring 编码过滤器 start --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 编码过滤器 End --> <!-- Spring Application Context Listener Start --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring Application Context Listener End --> <!-- Spring MVC Config Start --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Filter all resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> </web-app>
到此订单系统代码编写完成!
13. 在Tomcat v8.5 8080里面启动RabbitMQOrder,输入地址http://localhost:8080/RabbitMQOrder/order访问
编写库存系统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<String,Integer> goodsData = new ConcurrentHashMap<String, Integer>(); @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<String, Integer, Integer>() { 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<String, Integer, Integer>() { 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
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置扫描路径 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--配置队列 --> <rabbit:queue name="depot_queue" durable="true"/> <!--队列和交换器绑定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 对消息要手动确认 --> <rabbit:listener-container connection-factory="rabbitConnectionFactory" acknowledge="manual"> </rabbit:listener-container> </beans>
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配置消息机制处理库存
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.0.xsd"> <!-- 配置扫描路径 --> <context:component-scan base-package="com.study.demo"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- rabbitMQ配置 --> <bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <constructor-arg value="127.0.0.1"/> <property name="username" value="guest"/> <property name="password" value="guest"/> <property name="channelCacheSize" value="8"/> <property name="port" value="5672"></property> </bean> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--配置队列 --> <rabbit:queue name="depot_queue" durable="true"/> <!--队列和交换器绑定 --> <rabbit:direct-exchange name="depot-amount-exchange" xmlns="http://www.springframework.org/schema/rabbit" durable="true"> <rabbit:bindings> <rabbit:binding queue="depot_queue" key="amount.depot" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 对消息要手动确认 --> <rabbit:listener-container connection-factory="rabbitConnectionFactory" acknowledge="manual"> <rabbit:listener queues="depot_queue" ref="processDepot" method="onMessage" /> </rabbit:listener-container> </beans>
7. 新增/RabbitMQDepot/src/main/java/spring-mvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- <mvc:default-servlet-handler />--> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> <context:component-scan base-package="com.study.demo.controller"> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />--> </context:component-scan> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="stringHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="pdf" value="application/pdf" /> <entry key="xsl" value="application/vnd.ms-excel" /> <entry key="xml" value="application/xml" /> <entry key="json" value="application/json" /> </map> </property> <property name="defaultContentType" value="text/html" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"></property> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> </list> </property> </bean> </beans>
8. 新增/RabbitMQDepot/src/main/webapp/WEB-INF/web.xml配置文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>RabbitMQDepot</display-name> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- Spring 编码过滤器 start --> <filter> <filter-name>characterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 编码过滤器 End --> <!-- Spring Application Context Listener Start --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring Application Context Listener End --> <!-- Spring MVC Config Start --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- Filter all resources --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> </web-app>
到此库存系统代码编写完成!
9. 在Tomcat v8.5 8081启动RabbitMQDepot
10. 在下订单页面模拟下订单,查看控制台状态
库存系统状态:
订单系统状态: