作者:刘海龙
微博:[http://weibo.com/liuhailong2008]
博客:[http://blog.youkuaiyun.com/stationxp]
计划:
计划是这样的:先把Duboo所有文档看一遍;然后启动起来,玩一把;然后看源码汲取营养;最后再把文档过一遍。
用户指南部分篇幅较长,本次涉及:入门、快速启动、依赖、成熟度、配置、示例、Api参考、配置参考
2015-02-16 天园小区
入门
背景
规模增大,ORM->MVC->RPC->SOA
RPC体现业务复用和整合。
流动计算体现资源治理。
Dubbo服务治理
服务分层:核心(服务容器)、集成(服务编排)、前段(服务授权、负载均衡、路由发现、服务降级)。
监控中心:服务质量协定。
服务注册中心。
服务治理中心。
服务治理需要解决的问题:
1. 服务的注册和发现。
2. 自动管理服务间依赖。
3. 服务负载可视化。
架构
服务启动:生产者所在容器启动、生产者向注册中心注册、消费者向注册中心订阅。
调用过程中,生产者和消费者都向监控中心汇报调用次数和调用时间。
联通性
服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。
监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇总后每分钟一次发送到监控中心服务器,并以报表展示。
注册中心,服务提供者,服务消费者三者之间均为长连接,监控中心除外。
注册中心通过长连接感知服务提供者的存在,服务提供者宕机,注册中心将立即推送事件通知消费者。
注册中心和监控中心全部宕机,不影响已运行的提供者和消费者,消费者在本地缓存了提供者列表。
注册中心和监控中心都是可选的,服务消费者可以直连服务提供者。
健壮性
数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务。(Zookeeper只是实现的一种)
服务提供者无状态,任意一台宕掉后,不影响使用。
服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复。
伸缩性
注册中心可动态增加部署。客户端可动态发现新的注册中心。
服务提供者无状态,可动态增加机器部署实例,注册中心将推送新的服务提供者信息给消费者。
用法
生产者暴露服务
使用<duboo:service>
暴露服务。
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” /> <!-- 和本地服务一样实现远程服务 -->
<dubbo:service interface=“com.xxx.XxxService” ref=“xxxService” /> <!-- 增加暴露远程服务配置 -->
消费者引用服务
使用<duboo:reference>
引用服务。
<dubbo:reference id=“xxxService” interface=“com.xxx.XxxService” /> <!-- 增加引用远程服务配置 -->
<bean id=“xxxAction” class=“com.xxx.XxxAction”> <!-- 和本地服务一样使用远程服务 -->
<property name=“xxxService” ref=“xxxService” />
</bean>
快速启动
可以通过Spring方式,也可以API方式。
API配置
正在考虑实现Open API,格外关注下,下来看源码时也注意下。
生产者
import com.alibaba.dubbo.rpc.config.ApplicationConfig;
import com.alibaba.dubbo.rpc.config.RegistryConfig;
import com.alibaba.dubbo.rpc.config.ProviderConfig;
import com.alibaba.dubbo.rpc.config.ServiceConfig;
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;
// 服务实现
XxxService xxxService = new XxxServiceImpl();
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("xxx");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion("1.0.0");
// 暴露及注册服务
service.export();
消费者
import com.alibaba.dubbo.rpc.config.ApplicationConfig;
import com.alibaba.dubbo.rpc.config.RegistryConfig;
import com.alibaba.dubbo.rpc.config.ConsumerConfig;
import com.alibaba.dubbo.rpc.config.ReferenceConfig;
import com.xxx.XxxService;
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(XxxService.class);
reference.setVersion("1.0.0");
// 和本地bean一样使用xxxService
XxxService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
两个问题:
如果需要忽略掉注册中心呢?省掉reference.setRegistry(registry);
就可以?
目前支持js可以直接调用到协议吗?
稍后学习duboo的过程中注意下。
方法级设置
...
// 方法级配置
List<MethodConfig> methods = new ArrayList<MethodConfig>();
MethodConfig method = new MethodConfig();
method.setName("createXxx");
method.setTimeout(10000);
method.setRetries(0);
methods.add(method);
// 引用远程服务
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setMethods(methods); // 设置方法级配置
点对点直连
...
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
// 如果点对点直连,可以用reference.setUrl()指定目标地址,设置url后将绕过注册中心,
// 其中,协议对应provider.setProtocol()的值,端口对应provider.setPort()的值,
// 路径对应service.setPath()的值,如果未设置path,缺省path为接口名
reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService");
...
示例提供者安装
安装
wget http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-demo-provider/2.4.1/dubbo-demo-provider-2.4.1-assembly.tar.gz
tar zxvf dubbo-demo-provider-2.4.1-assembly.tar.gz
cd dubbo-demo-provider-2.4.1
http://code.alibabatech.com外网应该连不上。Github源代码里应该都有。
看完文档先,然后跑起来。
管理
vi conf/dubbo.properties
./bin/start.sh
./bin/stop.sh
./bin/restart.sh
./bin/start.sh debug
#系统状态
./bin/dump.sh
./bin/server.sh start
./bin/server.sh stop
./bin/server.sh restart
./bin/server.sh debug
./bin/server.sh dump
tail -f logs/stdout.log
telnet 127.0.0.1 20880
help
echo status | nc -i 127.0.0.1 208800
消费者安装方式类似。
服务提供者
- 定义接口。
- 实现服务。
- 在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: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="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
首先,给应用起个名字。
其次,在服务中心注册。
然后,定义协议。
再然后,暴露服务。
最后,定义服务。
Spring启动后,服务就ready for了。
Spring配置文件的路径,是这样的:”http://10.20.160.198/wiki/display/dubbo/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: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="multicast://224.5.6.7:1234" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
</beans>
首先,还是起个名字。
其次,注册中心。
然后,引用服务。
依赖
jdk 1.5以上。
可以通过mvn dependency:tree > dep.log命令分析。
缺省情况下依赖:
log4j:jar:1.2.16
javassist:jar:3.15.0-GA
spring:jar:2.5.6.SEC03
commons-logging:jar:1.1.1
netty:jar:3.2.5.Final(或mina.jar或grizzly.jar)
稍后关注哪里用到了javassist,效率是否针对性优化。
成熟度
考虑哪些功能单独编译,拿出来用。
这部分是:功能清单,可用性清单。含金量太高不敢看。
配置
有个configuration relation图画得很好!
- 服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心。
- 引用配置,用于创建一个远程服务代理,一个引用可以指向多个注册中心。
- 协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
- 应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
- 模块配置,用于配置当前模块信息,可选。
- 注册中心配置,用于配置连接注册中心相关信息。
- 监控中心配置,用于配置连接监控中心相关信息,可选。
- 提供方的缺省值,当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值,可选。
- 消费方缺省配置,当ReferenceConfig某属性没有配置时,采用此缺省值,可选。
- 方法配置,用于ServiceConfig和ReferenceConfig指定方法级的配置信息。
- 用于指定方法参数配置。
配置之间配置存在覆盖的情况,按照特定的优先级。
方法级优先,接口级次之,全局配置再次之。
如果级别一样,则消费方优先,提供方次之。
可以配置timeout、retries、loadbalance、actives等属性。
注解配置
生产者
import com.alibaba.dubbo.config.annotation.Service;
@Service(version="1.0.0")
public class FooServiceImpl implements FooService {
// ......
}
<!-- 公共信息,也可以用dubbo.properties配置 -->
<dubbo:application name="annotation-provider" />
<dubbo:registry address="127.0.0.1:4548" />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.foo.bar.service" />
消费者
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class BarAction {
@Reference(version="1.0.0")
private FooService fooService;
}
<!-- 公共信息,也可以用dubbo.properties配置 -->
<dubbo:application name="annotation-consumer" />
<dubbo:registry address="127.0.0.1:4548" />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.foo.bar.action" />
也可以:
<dubbo:annotation />
<context:component-scan base-package="com.foo.bar.service">
<context:include-filter type="annotation" expression="com.alibaba.dubbo.config.annotation.Service" />
</context:component-scan>
API
方法级
// 方法级配置
List<MethodConfig> methods = new ArrayList<MethodConfig>();
MethodConfig method = new MethodConfig();
method.setName("createXxx");
method.setTimeout(10000);
method.setRetries(0);
methods.add(method);
// 引用远程服务
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
...
reference.setMethods(methods); // 设置方法级配置
…
点对点直连
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
// 如果点对点直连,可以用reference.setUrl()指定目标地址,设置url后将绕过注册中心,
// 其中,协议对应provider.setProtocol()的值,端口对应provider.setPort()的值,
// 路径对应service.setPath()的值,如果未设置path,缺省path为接口名
reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService");
示例
启动时检查。
Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check=true。
如果你的Spring容器是懒加载的,或者通过API编程延迟引用服务,请关闭check,否则服务临时不可用时,会抛出异常,拿到null引用,如果check=false,总是会返回引用,当服务恢复时,能自动连上。
关闭某个服务的启动时检查:(没有提供者时报错)
关闭所有服务的启动时检查:(没有提供者时报错)
关闭注册中心启动时检查:(注册订阅失败时报错)
也可以用dubbo.properties配置:
dubbo.properties
dubbo.reference.com.foo.BarService.check=false
dubbo.reference.check=false
dubbo.consumer.check=false
dubbo.registry.check=false
引用缺省是延迟初始化的,只有引用被注入到其它Bean,或被getBean()获取,才会初始化。
如果需要饥饿加载,即没有人引用也立即生成动态代理,可以配置: