开发工具:IDEA,虚拟机 VMware Workstation
预备工作:安装好zookeeper的虚拟机,电脑jdk更换为1.7,本地tomcat启动,能够访问以下页面即可进行开发
2.建立以下springboot工程,关系图如下
1.首先打包父工程wk-parent,把需要的公共依赖导入,供其他项目依赖
2.打包pojo项目wk-pojo,实体类全部实现序列化
3.以上两个都打jar包即可,接下来整合接口项目wk-interface,只是单纯的接口即可,供提供者业务层实现
4.开发提供者wk-dubbo
a.service实现类代码
@Service(version="1.0.0")public class UserService implements IUserService { @Autowired private UserDao userDao; @Override public User loginAdmin(User user) { return userDao.loginAdmin(user); } @Override public WitkeyUser login(WitkeyUser witkeyUser) { return userDao.login(witkeyUser); }}b.application.properties
server.port=8013mybatis.mapper-locations=classpath:mapping/*.xmlmybatis.configuration.map-underscore-to-camel-case=truespring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/weike?characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=root# 应用名称spring.dubbo.application.name=provider#注册中心地址spring.dubbo.registry.address=zookeeper://192.168.17.130:2181#协议名称spring.dubbo.protocol.name=dubbo#协议端口spring.dubbo.protocol.port=20880#服务类包目录spring.dubbo.scan=com.buba.weike.dubbo.wkdubbo.service.impl#打印mybatis日志信息logging.level.com.buba.weike.dubbo.wkdubbo.dao=debug
5.开发消费者,把接口打包,加入项目依赖中,并注入到消费者service层的实现类中
a.application.properties
## 避免和 server 工程端口冲突
server.port=8081
spring.mvc.view.prefix=/jsps/
spring.mvc.view.suffix=.jsp
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://192.168.17.130:2181
spring.dubbo.scan=com.buba.weike.duboo.wkmanager.service
b.UserConsumer 代码
@Componentpublic class UserConsumer implements IUserConsumer { @Reference(version="1.0.0") IUserService iUserService; public User loginAdmin(User user) { return iUserService.loginAdmin(user); }} c.controller代码
@Controller@RequestMapping("/user")public class userController { @Autowired private IUserConsumer userService; //登录 @ResponseBody @RequestMapping("/loginAdmin") public Map<String,String> loginAjax(User user, String code,HttpSession session){ Map<String,String> map=new HashMap<String,String>(); String codet=(String)session.getAttribute("verCode"); if(codet==null){ map.put("flag","false"); map.put("messages","请输入验证码!"); } if(codet!=null && codet.equalsIgnoreCase(code)){ map.put("flag","true"); map.put("messages","验证码正确!"); } if(codet!=null && !codet.equalsIgnoreCase(code)){ map.put("flag","false"); map.put("messages","验证码错误!"); } User us =userService.loginAdmin(user); if(us!=null){ session.setAttribute("user",us); map.put("flag","true"); map.put("messages","登录成功!"); }else{ map.put("flag","false"); map.put("messages","账户名或密码错误!"); } return map; } //生成图片 @RequestMapping("/authImage") public void authImage(HttpServletResponse response, HttpSession session) throws IOException { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); //生成随机字串 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); //存入会话session //删除以前的 session.removeAttribute("verCode"); session.setAttribute("verCode", verifyCode.toLowerCase()); //生成图片 int w = 100, h = 30; VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode); } }6.出现以下页面
提供者项目debug,sql语句信息
两个消费者wk-partol和wk-manager,这两个项目都是运行出现以下效果
总结:提供者提供所需接口,消费者去调用,开发分工更清晰,方便。
原文地址:https://www.cnblogs.com/wutongvip/p/10076977.html