标签归档:dubbo

Dubbo与Zookeeper、SpringMVC整合和使用

互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的。现在核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应。

第一:介绍Dubbo背景

大规模服务化之前,应用可能只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡。

(1) 当服务越来越多时,服务URL配置管理变得非常困难,F5硬件负载均衡器的单点压力也越来越大。

此时需要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。

并通过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,降低对F5硬件负载均衡器的依赖,也能减少部分成本。

(2) 当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。

这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。

(3) 接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?

为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。

其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。

第二:Dubbo的简介

Dubbo是一个分布式服务框架,解决了上面的所面对的问题,Dubbo的架构如图所示:


节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

Dubbo提供了很多协议,Dubbo协议、RMI协议、Hessian协议,我们查看Dubbo源代码,有各种协议的实现,如图所示:


我们之前没用Dubbo之前时,大部分都使用Hessian来使用我们服务的暴露和调用,利用HessianProxyFactory调用远程接口。

上面是参考了Dubbo官方网介绍,接下来我们来介绍SpringMVC、Dubbo、Zookeeper整合使用。

第三:Dubbo与Zookeeper、SpringMVC整合使用  第一步:在Linux上安装Zookeeper

Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心,Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求。我们先在linux上安装Zookeeper,我们安装最简单的单点,集群比较麻烦。

(1)下载Zookeeper-3.4.6.tar.gz  地址http://www.apache.org/dist/zookeeper/

(2) 我们放到Linux下的一个文件夹,然后解压:

#tar zxvf zookeeper-3.4.6.tar.gz

(3)然后在对应的zookeeper-3.4.6/conf 下有一个文件zoo_sample.cfg的这个文件里面配置了监听客户端连接的端口等一些信息,Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件,所以我们复制一个名称为zoo.cfg的文件,如图所示:

我们查看一下这个文件的里面的一些配置信息,如图所示:

说明:

clientPort:监听客户端连接的端口。

tickTime:基本事件单元,以毫秒为单位。它用来控制心跳和超时,默认情况下最小的会话超时时间为两倍的 tickTime。

我们可以对配置文件的端口等或者进行高级配置和集群配置例如:maxClientCnxns:限制连接到 ZooKeeper 的客户端的数量等

(4)启动Zookeeper 的服务,如图所示:

到这边Zookeeper的安装和配置完成

第二步:配置dubbo-admin的管理页面,方便我们管理页面

(1)下载dubbo-admin-2.4.1.war包,在Linux的tomcat部署,先把dubbo-admin-2.4.1放在tomcat的webapps/ROOT下,然后进行解压:

#jar -xvf dubbo-admin-2.4.1.war

(2)然后到webapps/ROOT/WEB-INF下,有一个dubbo.properties文件,里面指向Zookeeper ,使用的是Zookeeper 的注册中心,如图所示:

(3)然后启动tomcat服务,用户名和密码:root,并访问服务,显示登陆页面,说明dubbo-admin部署成功,如图所示:

 第三步:SpringMVC与Dubbo的整合,这边使用的Maven的管理项目

    第一:我们先开发服务注册的,就是提供服务,项目结构如图所示:

(1)test-maven-api项目加入了一个服务接口,代码如下:

public interface TestRegistryService {
   public String hello(String name);
}

(2)test-maven-console在pom.xml加入Dubbo和Zookeeper的jar包、引用test-maven-api的jar包,代码如下:

      <dependency>
  		<groupId>cn.test</groupId>
  		<artifactId>test-maven-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  
      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        
         <dependency>
            <groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
        </dependency>
  
      <dependency>
     	 <groupId>com.github.sgroschupf</groupId>
		 <artifactId>zkclient</artifactId>
		 <version>0.1</version>
      </dependency>

(3)test-maven-console实现具体的服务,代码如下:

  @Service("testRegistryService")
public class TestRegistryServiceImpl implements TestRegistryService {
	public String hello(String name) {	
		return "hello"+name;
	}
}

(4)我们服务以及实现好了,这时要暴露服务,代码如下:

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	<span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false" >
   <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
   <dubbo:application name="dubbo_provider"></dubbo:application>
   <!-- 使用zookeeper注册中心暴露服务地址 -->  
   <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>
  <!-- 要暴露的服务接口 -->  
  <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />  	
</beans>

说明:

dubbo:registry 标签一些属性的说明:

1)register是否向此注册中心注册服务,如果设为false,将只订阅,不注册。

2)check注册中心不存在时,是否报错。

3)subscribe是否向此注册中心订阅服务,如果设为false,将只注册,不订阅。

4)timeout注册中心请求超时时间(毫秒)。

5)address可以Zookeeper集群配置,地址可以多个以逗号隔开等。

