Dubbo : 简单来说,Dubbo 是一个服务治理的框架,集中管理RPC调用,并提供多种访问策略和负载来满足应用系统之间的相互调用。
RPC远程过程调用协议:它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。RPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息的到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,获得进程结果,然后调用执行继续进行。Dubbo架构注意:Registry 是一个很轻的东西,实际上Dubbo只是在注册中心共享了服务的相关信息,Dubbo的主体是Provider和Consumer,这两者都是属于应用范 围内的。Monitor也是很轻的模块,需要有监控服务在Registry进行了注册应用才能正常使用监控,监控宕掉不影响服务。
Dubbo 本身提供了多种协议的支持,hession协议也是其中之一。
dubbo:// dubbo缺省协议采用单一长连接和NIO异步通讯(非组撒),适合于小数据量大并发的服务调用,以及服务消费者机器数大于服务提者机器数的情况。
Dubbo也支持多种类型的注册中心,分别有:
<a href="http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm" class="external-link" rel="nofollow">User+Guide-zh</a>
接口定义定义Maven接口项目
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.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion >4.0.0</ modelVersion >
< groupId >com.z</ groupId >
< artifactId >Iexample</ artifactId >
< version >1.0</ version > </ project > |
定义接口
ISayHello.java1234 | package com.z.iexample; public interface ISayHello {
public String sayHelloWaitAnswer(String s); } |
定义Maven服务端项目,并提供相应依赖(Iexample,dubbo,zookeeper)。
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.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion >4.0.0</ modelVersion >
< groupId >com.z</ groupId >
< artifactId >example-server</ artifactId >
< version >1.0</ version >
< dependencies >
< dependency >
< groupId >com.z</ groupId >
< artifactId >Iexample</ artifactId >
< version >1.0</ version >
</ dependency >
< dependency >
< groupId >com.alibaba</ groupId >
< artifactId >dubbo</ artifactId >
< version >2.5.3</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context</ artifactId >
< version >3.2.1.RELEASE</ version >
</ dependency >
< dependency >
< groupId >com.github.sgroschupf</ groupId >
< artifactId >zkclient</ artifactId >
< version >0.1</ version >
</ dependency >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-api</ artifactId >
< version >1.6.4</ version >
</ dependency >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-log4j12</ artifactId >
< version >1.6.4</ version >
</ dependency >
< dependency >
< groupId >log4j</ groupId >
< artifactId >log4j</ artifactId >
< version >1.2.16</ version >
</ dependency >
</ dependencies > </ project > |
Dubbo对外的依赖是需要什么就增加什么的jar依赖,我们需要到的依赖一个是dubbo自身的依赖,另外一个是注册中心zookeeper的依赖,需要提供zookeeper的连接客户端,再者就是需要实现的接口jar了。
实现接口的服务类
SayHelloImpl.java123456789101112131415161718192021 | package com.z.example.server.rpc; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.z.iexample.ISayHello; public class SayHelloImpl implements ISayHello {
private final Logger log = LoggerFactory.getLogger(getClass());
private DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
private String template = "[%s] - %d." ;
private AtomicInteger count = new AtomicInteger();
public String sayHelloWaitAnswer(String s) {
log.info( "Receive message {} from client" , s);
Date now = new Date();
String answerTime = dateFormat.format(now);
return String.format(template, answerTime, count.getAndIncrement());
} } |
准备Spring的配置文件,因为这个示例是基于Spring Context实现的。
applicationContext.xml<? 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"
xsi:schemaLocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd" >
<!-- 提供方应用信息,用于计算依赖关系 -->
< dubbo:application name = "say-hello-server" />
<!-- Group属性指定dubbo在zookeeper中以什么路径开始,group之间相对隔离,如果不写,则所有服务都从根/开始 -->
< dubbo:registry address = "zookeeper://10.10.110.58:2181" group = "dubbo" client = "zkclient" />
<!-- 如果注册中心里有监控服务进行注册,则可以开启下面的配置,监控会从注册中心寻找监控服务端,通过protocol="registry"指定 --> <!-- <dubbo:monitor protocol="registry"/> -->
<!-- 服务提供方对外提供的连接数控制,connections代表与每一个消费者建立多少连接,accepts代表本服务提供方总共对外提供多少连接 -->
< dubbo:provider connections = "5" accepts = "8" />
<!-- 用dubbo协议在20880端口暴露服务 -->
< dubbo:protocol name = "dubbo" port = "20880" />
<!-- 声明需要暴露的服务接口 -->
< dubbo:service interface = "com.z.iexample.ISayHello" ref = "simpleServer" />
<!-- 和本地bean一样实现服务 -->
< bean id = "simpleServer" class = "com.z.example.server.rpc.SayHelloImpl" /> </ beans > |
提供主程序入口
Main.java1234567891011121314151617 | package com.z.example.server; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );
context.start();
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
} } |
定义Maven项目,提供消费者的依赖
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.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion >4.0.0</ modelVersion >
< groupId >com.z</ groupId >
< artifactId >example-client</ artifactId >
< version >1.0</ version >
< dependencies >
< dependency >
< groupId >com.z</ groupId >
< artifactId >Iexample</ artifactId >
< version >1.0</ version >
</ dependency >
< dependency >
< groupId >com.alibaba</ groupId >
< artifactId >dubbo</ artifactId >
< version >2.5.3</ version >
</ dependency >
< dependency >
< groupId >org.springframework</ groupId >
< artifactId >spring-context</ artifactId >
< version >3.2.1.RELEASE</ 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 >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-api</ artifactId >
< version >1.6.4</ version >
</ dependency >
< dependency >
< groupId >org.slf4j</ groupId >
< artifactId >slf4j-log4j12</ artifactId >
< version >1.6.4</ version >
|