dubbo:分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案
核心部分包括:
1,远程通讯 :提供对多种基于长连接的NIO框架抽象封装,包括多线程模型,序列化,以及“请求-响应”模式的信息交换方式
2,集群容错 :提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持
3,自动发现 :基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
广播注册 multicast
不需要启动任何中心节点,只要广播地址一致,就会互相发现
1,提供者启动时广播自己的ip地址
2,消费者启动时广播订阅请求
3,提供者收到请求时,单播自己的地址给订阅者,如果设置了unicast=false,则广播给订阅者
4,消费者收到提供方地址时,连接该地址进行RPC调用。
<dubbo:registry address:"multicast://244.5.6.7:1234"/>
或
<dubbo:registry protocol="multicast" address="224.5.6.7:1234" />
为了减少广播量,dubbo缺省使用单播发送提供者地址信息给消费者,如果一个机器上同时启动了多个消费者进程,消费者需声明unicast=false,否则只会有一个消费者能收到信息。
<dubbo:registry address:"multicast://244.5.6.7:1234?unicast=false"/>
或
<dubbo:registry protocol="multicast" address="224.5.6.7:1234">
<dubbo:parameter key="unicast" value="false" />
</dubbo:registry>
zookeeper注册
需要在消费者项目 提供者项目中加入
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
从2.2.0版本开始缺省为zkclient实现,以提升zookeeper客户端的健状性。
ZKClient是Datameer开源的一个Zookeeper客户端实现,开源比较早,参见:https://github.com/sgroschupf/zkclient
缺省配置
<dubbo:registry ... client="zkclient" />
需在pom中添加依赖
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
zookeeper集群注册
<dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" />
或
<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />
此外,还有 redis注册中心 simple注册中心 详见http://dubbo.io/user-guide/reference-registry/redis.html
xml配置方式
服务提供者 provider.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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app" />
<!-- 使用multicast(组广播)广播注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="demo.api.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="demo.api.DemoServiceImpl" />
</beans>
服务提供者需要编写一个接口
public interface DemoService {
public String sayHello(String msg);
}
该接口的实现类:
public class DemoServiceImpl implements DemoService {
public String sayHello(String msg) {
return "Hello~~~~~"+msg;
}
}
服务提供者的主程序
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class provider {
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"dubbo-provider.xml"});
context.start();
System.out.println( "服务提供者启动(按任意键退出或在console窗口中停止):" );
}
}
服务消费者 consumer.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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.api.DemoService" />
</beans>
消费者主函数
import com.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class consumer {
public static void main( String[] args ) throws IOException
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"consumer.xml"});
context.start();
System.out.println( "消费者启动(按任意键退出或在console窗口中停止):" );
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String msg = demoService.sayHello("WANGJING");
System.out.println( "调用结果:"+msg );
System.in.read();
System.out.println( "已退出:" );
}
}
此外,消费者还需要编写一个接口,并不去实现,而是服务提供者实现
package com.api;
public interface DemoService {
public String sayHello(String msg);
}
运行结果如下:
服务提供方:
服务消费方: