• Dubbo特性


    dubbo.properties

    Dubbo 将自动加载 classpath 根目录下的dubbo.properties,可以通过JVM启动参数 -Ddubbo.properties.file=xxx.properties 改变缺省配置位置。

    启动时检查

    注意:是在消费端进行检查服务,格式如下,如果当前服务没有启动,就会报错

    <dubbo:reference id="sampleService" check="true" interface="bhz.dubbo.sample.provider.SampleService" />
    
    

    关闭所有服务的启动时检查 (没有提供者时报错)

    <dubbo:consumer check="false" />
    

    关闭注册中心启动时检查 (注册订阅失败时报错)

    <dubbo:registry check="false" />
    

    通过 dubbo.properties

    dubbo.reference.bhz.dubbo.sample.provider.SampleService.check=false
    dubbo.reference.check=false
    dubbo.consumer.check=false
    dubbo.registry.check=false
    

    配置的含义

    • dubbo.reference.check=false,强制改变所有 reference 的 check 值,就算配置中有声明,也会被覆盖。

    • dubbo.consumer.check=false,是设置 check 的缺省值,如果配置中有显式的声明,如:<dubbo:reference check="true"/>,不会受影响。

    • dubbo.registry.check=false,前面两个都是指订阅成功,但提供者列表是否为空是否报错,如果注册订阅失败时,也允许启动,需使用此选项,将在后台定时重试。

    集群容错 默认Failover Cluster

    在集群调用失败时,Dubbo 提供了多种容错方案,缺省为 failover 重试。

    详见 https://dubbo.gitbooks.io/dubbo-user-book/content/demos/fault-tolerent-strategy.html

    集群模式配置

    按照以下示例在服务提供方和消费方配置集群模式

    <dubbo:service cluster="failsafe" />
    
    <dubbo:reference cluster="failsafe" />
    

    直连提供者

    <dubbo:reference id="xxxService" interface="com.alibaba.xxx.XxxService" url="dubbo://localhost:20890" />
    
    java -Dcom.alibaba.xxx.XxxService=dubbo://localhost:20890
    

    只订阅

    <dubbo:registry address="10.20.153.10:9090" register="false" />
    
    

    只注册

    <dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />
    <dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />
    
    

    多协议

    Dubbo 允许配置多协议,在不同服务上支持不同协议或者同一服务上同时支持多种协议。

    不同服务在性能上适用不同协议进行传输,比如大数据用短连接协议,小数据大并发用长连接协议

    <?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="world"  />
        <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
        <!-- 多协议配置 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <dubbo:protocol name="hessian" port="8080" />
        <!-- 使用多个协议暴露服务 -->
        <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" />
    </beans>
    

    多协议暴露服务

    <?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="world"  />
        <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
        <!-- 多协议配置 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <dubbo:protocol name="hessian" port="8080" />
        <!-- 使用多个协议暴露服务 -->
        <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" protocol="dubbo,hessian" />
    </beans>
    

    多注册中心

    注册
    <?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="world"  />
        <!-- 多注册中心配置 -->
        <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
        <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
        <!-- 向多个注册中心注册 -->
        <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
    </beans>
    
    
    使用
    <?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="world"  />
        <!-- 多注册中心配置 -->
        <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
        <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
        <!-- 向中文站注册中心注册 -->
        <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
        <!-- 向国际站注册中心注册 -->
        <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
    </beans>
    
    
    <?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="world"  />
        <!-- 多注册中心配置 -->
        <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
        <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
        
        <!--用 id 区分-->
        <!-- 引用中文站服务 -->
        <dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />
        <!-- 引用国际站站服务 -->
        <dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" />
    </beans>
    
    

    服务分组

    当一个接口有多种实现时,可以用 group 区分。

    服务
    <dubbo:service group="feedback" interface="com.xxx.IndexService" />
    <dubbo:service group="member" interface="com.xxx.IndexService" />
    
    
    引用
    <!--根据id进行引用-->
    <dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" />
    <dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndewxService" />
    <!--任意组-->
    <dubbo:reference id="barService" interface="com.foo.BarService" group="*" />
    

    多版本

    就是加version=""字段

    分组聚合

    按组合并返回结果 1,比如菜单服务,接口一样,但有多种实现,用group区分,现在消费方需从每种group中调用一次返回结果,合并结果返回,这样就可以实现聚合菜单项。

    <!--搜索所有分组-->
    <dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" />
    
    <!--合并指定分组-->
    <dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" />
    
    
    <!--指定方法合并结果,其它未指定的方法,将只调用一个 Group-->
    <dubbo:reference interface="com.xxx.MenuService" group="*">
        <dubbo:method name="getMenuItems" merger="true" />
    </dubbo:service>
    
    <!--指定合并策略,缺省根据返回值类型自动匹配,如果同一类型有两个合并器时,需指定合并器的名称 2
    -->
    <dubbo:reference interface="com.xxx.MenuService" group="*">
        <dubbo:method name="getMenuItems" merger="mymerge" />
    </dubbo:service>
    
    

    参数验证

    https://dubbo.gitbooks.io/dubbo-user-book/content/demos/parameter-validation.html

    结果缓存

    结果缓存 1,用于加速热门数据的访问速度,Dubbo 提供声明式缓存,以减少用户加缓存的工作量

    缓存类型
    • lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存。
    • threadlocal 当前线程缓存,比如一个页面渲染,用到很多 portal,每个 portal 都要去查用户信息,通过线程缓存,可以减少这种多余访问。
    • jcache 与 JSR107 集成,可以桥接各种缓存实现。

    
    <dubbo:reference interface="com.foo.BarService" cache="lru" />
    
    <dubbo:reference interface="com.foo.BarService">
        <dubbo:method name="findBar" cache="lru" />
    </dubbo:reference>
    
    
    

    泛化调用

    回声测试

    // 远程服务引用
    MemberService memberService = ctx.getBean("memberService"); 
    
    EchoService echoService = (EchoService) memberService; // 强制转型为EchoService
    
    // 回声测试可用性
    String status = echoService.$echo("OK"); 
    
    assert(status.equals("OK"));
    
    

    上下文信息

    上下文中存放的是当前调用过程中所需的环境信息。所有配置信息都将转换为 URL 的参数,参见 schema 配置参考手册 中的对应URL参数一列。
    RpcContext 是一个 ThreadLocal 的临时状态记录器,当接收到 RPC 请求,或发起 RPC 请求时,RpcContext 的状态都会变化。比如:A 调 B,B 再调 C,则 B 机器上,在 B 调 C 之前,RpcContext 记录的是 A 调 B 的信息,在 B 调 C 之后,RpcContext 记录的是 B 调 C 的信息。

    服务消费方
    
    // 远程调用
    xxxService.xxx();
    // 本端是否为消费端,这里会返回true
    boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
    // 获取最后一次调用的提供方IP地址
    String serverIP = RpcContext.getContext().getRemoteHost();
    // 获取当前服务配置信息,所有配置信息都将转换为URL的参数
    String application = RpcContext.getContext().getUrl().getParameter("application");
    // 注意:每发起RPC调用,上下文状态会变化
    yyyService.yyy();
    
    
    服务提供方
    
    public class XxxServiceImpl implements XxxService {
    
        public void xxx() {
            // 本端是否为提供端,这里会返回true
            boolean isProviderSide = RpcContext.getContext().isProviderSide();
            // 获取调用方IP地址
            String clientIP = RpcContext.getContext().getRemoteHost();
            // 获取当前服务配置信息,所有配置信息都将转换为URL的参数
            String application = RpcContext.getContext().getUrl().getParameter("application");
            // 注意:每发起RPC调用,上下文状态会变化
            yyyService.yyy();
            // 此时本端变成消费端,这里会返回false
            boolean isProviderSide = RpcContext.getContext().isProviderSide();
        } 
    }
    
    

    隐式参数

    可以通过 RpcContext 上的 setAttachment 和 getAttachment 在服务消费方和提供方之间进行参数的隐式传递。 1

    在服务消费方端设置隐式参数
    // setAttachment 设置的 KV 对,在完成下面一次远程调用会被清空,即多次远程调用要多次设置。
    
    RpcContext.getContext().setAttachment("index", "1"); // 隐式传参,后面的远程调用都会隐式将这些参数发送到服务器端,类似cookie,用于框架集成,不建议常规业务使用
    
    xxxService.xxx(); // 远程调用
    
    // ...
    
    
    
    在服务提供方端获取隐式参数`
    
    
    public class XxxServiceImpl implements XxxService {
    
        public void xxx() {
            // 获取客户端隐式传入的参数,用于框架集成,不建议常规业务使用
            String index = RpcContext.getContext().getAttachment("index"); 
        }
    }
    
    

    异步调用

    消费者 consumer.xml
    
    <dubbo:reference id="fooService" interface="com.alibaba.foo.FooService">
          <dubbo:method name="findFoo" async="true" />
    </dubbo:reference>
    <dubbo:reference id="barService" interface="com.alibaba.bar.BarService">
          <dubbo:method name="findBar" async="true" />
    </dubbo:reference>
    
    
    调用代码
    
    // 此调用会立即返回null
    fooService.findFoo(fooId);
    // 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future
    Future<Foo> fooFuture = RpcContext.getContext().getFuture(); 
    
    // 此调用会立即返回null
    barService.findBar(barId);
    // 拿到调用的Future引用,当结果返回后,会被通知和设置到此Future
    Future<Bar> barFuture = RpcContext.getContext().getFuture(); 
    
    // 此时findFoo和findBar的请求同时在执行,客户端不需要启动多线程来支持并行,而是借助NIO的非阻塞完成
    
    // 如果foo已返回,直接拿到返回值,否则线程wait住,等待foo返回后,线程会被notify唤醒
    Foo foo = fooFuture.get(); 
    // 同理等待bar返回
    Bar bar = barFuture.get(); 
    
    // 如果foo需要5秒返回,bar需要6秒返回,实际只需等6秒,即可获取到foo和bar,进行接下来的处理。
    
    
    你也可以设置是否等待消息发出
    <!--sent="true" 等待消息发出,消息发送失败将抛出异常-->
    <!--sent="false" 不等待消息发出,将消息放入 IO 队列,即刻返回 -->
    
    <dubbo:method name="findFoo" async="true" sent="true" />
    
    
    
    如果你只是想异步,完全忽略返回值,可以配置 return="false",以减少 Future 对象的创建和管理成本:
    <dubbo:method name="findFoo" async="true" return="false" />
    
    

    事件通知

    在调用之前、调用之后、出现异常时,会触发 oninvoke、onreturn、onthrow 三个事件,可以配置当事件发生时,通知哪个类的哪个方法 1。

    服务提供者与消费者共享服务接口
    interface IDemoService {
        public Person get(int id);
    }
    
    
    服务提供者实现
    class NormalDemoService implements IDemoService {
        public Person get(int id) {
            return new Person(id, "charles`son", 4);
        }
    }
    ```java
    
    ###### 服务提供者配置
    
    ```xml
    
    <dubbo:application name="rpc-callback-demo" />
    <dubbo:registry address="http://10.20.160.198/wiki/display/dubbo/10.20.153.186" />
    <bean id="demoService" class="com.alibaba.dubbo.callback.implicit.NormalDemoService" />
    <dubbo:service interface="com.alibaba.dubbo.callback.implicit.IDemoService" ref="demoService" version="1.0.0" group="cn"/>
    
    
    服务消费者 Callback 接口
    interface Notify {
        public void onreturn(Person msg, Integer id);
        public void onthrow(Throwable ex, Integer id);
    }
    
    服务消费者 Callback 实现
    
    class NotifyImpl implements Notify {
        public Map<Integer, Person>    ret    = new HashMap<Integer, Person>();
        public Map<Integer, Throwable> errors = new HashMap<Integer, Throwable>();
    
        public void onreturn(Person msg, Integer id) {
            System.out.println("onreturn:" + msg);
            ret.put(id, msg);
        }
    
        public void onthrow(Throwable ex, Integer id) {
            errors.put(id, ex);
        }
    }
    
    
    服务消费者 Callback 配置
    <bean id ="demoCallback" class = "com.alibaba.dubbo.callback.implicit.NofifyImpl" />
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.callback.implicit.IDemoService" version="1.0.0" group="cn" >
          <dubbo:method name="get" async="true" onreturn = "demoCallback.onreturn" onthrow="demoCallback.onthrow" />
    </dubbo:reference>
    
    

    callback 与 async 功能正交分解,async=true 表示结果是否马上返回,onreturn 表示是否需要回调。
    两者叠加存在以下几种组合情况 2:
    异步回调模式:async=true onreturn="xxx"
    同步回调模式:async=false onreturn="xxx"
    异步无回调 :async=true
    同步无回调 :async=false

    测试代码
    IDemoService demoService = (IDemoService) context.getBean("demoService");
    NofifyImpl notify = (NofifyImpl) context.getBean("demoCallback");
    int requestId = 2;
    Person ret = demoService.get(requestId);
    Assert.assertEquals(null, ret);
    //for Test:只是用来说明callback正常被调用,业务具体实现自行决定.
    for (int i = 0; i < 10; i++) {
        if (!notify.ret.containsKey(requestId)) {
            Thread.sleep(200);
        } else {
            break;
        }
    }
    Assert.assertEquals(requestId, notify.ret.get(requestId).getId());
    
    

    并发控制

    限制 com.foo.BarService 的每个方法,服务器端并发执行(或占用线程池线程数)不能超过 10 个:

    <dubbo:service interface="com.foo.BarService" executes="10" />
    

    限制 com.foo.BarService 的 sayHello 方法,服务器端并发执行(或占用线程池线程数)不能超过 10 个:

    <dubbo:service interface="com.foo.BarService">
        <dubbo:method name="sayHello" executes="10" />
    </dubbo:service>
    

    限制 com.foo.BarService 的每个方法,每客户端并发执行(或占用连接的请求数)不能超过 10 个:

    <dubbo:service interface="com.foo.BarService" actives="10" />
    

    <dubbo:reference interface="com.foo.BarService" actives="10" />
    

    限制 com.foo.BarService 的 sayHello 方法,每客户端并发执行(或占用连接的请求数)不能超过 10 个:

    <dubbo:service interface="com.foo.BarService">
        <dubbo:method name="sayHello" actives="10" />
    </dubbo:service>
    

    <dubbo:reference interface="com.foo.BarService">
        <dubbo:method name="sayHello" actives="10" />
    </dubbo:service>
    
    如果 dubbo:servicedubbo:reference 都配了actives,dubbo:reference 优先,参见:配置的覆盖策略。

    令牌验证

    通过令牌验证在注册中心控制权限,以决定要不要下发令牌给消费者,可以防止消费者绕过注册中心访问提供者,另外通过注册中心可灵活改变授权方式,而不需修改或升级提供者

    image

    可以全局设置开启令牌验证
    <!--随机token令牌,使用UUID生成-->
    <dubbo:provider interface="com.foo.BarService" token="true" />
    
    <!--固定token令牌,相当于密码-->
    <dubbo:provider interface="com.foo.BarService" token="123456" />
    
    <!--随机token令牌,使用UUID生成-->
    <dubbo:service interface="com.foo.BarService" token="true" />
    
    <!--固定token令牌,相当于密码-->
    <dubbo:service interface="com.foo.BarService" token="123456" />
    
    <!--随机token令牌,使用UUID生成-->
    <dubbo:protocol name="dubbo" token="true" />
    
    <!--固定token令牌,相当于密码-->
    <dubbo:protocol name="dubbo" token="123456" />
    
    

    优雅停机

    设置优雅停机超时时间 缺省超时时间是 10 秒,如果超时则强制关闭。
    dubbo.service.shutdown.wait=15000
    

    日志适配

    命令行

    java -Ddubbo.application.logger=log4j
    

    在 dubbo.properties 中指定

    dubbo.application.logger=log4j
    

    在 dubbo.xml 中配置

    <dubbo:application logger="log4j" />
    

    访问日志

    将访问日志输出到当前应用的log4j日志:

    <dubbo:protocol accesslog="true" />
    
    

    将访问日志输出到指定文件:

    <dubbo:protocol accesslog="http://10.20.160.198/wiki/display/dubbo/foo/bar.log" />
    

    服务容器

    Spring Container
    • 自动加载 META-INF/spring 目录下的所有 Spring 配置。
    • 配置 spring 配置加载位置:
    dubbo.spring.config=classpath*:META-INF/spring/*.xml
    
    
    Jetty Container

    启动一个内嵌 Jetty,用于汇报状态。
    配置:

    dubbo.jetty.port=8080:配置 jetty 启动端口
    dubbo.jetty.directory=/foo/bar:配置可通过 jetty 直接访问的目录,用于存放静态文件
    dubbo.jetty.page=log,status,system:配置显示的页面,缺省加载所有页面
    
    Log4j Container

    自动配置 log4j 的配置,在多进程启动时,自动给日志文件按进程分目录。
    配置:

    dubbo.log4j.file=/foo/bar.log:配置日志文件路径
    dubbo.log4j.level=WARN:配置日志级别
    dubbo.log4j.subdirectory=20880:配置日志子目录,用于多进程启动,避免冲突
    
    容器启动
    • java com.alibaba.dubbo.container.Main
    • java com.alibaba.dubbo.container.Main spring jetty log4j
    • java com.alibaba.dubbo.container.Main -Ddubbo.container=spring,jetty,log4j
    • dubbo.container=spring,jetty,log4j
  • 相关阅读:
    mybatis批量操作问题总结
    activity判断流程结束和删除流程
    mybaties controller中总会优先执行方法
    mybaties接受 对象.属性 参数
    Tomcat发布Maven项目遇到的种种异常(转:http://blog.csdn.net/zhang6622056/article/details/9772951)
    activiti5用户任务分配
    maven部署项目异常
    sql分组按条件统计count case when then
    服务器管理—DNS
    ftp服务
  • 原文地址:https://www.cnblogs.com/leihuazhe/p/7880671.html
Copyright © 2020-2023  润新知