Dubbo配置详解

基本配置
服务端    
<!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="storeServer"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20889"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.enjoy.service.OrderService" ref="orderService">
    </dubbo:service>

    <bean id="orderService" class="com.enjoy.service.impl.OrderServiceImpl"/>
客户端   
 <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="enjoyStore"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://106.12.204.43:2181"/>
    <dubbo:consumer check="false"/>
    <dubbo:reference id="orderService" interface="com.enjoy.service.OrderService">
    </dubbo:reference>

 

超时配置(客户端、服务端)

timeout,如果和服务端同时设置,则以客户端为准。自己做测试的时候,服务端配置100超时,客户端配置9999,接口耗时500,这种情况下接口可以正常调通

    <dubbo:reference id="orderService" interface="com.service.OrderService"
                     timeout="2000" >
    </dubbo:reference>

集群容错(客户端)

    <dubbo:reference id="orderService" interface="com.service.OrderService"
                     cluster="failfast" >
    </dubbo:reference>
cluster:集群容错机制 
failover失败自动切换,自动重试其它服务器(默认)retries设置重试次数(不含第一次),超过retries次就不会再尝试请求其它服务器适合读
failfast快速失败,立即报错 适合写
failsafe出现异常时,直接忽略 适合日志类
failback失败自动恢复,记录失败请求,定时重发  
forking并行调用多个服务器,只要有一个成功即返回forks 设置最大并行数 
Broadcast广播逐个调用所有请求,任意一个报错则报错  

 

负载均衡(客户端)

    <dubbo:reference id="orderService" interface="com.service.OrderService"
                      loadbalance="roundrobin">
    </dubbo:reference>
loadbalance:负载均衡
random按权重随机
roundrobin轮询
leastactive最少活跃调用数,相同则随机
consistentHash一致性hash,相同参数请求总是发到同一提供者

声明式缓存(客户端、服务端)

     参数/返回:key/value的形式

    <dubbo:service interface="com.service.OrderService" ref="orderService" protocol="dubbo">
        <dubbo:method name="getDetail" cache="lru" />
    </dubbo:service>
cache:缓存
lru基于最少使用原则删除多余缓存
threadlocal当前线程缓存

异步调用

  也可以配置在具体方法上

    <dubbo:reference id="orderService" async="true" interface="com.service.OrderService">
    </dubbo:reference>

    <dubbo:reference id="userService" async="true" interface="com.service.UserService" />

可以看到,没有立即返回值。

回调

#onreturn 回调方法。 onthrow异常方法
<dubbo:reference id="orderService" interface="com.service.OrderService">
        <dubbo:method name="getDetail" async="true" onreturn="callback.tt" onthrow="callback.onEx"/>
    </dubbo:reference>


// 回调类
public class Callback {
    // 第一个参数是返回值, 第二个参数是入参
    public void tt(String returnStr, String id) {
        System.out.println(returnStr +"--- "+ "id");
    }
    // 第一个参数是返回值,第二个是入参
    public void onEx(Throwable e, String id) {
        System.out.println(e.getMessage() +"--- "+ "id");
    }
}

泛化调用

    当A项目引用B项目接口,但是知道接口路径名称的时候,可以通过泛化调用。

将想要调用的generic设为true
<dubbo:reference id="testService" interface="com.enjoy.service.impl.TestService"  generic="true"/>

//客户端调用方式
public class IndexController implements ApplicationContextAware{
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public String index() {
        // gg是方法名, 后面是参数类型及参数值
        GenericService genericService = (GenericService) context.getBean("testService");
        Object gg = genericService.$invoke("gg", new String[]{"java.lang.String"}, new Object[]{"name"});
        System.out.println(gg);
        return "index";
    }

}

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值