dubbo:service标签的一些属性说明:

1)interface服务接口的路径

2)ref引用对应的实现类的Bean的ID

3)registry向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A

4)register 默认true ,该协议的服务是否注册到注册中心。

(5)启动项目,然后我们在Dubbo管理页面上显示,已经暴露的服务,但显示还没有消费者,因为我们还没实现消费者服务,如图所示:

第二:我们在开发服务消费者,就是调用服务,我们在新建一个新的消费者项目结构如图所示:

(1)test-maven-server-console的pom.xml引入Dubbo和Zookeeper的jar包、test-maven-api的jar包,因为引入test-maven-api的jar包,我们在项目中调用像在本地调用一样。代码如下:

 

      <dependency>
  		<groupId>cn.test</groupId>
  		<artifactId>test-maven-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  
      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        
         <dependency>
            <groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
        </dependency>
  
      <dependency>
     	 <groupId>com.github.sgroschupf</groupId>
		 <artifactId>zkclient</artifactId>
		 <version>0.1</version>
      </dependency>

(2)test-maven-server-console项目的具体实现,代码如下:

@Controller
public class IndexController {
	
	@Autowired
	private TestRegistryService testRegistryService;
	
	@RequestMapping("/hello")
	public String index(Model model){
	     String name=testRegistryService.hello("zz");
	     System.out.println("xx=="+name);
		return "";
	}

}

(3)我们要引用的地址,代码如下:

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	<span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	<span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false" >

   <dubbo:application name="dubbo_consumer"></dubbo:application>
   <!-- 使用zookeeper注册中心暴露服务地址 -->  
   <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry> 
     <!-- 要引用的服务 -->  
   <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>
</beans>

说明:

dubbo:reference 的一些属性的说明:

1)interface调用的服务接口

2)check 启动时检查提供者是否存在,true报错,false忽略

3)registry 从指定注册中心注册获取服务列表,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔

4)loadbalance 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用

 

(4)项目启动,Dubbo管理页面,能看到消费者,如图所示:

(5)然后访问消费者项目,Controller层能像调用本地一样调用服务的具体实现,如图所示:

Dubbo提供了多种容错方案,包括负载均衡这些,如图所示:

 

 

参考资料:

     http://dubbo.io/Home-zh.htm

 

Dubbo实现的源码分析

1.      Dubbo概述

Dubbo是阿里巴巴开源出来的一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及作为SOA服务治理的方案。它的核心功能包括:

#remoting:远程通讯基础,提供对多种NIO框架抽象封装,包括“同步转异步”和“请求-响应”模式的信息交换方式。

#Cluster: 服务框架核心,提供基于接口方法的远程过程调用,包括多协议支持,并提供软负载均衡和容错机制的集群支持。

#registry: 服务注册中心,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

由于Dubbo团队的文档和代码都非常优秀,所以更多关于dubbo的方方面面请参考网站http://code.alibabatech.com/wiki/display/dubbo/Home-zh

这里我们只是补充一下从源码具体实现角度来看的某些细节方面,包括Invoker、ExtensionLoader等方面。任何官方已经介绍过的细节,我们不做画蛇添足,官方文档已经足够详实了,这篇文档的定位是补充实现的相关细节,是基于我在往Dubbo添加web service协议过程中,所碰到过的一些困难。

 

2. 服务提供者暴露一个服务的详细过程

上图是服务提供者暴露服务的主过程:

首先ServiceConfig类拿到对外提供服务的实际类ref(如:HelloWorldImpl),然后通过ProxyFactory类的 getInvoker方法使用ref生成一个AbstractProxyInvoker实例,到这一步就完成具体服务到Invoker的转化。接下来就是 Invoker转换到Exporter的过程。

Dubbo处理服务暴露的关键就在Invoker转换到Exporter的过程(如上图中的红色部分),下面我们以Dubbo和RMI这两种典型协议的实现来进行说明:

 

Dubbo的实现

Dubbo协议的Invoker转为Exporter发生在DubboProtocol类的export方法,它主要是打开socket侦听服务,并接收客户端发来的各种请求,通讯细节由Dubbo自己实现。

 

RMI的实现

RMI协议的Invoker转为Exporter发生在RmiProtocol类的export方法,它通过Spring或Dubbo或JDK来实现RMI服务,通讯细节这一块由JDK底层来实现,这就省了不少工作量。

 

3. 服务消费者消费一个服务的详细过程

上图是服务消费的主过程:

首先ReferenceConfig类的init方法调用Protocol的refer方法生成Invoker实例(如上图中的红色部分),这是服务消费的关键。接下来把Invoker转换为客户端需要的接口(如:HelloWorld)。

