A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不二晨 金牌黑马   /  2019-4-8 14:06  /  1015 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

基本配置
服务端   
<!-- 提供方应用信息,用于计算依赖关系 -->
    <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,如果和服务端同时设置,则以客户端为准。

    <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"/>
    </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";
    }

}

---------------------
【转载,仅作分享,侵删】
作者:一只叫狗的猫
原文:https://blog.csdn.net/zgsxhdzxl/article/details/88673376
版权声明:本文为博主原创文章,转载请附上博文链接!

1 个回复

倒序浏览
奈斯,感谢分享!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马