/**
* UsersMapper接口文件
* @author dengp
*
*/
public interface UsersMapper {
void insertUsers(User user);
}
<mapper namespace="com.bobo.mapper.UsersMapper">
<insert id="insertUsers" parameterType="com.bobo.pojo.User">
insert into users(username,userage)values(#{username},#{userage})
insert>
mapper>
package com.bobo.dubbo.service;
import com.bobo.pojo.User;
/**
* 将用户的CRUD四个操作我们拆成了4个服务,便于演示dubbo
* 实际开发过程中不用
* @author dengp
*
*/
public interface AddUserDubboService {
void addUser(User user);
}
package com.bobo.dubbo.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.bobo.dubbo.service.AddUserDubboService;
import com.bobo.mapper.UsersMapper;
import com.bobo.pojo.User;
@Service
public class AddUserDubboServiceImpl implements AddUserDubboService {
@Resource
private UsersMapper userMapper;
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
userMapper.insertUsers(user);
}
}
注意我们在服务端添加了服务,我们需要在Dubbo中注册该服务。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<import resource="../../applicationContext-dao.xml" />
<import resource="../../applicationContext-service.xml" />
<import resource="../../applicationContext-trans.xml" />
<dubbo:application name="user-provider" />
<dubbo:registry protocol="zookeeper"
address="node01:2181,node02:2181,node03:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.bobo.dubbo.service.AddUserDubboService"
ref="addUserDubboServiceImpl"/>
beans>
服务端测试
连接成功
/**
* 用户管理的业务接口
* @author dengp
*
*/
public interface UserService {
void addUser(User user);
}
@Service
public class UserServiceImpl implements UserService {
@Resource
private AddUserDubboService addUserService;
@Override
public void addUser(User user) {
// TODO Auto-generated method stub
addUserService.addUser(user);
}
}
UserController
package com.bobo.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bobo.pojo.User;
import com.bobo.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/addUser")
public String addUser(User user){
userService.addUser(user);
return "success";
}
}
Consumer需要指定从dubbo中获取什么服务
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer"/>
<dubbo:registry protocol="zookeeper"
address="node01:2181,node02:2181,node03:2181" />
<dubbo:reference id="addUserDubboService"
interface="com.bobo.dubbo.service.AddUserDubboService"/>
beans>
addUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="/user/addUser" method="post">
用户名:<input type="text" name="username" /><br />
用户年龄:<input type="text" name="userage" /><br />
<input type="submit" value="提交" />
form>
body>
html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<h1>dubbo .... 欢迎h1>
<h2><a href="/addUser">添加用户信息a>h2>
body>
html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<h1>操作成功h1>
body>
html>
PageController
@Controller
public class PageController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/{path}")
public String base(@PathVariable String path){
return path;
}
}
报错
在访问
向dubbo注册服务
userController
从dubbo中发现服务
index.jsp
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<table border="1" align="center">
<tr>
<th>用户 IDth>
<th>用户姓名th>
<th>用户年龄th>
<th>操作th>
tr>
<c:forEach items="${list }" var="user">
<tr>
<td>${user.id }td>
<td>${user.username }td>
<td>${user.userage }td>
<td><a href="">更新a> <a href="">删除a>td>
tr>
c:forEach>
table>
body>
html>
测试
成功查询出数据
UserController
dubbo中获取服务
删除按钮
测试
操作成功~