关于每种协议如RMI/Dubbo/Web service等它们在调用refer方法生成Invoker实例的细节和上一章节所描述的类似。

 

4. 满眼都是Invoker

 

由于Invoker是Dubbo领域模型中非常重要的一个概念,很多设计思路都是向它靠拢。这就使得Invoker渗透在整个实现代码里,对于刚开始接触Dubbo的人,确实容易给搞混了。

下面我们用一个精简的图来说明最重要的两种Invoker:服务提供Invoker和服务消费Invoker:

 

为了更好的解释上面这张图,我们结合服务消费和提供者的代码示例来进行说明:

#服务消费者代码

public class DemoClientAction {

private DemoService demoService;

public void setDemoService(DemoService demoService) {

this.demoService = demoService;

}

public void start() {

String hello = demoService.sayHello("world" + i);

}

}
       上面代码中的’DemoService’就是上图中服务消费端的proxy,用户代码通过这个proxy调用其对应的 Invoker(DubboInvoker、 HessianRpcInvoker、 InjvmInvoker、 RmiInvoker、 WebServiceInvoker中的任何一个),而该Invoker实现了真正的远程服务调用。

 

#服务提供者代码

public class DemoServiceImpl

implements DemoService

{

public String sayHello(String name) throws RemoteException

{

return "Hello " + name;

}

}

 

上面这个类会被封装成为一个AbstractProxyInvoker实例,并新生成一个Exporter实例。这样当网络通讯层收到一个请求后,会找到 对应的Exporter实例,并调用它所对应的AbstractProxyInvoker实例,从而真正调用了服务提供者的代码。

Dubbo里还有一些其他的Invoker类,但上面两种是最重要的。

5. ExtensionLoader的完整分析

 

ExtensionLoader是Dubbo中一个非常重要的类,刚接触Dubbo源码的人看这个类的时候也多少会有点困惑,这个类非常重要,它就像是厨房里的“大厨”,按照用户的随时需要把各种“食材”烹调出来。

我们结合具体代码详细说一下ExtensionLoader的实现,下面是ServiceConfig类里的一行代码:

private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();

上面代码的程序流程图如下所示(假定是第一次执行这行代码):

在这个过程中最重要的两个方法是getExtensionClasses和createAdaptiveExtensionClass(图中红色部分),下面详细对这两个方法进行分析:

getExtensionClasses

这个方法主要读取META-INF/services/目录下对应文件内容,在本示例代码中,是读取META-INF/services/com.alibaba.dubbo.rpc.Protocol文件中的内容,具体内容如下:

com.alibaba.dubbo.registry.support.RegistryProtocol

com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper

com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper

com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol

com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol

com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol

com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol

com.alibaba.dubbo.rpc.protocol.webservice.WebServiceProtocol

它分析该文件中的每一行(每一行对应一个类),分析这些类,如果发现有哪个类的Annotation是@Adaptive,则找到对应的 AdaptiveClass了,但由于Protocol文件里没有哪个类的Annotation是@Adaptive,所以在这个例子中该方法没找到对应 的AdaptiveClass。

createAdaptiveExtensionClass

该方法是在getExtensionClasses方法找不到AdaptiveClass的情况下被调用,该方法主要是通过字节码的方式在内存中新 生成一个类,它具有AdaptiveClass的功能,Protocol就是通过这种方式获得AdaptiveClass类的。

   AdaptiveClass类的作用是能在运行时动态判断具体是要调用哪个类的方法,更多关于AdaptiveClass的内容请参考Dubbo官方文档。

关键代码:

com.taobao.remoting.impl.DefaultClient.java

//同步调用远程接口

public Object invokeWithSync(Object appRequest, RequestControl control) throws RemotingException, InterruptedException {

byte protocol = getProtocol(control);

if (!TRConstants.isValidProtocol(protocol)) {

throw new RemotingException("Invalid serialization protocol [" + protocol + "] on invokeWithSync.");

}

ResponseFuture future = invokeWithFuture(appRequest, control);

return future.get();  //获取结果时让当前线程等待,ResponseFuture其实就是前面说的callback

}

public ResponseFuture invokeWithFuture(Object appRequest, RequestControl control) {

byte protocol = getProtocol(control);

long timeout = getTimeout(control);

ConnectionRequest request = new ConnectionRequest(appRequest);

request.setSerializeProtocol(protocol);

Callback2FutureAdapter adapter = new Callback2FutureAdapter(request);

connection.sendRequestWithCallback(request, adapter, timeout);

return adapter;

}

Callback2FutureAdapter implements ResponseFuture

