開發工具:IDEA,虛擬機 VMware Workstationmysql
預備工做:安裝好zookeeper的虛擬機,電腦jdk更換爲1.7,本地tomcat啓動,可以訪問如下頁面便可進行開發spring
2.創建如下springboot工程,關係圖以下sql
1.首先打包父工程wk-parent,把須要的公共依賴導入,供其餘項目依賴tomcat
2.打包pojo項目wk-pojo,實體類所有實現序列化springboot
3.以上兩個都打jar包便可,接下來整合接口項目wk-interface,只是單純的接口便可,供提供者業務層實現session
4.開發提供者wk-dubbomybatis
a.service實現類代碼mvc
@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=8013
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/weike?characterEncoding=utf-8
spring.datasource.username=root
spring.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層的實現類中app
a.application.propertiesjsp
## 避免和 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 代碼
@Component
public 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,這兩個項目都是運行出現如下效果
總結:提供者提供所需接口,消費者去調用,開發分工更清晰,方便。