一、简介
Dubbox是一个开源的RPC(Remote ProcedureCall Protocol)远程调用框架,是由dangdang对阿里的Dubbo的升级,可以被视为Dubbo的增强版,基本架构没有变动,升级spring2.x到spring3.x,支持restful风格的调用调试方式,丰富了序列化的方式,提高了序列化的性能。
二、dubbox当前的主要功能
摘录官方的说明:
支持REST风格远程调用(HTTP + JSON/XML):基于非常成熟的JBoss RestEasy框架,在dubbo中实现了REST风格(HTTP + JSON/XML)的远程调用,以显著简化企业内部的跨语言交互,同时显著简化企业对外的Open API、无线API甚至AJAX服务端等等的开发。事实上,这个REST调用也使得Dubbo可以对当今特别流行的“微服务”架构提供基础性支持。 另外,REST调用也达到了比较高的性能,在基准测试下,HTTP + JSON与Dubbo 2.x默认的RPC协议(即TCP + Hessian2二进制序列化)之间只有1.5倍左右的差距,详见文档中的基准测试报告。
支持基于Kryo和FST的Java高效序列化实现:基于当今比较知名的Kryo和FST高性能序列化库,为Dubbo默认的RPC协议添加新的序列化实现,并优化调整了其序列化体系,比较显著的提高了Dubbo RPC的性能,详见文档中的基准测试报告。
支持基于Jackson的JSON序列化:基于业界应用最广泛的Jackson序列化库,为Dubbo默认的RPC协议添加新的JSON序列化实现。
支持基于嵌入式Tomcat的HTTP remoting体系:基于嵌入式tomcat实现dubbo的HTTP remoting体系(即dubbo-remoting-http),用以逐步取代Dubbo中旧版本的嵌入式Jetty,可以显著的提高REST等的远程调用性能,并将Servlet API的支持从2.5升级到3.1。(注:除了REST,dubbo中的WebServices、Hessian、HTTP Invoker等协议都基于这个HTTP remoting体系)。
升级Spring:将dubbo中Spring由2.x升级到目前最常用的3.x版本,减少版本冲突带来的麻烦。
升级ZooKeeper客户端:将dubbo中的zookeeper客户端升级到最新的版本,以修正老版本中包含的bug。
支持完全基于Java代码的Dubbo配置:基于Spring的Java Config,实现完全无XML的纯Java代码方式来配置dubbo
调整Demo应用:暂时将dubbo的demo应用调整并改写以主要演示REST功能、Dubbo协议的新序列化方式、基于Java代码的Spring配置等等。
修正了dubbo的bug 包括配置、序列化、管理界面等等的bug。
注:dubbox和dubbo 2.x是兼容的,没有改变dubbo的任何已有的功能和配置方式(除了升级了spring之类的版本)
三、项目搭建
1、工程结构,本项目为maven工程:
2、POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gsl</groupId>
<artifactId>gsl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>gsl-common</artifactId>
<packaging>war</packaging>
<name>service-provider</name>
<properties>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>
<!-- Redis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>service-provider</finalName>
</build>
</project>
主要引用了dubbo的2.8.4版本的包,以及restful和jackson依赖的包。
dubbox的restful是使用标准java rest api:JAX-RS。一般使用它的几个注解就可以了。
3、以一个用户管理的模块为例,建立User类
package entity;
public class User {
Long id;
String name;
int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4、建立接口UserService.java
package service;
import entity.User;
public interface UserService {
String regist(User user);
User queryById(Long id);
String regist2();
}
5、接口实现类UserServiceImpl.java
package service.impl;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.RpcContext;
import entity.User;
@Path("user")
@Service
public class UserServiceImpl implements UserService {
@POST
@Path("regist")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
@Override
public String regist(User user) {
System.out.println(user);
HttpServletRequest request = (HttpServletRequest) RpcContext.getContext().getRequest();
String host = request.getLocalAddr();
return "regist success!, ur host is:" + host;
}
@POST
@Path("regist2")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
@Override
public String regist2() {
HttpServletRequest request = (HttpServletRequest) RpcContext.getContext().getRequest();
String host = request.getLocalAddr();
return "regist success!, ur host is:" + host;
}
@POST
@Path("{id:\\d+}")
@Produces({ MediaType.APPLICATION_JSON })
@Override
public User queryById(@PathParam("id") Long uid) {
User user = new User();
user.setId(uid);
user.setName("jetty");
user.setAge(25);
return user;
}
}
注解代码说明:
@Path:代表服务的访问路径,如“user”,则访问路径为domain:port/context/user,在方法上注解是相对类上面的。依次展开路径即可。
@POST:服务请求的方式,支持POST、GET等。
@Consumes:表示将接收到的数据自动转换为什么格式的,支持json和xml。
@Produces:表示将结果返回时候输出为什么格式的,支持json和xml。
@PathParm:用来修饰方法参数的。配合@Path中的指定变量名使用。
6、dubbo启动类
package service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboProvider {
private static final Log logger = LogFactory.getLog(DubboProvider.class);
private static ClassPathXmlApplicationContext context;
public static void main(String[] args) {
try {
context = new ClassPathXmlApplicationContext("classpath:context.xml");
context.start();
} catch (Exception e) {
logger.error("== DubboProvider context start error:", e);
}
synchronized (DubboProvider.class) {
while (true) {
try {
DubboProvider.class.wait();
} catch (InterruptedException e) {
logger.error("== synchronized error:", e);
}
}
}
}
}
7、spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
">
<!-- 注解扫描 -->
<context:component-scan base-package="service" />
<!-- dubbo 配置 -->
<import resource="provider.xml" />
</beans>
8、dubbox配置文件,主要注册在redis服务上
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
<dubbo:application name="service-provider" />
<dubbo:registry address="redis://127.0.0.1:6379" />
<!-- 指定服务使用rest协议并且暴露在8080端口 -->
<dubbo:protocol name="rest" port="8082" />
<dubbo:service interface="service.UserService" ref="userService" />
<bean id="userService" class="service.impl.UserServiceImpl" />
</beans>