dubbo框架标签使用之直连服务提供者、只订阅、只注册、静态服务、多协议暴露、服务分组、多版本、结果缓存等

直连提供者

在开发及测试环境下,经常需要绕过注册中心,只测试服务提供者,需要点对点直连将以服务接口为单位,忽略注册中心的提供者列表,A接口配置点对点,不影响B接口从注册中心获取列表。

dubbo

只订阅

为了方便开测试同用一个注册中心时,服务提供者不注册服务,消费者订阅消息通过直连方式测试服务。
<dubbo:registry address="10.20.153.10:9090" register="false" />或
<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:registry id="hzRegistry" address="10.20.153.10:9090" />
<dubbo:registry id="qdRegistry" address="10.20.141.150:9090?subscribe=false" />

dubbo静态服务

如果希望人工管理服务提供者的上线和下线,可以将注册中心标识为非动态管理模式。
<dubbo:registry address="10.20.141.150:9090" dynamic="false" />
或
<dubbo:registry address="10.20.141.150:9090?dynamic=false" />

dubbo多协议暴露服务

Dubbo允许配置多协议,在不同服务上支持不同协议,或者同一服务同事支持多种协议
<!-- 多协议配置 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:protocol name="rmi" port="1099" />
    <!-- 使用dubbo协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
    <!-- 使用rmi协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" /> 
或
<!-- 多协议配置 -->
    <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" />

dubbo多注册中心

Dubbo支持统一服务向多注册中心同时注册,或者不同服务分别注册到不同的注册中心去,甚至同时引用注册在不同注册中心上的同名服务。
多注册中心注册
<!-- 多注册中心配置 -->
    <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" />

不同服务使用不同注册中心
<!-- 多注册中心配置 -->
    <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" />

多注册中心引用
<!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 引用中文站服务 -->
    <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" />

如果临时需要连接两个不同的注册中心,使用竖号分隔多个不同注册中心地址
<!-- 多注册中心配置,竖号分隔表示同时连接多个不同注册中心,同一注册中心的多个集群地址用逗号分隔 -->
    <dubbo:registry address="10.20.141.150:9090|10.20.154.177:9010" />
    <!-- 引用服务 -->
    <dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" />

dubbo服务分组

当一个接口有多种实现,可以用group区分
服务方
<dubbo:service group="feedback" interface="com.xxx.IndexService" />
<dubbo:service group="member" interface="com.xxx.IndexService" />

引用方
<dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" />
<dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndexService" />

或
<dubbo:reference id="barService" interface="com.foo.BarService" group="*" />

dubbo多版本

当一个接口实现,出现不兼容升级时,可以用版本号过滤,不同版本号服务间不引用
<dubbo:service interface="com.foo.BarService" version="2.0.0" />

<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />

dubbo结果缓存

用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量
缓存类型有三种
.lru 基于最近最少使用原则删除多余缓存,保存最热的数据被缓存。
.threadlocal 当前线程缓存,如渲染页面
.jcache
<dubbo:reference interface="com.foo.BarService" cache="lru" />

或
<dubbo:reference interface="com.foo.BarService">
    <dubbo:method name="findBar" cache="lru" />
</dubbo:reference>
示例代码:https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-cache

其他dubbo标签

dubbo接口分组聚合
http://dubbo.apache.org/zh-cn/docs/user/demos/group-merger.html
 
dubbo参数验证
http://dubbo.apache.org/zh-cn/docs/user/demos/parameter-validation.html
 
dubbo回声测试,检测服务是否可以正常使用,这个应该用的较少。
 
dubbo消费者异步调用
http://dubbo.apache.org/zh-cn/docs/user/demos/async-call.html
 
dubbo提供者异步执行(采用servlet3.0的异步调用特性)
http://dubbo.apache.org/zh-cn/docs/user/demos/async-execute-on-provider.html
 
dubbo参数回调
http://dubbo.apache.org/zh-cn/docs/user/demos/callback-parameter.html
 
dubbo事件通知(监控服务调用前、后等)
http://dubbo.apache.org/zh-cn/docs/user/demos/events-notify.html
 
dubbo本地存根(提供方想在客户端做部分逻辑)
http://dubbo.apache.org/zh-cn/docs/user/demos/local-stub.html
 
dubbo本地伪装(通常用于服务降级)
http://dubbo.apache.org/zh-cn/docs/user/demos/local-mock.html
 
dubbo延时暴露(为服务的预热预留时间)
http://dubbo.apache.org/zh-cn/docs/user/demos/delay-publish.html
 
dubbo延迟连接(延迟连接用于减少长连接数。当调用发起时,在创建长连接)
<dubbo:protocol name="dubbo" lazy="true" />

dubbo粘滞连接(用于有状态服务,尽可能让客户端总会向同一提供者发起调用,除非挂了,才连接另一台)
<dubbo:reference id="xxxService" interface="com.xxx.XxxService" sticky="true" />

dubbo令牌验证(在注册中心控制权限,已决定是否下发令牌给消费者,防止消费者绕过注册中心访问提供者)
http://dubbo.apache.org/zh-cn/docs/user/demos/token-authorization.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值