public Object get() throws RemotingException, InterruptedException {

synchronized (this) {  // 旋锁

while (!isDone) {  // 是否有结果了

wait(); //没结果是释放锁,让当前线程处于等待状态

}

}

if (errorCode == TRConstants.RESULT_TIMEOUT) {

throw new TimeoutException("Wait response timeout, request["

+ connectionRequest.getAppRequest() + "].");

}

else if (errorCode > 0) {

throw new RemotingException(errorMsg);

}

else {

return appResp;

}

}

客户端收到服务端结果后,回调时相关方法,即设置isDone = true并notifyAll()

public void handleResponse(Object _appResponse) {

appResp = _appResponse; //将远程调用结果设置到callback中来

setDone();

}

public void onRemotingException(int _errorType, String _errorMsg) {

errorCode = _errorType;

errorMsg = _errorMsg;

setDone();

}

private void setDone() {

isDone = true;

synchronized (this) { //获取锁,因为前面wait()已经释放了callback的锁了

notifyAll(); // 唤醒处于等待的线程

}

}

com.taobao.remoting.impl.DefaultConnection.java

// 用来存放请求和回调的MAP

private final ConcurrentHashMap<Long, Object[]> requestResidents;

//发送消息出去

void sendRequestWithCallback(ConnectionRequest connRequest, ResponseCallback callback, long timeoutMs) {

long requestId = connRequest.getId();

long waitBegin = System.currentTimeMillis();

long waitEnd = waitBegin + timeoutMs;

Object[] queue = new Object[4];

int idx = 0;

queue[idx++] = waitEnd;

queue[idx++] = waitBegin;   //用于记录日志

queue[idx++] = connRequest; //用于记录日志

queue[idx++] = callback;

requestResidents.put(requestId, queue); // 记录响应队列

write(connRequest);

// 埋点记录等待响应的Map的大小

StatLog.addStat("TBRemoting-ResponseQueues", "size", requestResidents.size(),

1L);

}

public void write(final Object connectionMsg) {

//mina里的IoSession.write()发送消息

WriteFuture writeFuture = ioSession.write(connectionMsg);

// 注册FutureListener,当请求发送失败后,能够立即做出响应

writeFuture.addListener(new MsgWrittenListener(this, connectionMsg));

}

/**

* 在得到响应后,删除对应的请求队列,并执行回调

* 调用者:MINA线程

*/

public void putResponse(final ConnectionResponse connResp) {

final long requestId = connResp.getRequestId();

Object[] queue = requestResidents.remove(requestId);

if (null == queue) {

Object appResp = connResp.getAppResponse();

String appRespClazz = (null == appResp) ? "null" : appResp.getClass().getName();

StringBuilder sb = new StringBuilder();

sb.append("Not found response receiver for requestId=[").append(requestId).append("],");

sb.append("from [").append(connResp.getHost()).append("],");

sb.append("response type [").append(appRespClazz).append("].");

LOGGER.warn(sb.toString());

return;

}

int idx = 0;

idx++;

long waitBegin = (Long) queue[idx++];

ConnectionRequest connRequest = (ConnectionRequest) queue[idx++];

ResponseCallback callback = (ResponseCallback) queue[idx++];

// ** 把回调任务交给业务提供的线程池执行 **

Executor callbackExecutor = callback.getExecutor();

callbackExecutor.execute(new CallbackExecutorTask(connResp, callback));

long duration = System.currentTimeMillis() - waitBegin; // 实际读响应时间

logIfResponseError(connResp, duration, connRequest.getAppRequest());

}

CallbackExecutorTask

static private class CallbackExecutorTask implements Runnable {

final ConnectionResponse resp;

final ResponseCallback callback;

final Thread createThread;

CallbackExecutorTask(ConnectionResponse _resp, ResponseCallback _cb) {

resp = _resp;

callback = _cb;

createThread = Thread.currentThread();

}

public void run() {

// 预防这种情况:业务提供的Executor,让调用者线程来执行任务

if (createThread == Thread.currentThread()

&& callback.getExecutor() != DIYExecutor.getInstance()) {

StringBuilder sb = new StringBuilder();

sb.append("The network callback task [" + resp.getRequestId() + "] cancelled, cause:");

sb.append("Can not callback task on the network io thhread.");

LOGGER.warn(sb.toString());

return;

}

if (TRConstants.RESULT_SUCCESS == resp.getResult()) {

callback.handleResponse(resp.getAppResponse()); //设置调用结果

}

else {

callback.onRemotingException(resp.getResult(), resp

.getErrorMsg());  //处理调用异常

}

}

}

另外:

1, 服务端在处理客户端的消息,然后再处理时,使用了线程池来并行处理,不用一个一个消息的处理

同样,客户端接收到服务端的消息,也是使用线程池来处理消息,再回调

来源:http://blog.csdn.net/aisoo/article/details/8286875