基本配置
服务端
<!-- 提供方应用信息,用于计算依赖关系 -->
<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";
}
}