第一篇:手把手教你搭建dubbo框架

SSM+dubbo+zookeeper搭建分布式框架

好的,废话不多说,直接开始搭。
这个项目总工分为四个部分:
dubbo-parent
dubbo-server
dubbo-service-api
dubbo-client
至于其他的你还想在细分的,只要学会这个以后,都是可以的。

准备工作

安装zookeeper并启动
eclipse配置dubbo.xsd,不然在配置文件中没法用dubbo
ssm框架

dubbo-parent搭建

这个项目整个是基于ssm基础的,所以应该至少会SSM框架的搭建(相信大家都会哦)
开始搭建:
1.新建一个maven项目,选择maven project



2.注意标注的地方,一定要打勾


3. 记得选择packaging类型为pom


4.打开该项目的pom.xml文件,导入依赖:

	4.0.0
	com.test.dubbo
	dubbo--parent
	0.0.1-SNAPSHOT
	pom

	
	
		4.0.2.RELEASE
		3.2.8
		1.7.12
		1.2.17
	

	
		
			junit
			junit
			3.8.1
			test
		
		
			com.alibaba
			dubbo
			2.5.3
			
				
					spring
					org.springframework
				
			
		
		
			org.apache.zookeeper
			zookeeper
			3.4.6
			
				
					log4j
					log4j
				
			
		
		
		
			javax
			javaee-api
			7.0
		
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
			org.codehaus.jackson
			jackson-core-asl
			1.9.13
		
		
		
			org.springframework
			spring-test
			${srping.version}
		
		
			org.springframework
			spring-core
			${srping.version}
		
		
			org.springframework
			spring-oxm
			${srping.version}
		
		
			org.springframework
			spring-tx
			${srping.version}
		
		
			org.springframework
			spring-jdbc
			${srping.version}
		
		
			org.springframework
			spring-aop
			${srping.version}
		
		
			org.springframework
			spring-context
			${srping.version}
		
		
			org.springframework
			spring-context-support
			${srping.version}
		
		
			org.springframework
			spring-expression
			${srping.version}
		
		
			org.springframework
			spring-orm
			${srping.version}
		
		
			org.springframework
			spring-web
			${srping.version}
		
		
			org.springframework
			spring-webmvc
			${srping.version}
		
		
		
		
			org.mybatis
			mybatis
			${mybatis.version}
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		
		
		
		
			mysql
			mysql-connector-java
			5.1.18
		
		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
		
			jstl
			jstl
			1.2
		
		
		
			log4j
			log4j
			${log4j.version}
		
		
			org.slf4j
			slf4j-api
			${slf4j.version}
		
		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		
		
		
			ch.qos.logback
			logback-classic
			1.1.1
		
		
		
		
		
			com.alibaba
			fastjson
			1.2.6
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
		
			commons-fileupload
			commons-fileupload
			1.3.1
		
		
			commons-io
			commons-io
			2.4
		
		
			commons-codec
			commons-codec
			1.10
		
		
		
		
			org.springframework.data
			spring-data-redis
			1.6.0.RELEASE
		
		
			redis.clients
			jedis
			2.7.3
		
		
		
		
			com.dyuproject.protostuff
			protostuff-core
			1.0.8
		
		
			com.dyuproject.protostuff
			protostuff-runtime
			1.0.8
		
		
			commons-collections
			commons-collections
			3.2
		

		
			com.github.sgroschupf
			zkclient
			0.1
		
	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.2
				
					1.7
					1.7
					UTF-8
				
			
		
	
注意待会添加完子项目后,pom.xml会自动添加如下配置

	dubbo-server
	dubbo-client
dubbo-parent搭建完成

dubbo-service-api的搭建

新建一个maven项目,项目名叫dubbo-service-api
单击左上角file->new->orther->maven->maven project
注意这里要打勾哦


2.选择 quickstart项目


点击finish,完成创建
然后在此项目中添加两个类:User.实体类和UserService接口
User实体类
package com.dubbo.entity;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户实体类
 * @author 郭鹏飞
 *
 */
public class User implements Serializable{
	
	private String id;
	private String name;
	private String password;
	private String salt;
	private Date createTime;
	private Date updateTime;
	private int b_enable;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSalt() {
		return salt;
	}
	public void setSalt(String salt) {
		this.salt = salt;
	}
	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;
	}
	public int getB_enable() {
		return b_enable;
	}
	public void setB_enable(int b_enable) {
		this.b_enable = b_enable;
	}
}

UserService接口
package com.dubbo.service;

import java.util.List;

import com.dubbo.entity.User;

/**
 * 用户操作实现类
 * @author 郭鹏飞
 *
 */
public interface UserService {

	List getAllUser();
	
}

dubbo-service-api项目创建完毕
在dubbo-parent项目的pom.xml中添加如下代码
		
			com.test.dubbo
			dubbo-service-api
			0.0.1-SNAPSHOT
		

dubbo-server搭建

右键dubbo-parent项目,新建一个maven module项目





新建完成,添加SSM框架的一些配置文件
这里要新增一个xml配置文件application-dubbo.xml



	
	

	
	

	
	
	
	
	
	
	
	
  

dubbo-client框架搭建

新建一个dubbo-client项目,创建方法跟dubbo-server一样
同时为该框架搭建一个SSM环境
增加application-dubbo.xml配置文件



	
	

	
	

	
	
	
	
	
  

新建一个UserController类
package com.dubbo.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.dubbo.entity.User;
import com.dubbo.service.RedisService;
import com.dubbo.service.UserService;

@Controller
@RequestMapping("/test")
public class TestController {

	@Autowired
	private UserService userService;
	@Autowired
	private RedisService redisService;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@RequestMapping("/getUser")
	@ResponseBody
	public String getUser(){
		System.out.println(sdf.format(new Date())+"==========================================开始=======================================");
		List list = userService.getAllUser();
		String json = JSONArray.toJSON(list).toString();
		System.out.println(sdf.format(new Date())+"==========================================结束=======================================");
		return json;
	}
}

搭建完成

测试

运行zookeeper服务器
将dubbo-server项目和dubbo-client项目拉进两个Tomcat里(一个也可以),运行两个项目
在页面中访问localhost:8080/dubbo-client/test/getUser.html
返回数据库中的数据,测试成功

你可能感兴趣的:(dubbo)