看過第一篇博客的應該都知道流程,雖然並非如出一轍,但大致是同樣的,如今來確認具體方案。
java
註冊:
mysql
一、用戶錄入信息提交到上游後臺
git
二、上游後臺接受信息,構建mq消息,發送消息,返回到tip頁面
github
三、tip頁面等待1秒執行驗證,驗證是否添加成功,若成功將註冊的用戶數據放入到redis緩存,並返回到登陸頁
redis
四、下游後臺接收mq消息,將數據持久化到mysql,並觸發同步消息隊列,發送同步數據到同步消息隊列後結束線程
sql
五、下游同步消息隊列監聽到消息,開始同步數據到mongo
緩存
登陸:
app
一、用戶錄入信息提交到上游後臺ide
二、拿着信息到redis查詢是否有登陸緩存,如有直接返回到main頁面,若無調用下游服務接口
ui
三、下游服務接口接收到調用,帶參到mongo查詢數據並返回結果集
四、驗證結果集是否存在數據,若存在返回main頁面,若不存在跳轉到註冊頁面
如下貼上整個流程的代碼
上游controller
@RequestMapping("UserController") @Controller public class UserController { @Resource private IUserService userService; @RequestMapping("toRegist.do") public ModelAndView toRegist(User user, Model model) { model.addAttribute("registUser", user); return new ModelAndView("regist"); } @RequestMapping("login.do") public ModelAndView login(User user, Model model) { User loginUser = this.userService.login(user); if (null != loginUser) { model.addAttribute("message", "登陸成功"); model.addAttribute("registUser", loginUser); return new ModelAndView("main"); } else { return new ModelAndView("login"); } } @RequestMapping("regist.do") public ModelAndView regist(User user, Model model) { this.userService.regist(user); model.addAttribute("message", "正在註冊,請稍等……"); model.addAttribute("registUser", user); return new ModelAndView("tip"); } @RequestMapping("toLogin.do") public ModelAndView toLogin(User user, Model model) { model.addAttribute("registUser", user); return new ModelAndView("login"); } @RequestMapping("validateRegist.do") @ResponseBody public AjaxRetuen validateRegist(User user) { if (this.userService.validateRegist(user)) { return new AjaxRetuen("註冊成功,即將返回登陸頁", "y"); } return new AjaxRetuen("註冊失敗,請從新註冊", "n"); } }
上游service
@Repository public class UserServiceImpl implements IUserService { @Resource private QueueMessageProducer<User> registQueueMessageProducer; @Reference private DUserService dUserService; @Override public void regist(User user) { PhoneNoticeInfo noticeInfo = new PhoneNoticeInfo(); noticeInfo.setNoticeContent("Hello Word"); noticeInfo.setNoticeTitle("hello Word"); noticeInfo.setReceiver("hello"); noticeInfo.setReceiverPhone("1111111"); this.registQueueMessageProducer.sendQueue(user); } @Override public boolean validateRegist(User user) { user = this.dUserService.validateRegist(user); if (user == null) { return false; } ICache cache = SccUtilFactory.instanceCache(); cache.addCacheData("login:" + user, new CacheData(user)); return true; } @Override public User login(User user) { ICache cache = SccUtilFactory.instanceCache(); if (cache.hasCacheData("login:" + user)) { return (User) cache.getCacheData("login:" + user); } else { user = this.dUserService.validateRegist(user); if (null != user) { cache.addCacheData("login:" + user, new CacheData(user)); return user; } } return null; } }
服務接口
public interface DUserService { User addUser(User user); User validateRegist(User user); }
下游服務接口實現
@Service public class DUserServiceImpl implements DUserService { @Resource private IUserdao userdao; @Resource private MongoTemplate mongoTemplate; @Override public User addUser(User user) { this.userdao.insertUser(user); return user; } @Override public User validateRegist(User user) { List<User> users = this.mongoTemplate.find( new Query(Criteria.where("uname").is(user.getUname()).and("password").is(user.getPassword())), User.class); if (null != users && users.size() > 0) { return users.get(0); } return null; } }
註冊消息處理類
public class RegistDeal extends DealJmsMessage { @Resource private DUserService userService; @Resource private QueueMessageProducer<User> syncUserQueueMessageProducer; @Override public void dealMessage(ObjectMessage objectMessage, MessageConverter messageConverter) throws MessageConversionException, JMSException { User user = (User) messageConverter.fromMessage(objectMessage); user = this.userService.addUser(user); this.syncUserQueueMessageProducer.sendQueue(user); } }
同步消息處理類
public class SyncUserDealJmsMessage extends DealJmsMessage { @Resource private MongoTemplate mongoTemplate; @Override public void dealMessage(ObjectMessage objectMessage, MessageConverter messageConverter) throws MessageConversionException, JMSException { User user = (User) messageConverter.fromMessage(objectMessage); this.mongoTemplate.save(user); } }
dao接口
public interface IUserdao { Integer insertUser(User user); User findUserByUnameAndPass(User user); }
dao mapper
<mapper namespace="sustainable.service.dao.IUserdao"> <insert id="insertUser" parameterType="sustainable.common.po.User" useGeneratedKeys="true" keyProperty="uid"> INSERT INTO tab_user (uname, password) VALUES (#{uname}, #{password}); </insert> </mapper>
詳細項目代碼請看github託管,本身fork下來看