看过第一篇博客的应该都知道流程,虽然并不是一模一样,但大体是一样的,现在来确认具体方案。
注册:
1、用户录入信息提交到上游后台
2、上游后台接受信息,构建mq消息,发送消息,返回到tip页面
3、tip页面等待1秒执行验证,验证是否添加成功,若成功将注册的用户数据放入到redis缓存,并返回到登录页
4、下游后台接收mq消息,将数据持久化到mysql,并触发同步消息队列,发送同步数据到同步消息队列后结束线程
5、下游同步消息队列监听到消息,开始同步数据到mongo
登录:
1、用户录入信息提交到上游后台
2、拿着信息到redis查询是否有登录缓存,若有直接返回到main页面,若无调用下游服务接口
3、下游服务接口接收到调用,带参到mongo查询数据并返回结果集
4、验证结果集是否存在数据,若存在返回main页面,若不存在跳转到注册页面
以下贴上整个流程的代码
上游controller
@RequestMapping("UserController")@Controllerpublic 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
@Repositorypublic class UserServiceImpl implements IUserService { @Resource private QueueMessageProducerregistQueueMessageProducer; @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);}
下游服务接口实现
@Servicepublic 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) { Listusers = 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 QueueMessageProducersyncUserQueueMessageProducer; @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
INSERT INTO tab_user (uname, password) VALUES (#{uname}, #{password});
详细项目代码请看github托管,自己fork下来看