说明:这里只简单演示使用dubbo远程调用框架实现接口调用,zookeeper安装到本地测试
项目说明:
com.maven.api:公用api,这里只存放最基本的service接口,以及实体类,pom.xml中需要引入dubbo+zookeeper 依赖包
com.maven.core:服务提供方,也是数据库访问层,实现业务逻辑,需要引入com.maven.api依赖
com.maven.consumer:服务调用方,web项目,对外开放访问权限,需要引入com.maven.api依赖
一,com.maven.api 项目
(一) 引入依赖包,pom.xml 配置文件如下:
4.0.0
com.maven.dubbo
com.maven.api
0.0.1-SNAPSHOT
com.alibaba
dubbo
2.5.3
org.springframework
spring
com.101tec
zkclient
0.7
(二) 新建UserService
package com.maven.facade;
import java.util.List;
import com.maven.entity.UserInfo;
public interface UserService {
Integer insert(UserInfo userInfo);
Integer delete(Long uid);
UserInfo select(Long uid);
/** 获取用户信息集合 */
List listUserInfo();
}
(三) 新建实体类对应数据库user_info表
package com.maven.entity;
import java.io.Serializable;
import java.util.Date;
public class UserInfo implements Serializable{
private static final long serialVersionUID = 5775086719909153531L;
private Long id;
private String userName;
private String password;
private String email;
private String phone;
private String sex;
private String status;
private Date createTime;
private Date updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status == null ? null : status.trim();
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", userName=" + userName + ", password=" + password + ", email=" + email
+ ", phone=" + phone + ", sex=" + sex + "]";
}
}
二,com.maven.core
使用sping+mybatis+mysql构建项目,前面已经介绍过相关框架,这里不多介绍
不熟悉的同学可以跳转查看:http://blog.csdn.net/mynoteblog/article/details/54922775
(一) pom.xml 引入相关依赖
4.0.0
com.maven.dubbo
com.maven.core
0.0.1-SNAPSHOT
4.3.0.RELEASE
3.3.0
1.2.3
5.1.29
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
com.maven.dubbo
com.maven.api
0.0.1-SNAPSHOT
junit
junit
3.8.1
test
javax.annotation
javax.annotation-api
1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
commons-dbcp
commons-dbcp
1.4
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis-spring.version}
org.mybatis
mybatis-generator-core
1.3.2
mysql
mysql-connector-java
${mysql.connector.version}
(二) 配置Spring-config.xml
(三) 配置Spring-dubbo.xml,配置注册中心地址,暴露dubbo服务
这里使用的是安装在本地的zookeeper,启动项目前需要先启动zookeeper,默认端口2181
(四) 配置dubbo-provider.xml,这里主要是配置需要提供服务的接口及方法
这里需要注意:ref="userService" 接口名需要对应到userServiceImpl,
本例采用注解方式配置,如下配置@Service("userService")
(五) 新建UserServiceImpl实现UserService接口
package com.maven.core.facade.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.maven.core.mapper.UserInfoMapper;
import com.maven.entity.UserInfo;
import com.maven.facade.UserService;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource
private UserInfoMapper userInfoMapper;
public Integer insert(UserInfo userInfo) {
return userInfoMapper.insertSelective(userInfo);
}
public Integer delete(Long uid) {
return userInfoMapper.deleteByPrimaryKey(uid);
}
public UserInfo select(Long uid) {
System.out.println("............");
return userInfoMapper.selectByPrimaryKey(uid);
}
public List listUserInfo() {
System.out.println(".............");
List list = userInfoMapper.selectAll();
return list;
}
}
(六) UserInfoMapper
package com.maven.core.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.maven.entity.UserInfo;
@Repository
public interface UserInfoMapper {
int deleteByPrimaryKey(Long id);
int insert(UserInfo record);
int insertSelective(UserInfo record);
UserInfo selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(UserInfo record);
int updateByPrimaryKey(UserInfo record);
List selectAll();
}
(七) UserInfoMapper.xml
id, user_name, password, email, phone, sex, status, create_time, update_time
delete from user_info
where id = #{id,jdbcType=BIGINT}
insert into user_info (id, user_name, password,
email, phone, sex,
status, create_time, update_time
)
values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
)
insert into user_info
id,
user_name,
password,
email,
phone,
sex,
status,
create_time,
update_time,
#{id,jdbcType=BIGINT},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR},
now(),
now(),
update user_info
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
status = #{status,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP},
where id = #{id,jdbcType=BIGINT}
update user_info
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
status = #{status,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
(八) 启动服务,这里在main函数加载启动
package com.maven.core.provider;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 启动Spring容器
* @author Administrator
*
*/
public class Provider {
public static void main(String[] args) throws IOException {
String[] files = new String[]{"classpath:Spring-config.xml"};
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(files);
context.registerShutdownHook();
context.start();
CountDownLatch latch = new CountDownLatch(1);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
// 为保证服务一直开着,利用输入流的阻塞来模拟
System.in.read();
}
}
}
(一) 引入相关依赖,pom.xml
4.0.0
com.maven.dubbo
com.maven.consumer
war
0.0.1-SNAPSHOT
com.maven.consumer Maven Webapp
4.3.0.RELEASE
2.6.5
1.2.23
3.3.0
1.2.3
5.1.29
2.7.3
1.7.1.RELEASE
1.2.4
1.7.21
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
Version99
Version 99 Does Not Exist Maven repository
default
http://no-commons-logging.zapto.org/mvn2
junit
junit
3.8.1
test
com.maven.dubbo
com.maven.api
0.0.1-SNAPSHOT
javax.annotation
javax.annotation-api
1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
com.alibaba
fastjson
${fastjson.version}
javax.servlet
javax.servlet-api
3.1.0
provided
org.slf4j
slf4j-api
${slf4j.version}
ch.qos.logback
logback-classic
1.1.3
org.slf4j
jcl-over-slf4j
${slf4j.version}
org.slf4j
log4j-over-slf4j
${slf4j.version}
org.logback-extensions
logback-ext-spring
0.1.2
commons-logging
commons-logging
99.0-does-not-exist
com.maven.dubbo
com.maven.api
0.0.1-SNAPSHOT
com.maven.consumer
(二) 相关配置文件:
web.xml
contextConfigLocation
classpath:Spring-config.xml
logbackConfigLocation
classpath:logback.xml
org.springframework.web.util.IntrospectorCleanupListener
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextCleanupListener
ch.qos.logback.ext.spring.web.LogbackConfigListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:Spring-servlet.xml
1
true
SpringMVC
/
index.jsp
Spring-config.xml
Spring-servlet.xml
JSON转换器
application/json;charset=UTF-8
text/html;charset=UTF-8
QuoteFieldNames
WriteDateUseDateFormat
Spring-dubbo.xml,服务调用方注册同一个zookeeper
dubbo-consumer.xml,配置调用接口信息,与服务提供方配置相似,注意接口名一直
(三) 测试,UserController
package com.maven.consumer.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.maven.entity.UserInfo;
import com.maven.facade.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@ResponseBody
@RequestMapping(value="/insert", method=RequestMethod.POST)
public String insert(@RequestBody UserInfo userInfo){
if(userInfo.getStatus()==null){
userInfo.setStatus("0");
}
Integer count = userService.insert(userInfo);
if(count>0){
return "保存用户信息成功";
}
return "保存用户信息失败";
}
@ResponseBody
@RequestMapping(value="/select", method=RequestMethod.GET)
public String select(@RequestParam Long uid){
UserInfo userInfo = userService.select(uid);
if(userInfo!=null){
return "您要查找的用户名是:"+userInfo.getUserName();
}
return "查找用户失败";
}
@ResponseBody
@RequestMapping(value="/delete", method=RequestMethod.DELETE)
public String delete(@RequestParam Long uid){
Integer count = userService.delete(uid);
if(count>0){
return "删除用户信息成功";
}
return "删除用户信息失败";
}
@RequestMapping(value="/list",method=RequestMethod.GET)
@ResponseBody
public List list(){
System.out.println(".............");
List userInfos = userService.listUserInfo();
return userInfos;
}
}
(一) 启动zookeeper,在本地安装zookeeper后找到bin文件夹,开启zkServer.cmd
(二) 启动服务提供方,找到Provider.java,运行启动
(三) 启动服务调用方,将项目部署到tomcat,启动
(四) 调用测试:
测试成功,分享项目源码:http://pan.baidu.com/s/1jI7J802