1. 应用环境搭建
- 代码已经上传至 https://github.com/masteryourself/study-dubbo.git ,分支是
master
,工程是dubbo-2.6.5-demo
1.1 配置文件
1. pom.xml
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<!-- curator -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
2. provider.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider">
<dubbo:parameter key="qos.enable" value="false"/>
</dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:protocol name="dubbo" port="20882"/>
<dubbo:service interface="pers.masteryourself.study.dubbo.api.DemoService" ref="demoService"/>
<bean id="demoService" class="pers.masteryourself.study.dubbo.provider.DemoServiceImpl"/>
</beans>
3. consumer.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer">
<dubbo:parameter key="qos.enable" value="false"/>
</dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- failover 失败自动切换,当出现失败,重试其它服务器,默认配置 -->
<!--<dubbo:reference id="demoService" check="false" interface="pers.masteryourself.study.dubbo.api.DemoService" cluster="failover" retries="2"/>-->
<!-- failfast 快速失败,只发起一次调用,失败立即报错 -->
<dubbo:reference id="demoService" check="false" interface="pers.masteryourself.study.dubbo.api.DemoService" cluster="failfast"/>
</beans>
4. log4j.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
1.2 代码
1. DemoService
public interface DemoService {
String sayHello(String name);
}
2. DemoServiceImpl
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
URL url = RpcContext.getContext().getUrl();
String message = String.format(" protocol is %s, address is %s", url.getProtocol(), url.getAddress());
// cluster 策略演示
try {
System.out.println("调用此方法了。。。" + message);
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "你好:" + name + message;
}
}
3. Provider
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
context.start();
System.in.read();
}
}
4. Consumer
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
while (true){
try {
System.out.println(demoService.sayHello("dubbo"));
} catch (Exception e) {
System.out.println("报错了:" + e.getMessage());
}
TimeUnit.SECONDS.sleep(2);
}
// cluster 策略演示
//System.out.println(demoService.sayHello("dubbo"));
}
}
2. dubbo-admin 使用
- 代码已经上传至 https://github.com/masteryourself/dubbo-admin.git ,分支是
dubbo-admin-2.6.x
,启动类是DubboAdminApplication
2.1 服务消费者负载均衡
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
[08/12/19 11:40:55:518 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575776384364, urls: [override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin], dubbo version: 2.6.5, current host: 192.168.89.1
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
2.2 服务提供者禁用
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
[08/12/19 11:45:32:000 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575776384364, urls: [override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin, override://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&disabled=true&dynamic=false&enabled=true], dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 11:45:32:001 CST] main-EventThread INFO netty.NettyChannel: [DUBBO] Close netty channel [id: 0x02af004b, /192.168.89.1:61519 => /192.168.89.1:20880], dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 11:45:32:003 CST] DubboSharedHandler-thread-1 INFO dubbo.DubboProtocol: [DUBBO] disconnected from /192.168.89.1:20880,url:dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&codec=dubbo&dubbo=2.0.2&generic=false&heartbeat=60000&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575776289701&side=consumer×tamp=1575776384364, dubbo version: 2.6.5, current host: 192.168.89.1
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
2.3 服务消费者集群容错之 failover
1. 配置
cluster
默认是failover
,retries
默认是 2
<!-- failover 失败自动切换,当出现失败,重试其它服务器,默认配置 -->
<dubbo:reference id="demoService" check="false" interface="pers.masteryourself.study.dubbo.api.DemoService" cluster="failover" retries="2"/>
2. 服务提供者
- 服务提供者被调用了 3 次
调用此方法了。。。 protocol is dubbo, address is 192.168.89.1:20880
调用此方法了。。。 protocol is dubbo, address is 192.168.89.1:20881
调用此方法了。。。 protocol is dubbo, address is 192.168.89.1:20882
3. 服务消费者
- 服务消费者连续调用三次(前两次打印 warn 日志,第三次才真正抛出异常)
[08/12/19 11:58:50:318 CST] DubboClientHandler-192.168.89.1:20880-thread-1 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 11:58:50.318, response Response [id=0, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20880, exception=null]], channel: /192.168.89.1:61981 -> /192.168.89.1:20880, dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 11:58:51:284 CST] DubboClientHandler-192.168.89.1:20881-thread-1 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 11:58:51.284, response Response [id=1, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20881, exception=null]], channel: /192.168.89.1:61988 -> /192.168.89.1:20881, dubbo version: 2.6.5, current host: 192.168.89.1
Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method sayHello in the service pers.masteryourself.study.dubbo.api.DemoService. Tried 3 times of the providers [192.168.89.1:20882, 192.168.89.1:20881, 192.168.89.1:20880] (3/3) from the registry 127.0.0.1:2181 on the consumer 192.168.89.1 using the dubbo version 2.6.5. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20882/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=28056&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575777348168&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575777527245, cause: Waiting server-side response timeout by scan timer. start time: 2019-12-08 11:58:50.284, end time: 2019-12-08 11:58:51.299, client elapsed: 0 ms, server elapsed: 1014 ms, timeout: 1000 ms, request: Request [id=2, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:61993 -> /192.168.89.1:20882
at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:109)
at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:244)
at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:75)
at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
at com.alibaba.dubbo.common.bytecode.proxy0.sayHello(proxy0.java)
at pers.masteryourself.study.dubbo.consumer.Consumer.main(Consumer.java:27)
Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2019-12-08 11:58:50.284, end time: 2019-12-08 11:58:51.299, client elapsed: 0 ms, server elapsed: 1014 ms, timeout: 1000 ms, request: Request [id=2, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:61993 -> /192.168.89.1:20882
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.returnFromResponse(DefaultFuture.java:243)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:162)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:135)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker.doInvoke(DubboInvoker.java:95)
at com.alibaba.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:155)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:54)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:49)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:77)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:78)
... 5 more
2.4 服务消费者集群容错之 failfast
<!-- failfast 快速失败,只发起一次调用,失败立即报错 -->
<dubbo:reference id="demoService" check="false" interface="pers.masteryourself.study.dubbo.api.DemoService" cluster="failfast"/>
2.5 服务消费者服务降级之容错
-
容错即调用失败后会返回配置好的值(这里是 null)
-
容错也可以直接在动态配置里配置
报错了:Failfast invoke providers dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224 RoundRobinLoadBalance select from all providers [com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@37911f88, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@6f1c29b7, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@4d6025c5] for service pers.masteryourself.study.dubbo.api.DemoService method sayHello on consumer 192.168.89.1 use dubbo version 2.6.5, but no luck to perform the invocation. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, cause: Waiting server-side response timeout. start time: 2019-12-08 12:12:20.045, end time: 2019-12-08 12:12:21.046, client elapsed: 0 ms, server elapsed: 1001 ms, timeout: 1000 ms, request: Request [id=5, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880
[08/12/19 12:12:22:046 CST] DubboClientHandler-192.168.89.1:20880-thread-1 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 12:12:22.046, response Response [id=5, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20880, exception=null]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880, dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:12:23:358 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [override://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&mock=fail%3Areturn+null, override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin], dubbo version: 2.6.5, current host: 192.168.89.1
报错了:Failfast invoke providers dubbo://192.168.89.1:20881/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778267982&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224 RoundRobinLoadBalance select from all providers [com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@37911f88, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@6f1c29b7, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@4d6025c5] for service pers.masteryourself.study.dubbo.api.DemoService method sayHello on consumer 192.168.89.1 use dubbo version 2.6.5, but no luck to perform the invocation. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20881/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778267982&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, cause: Waiting server-side response timeout by scan timer. start time: 2019-12-08 12:12:23.046, end time: 2019-12-08 12:12:24.047, client elapsed: 0 ms, server elapsed: 1001 ms, timeout: 1000 ms, request: Request [id=6, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62338 -> /192.168.89.1:20881
[08/12/19 12:12:25:048 CST] DubboClientHandler-192.168.89.1:20881-thread-1 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 12:12:25.048, response Response [id=6, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20881, exception=null]], channel: /192.168.89.1:62338 -> /192.168.89.1:20881, dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:12:27:073 CST] main WARN wrapper.MockClusterInvoker: [DUBBO] fail-mock: sayHello fail-mock enabled , url : zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&mock=fail%3Areturn+null&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778268014&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, dubbo version: 2.6.5, current host: 192.168.89.1
com.alibaba.dubbo.rpc.RpcException: Failfast invoke providers dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&mock=fail%3Areturn+null&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224 RoundRobinLoadBalance select from all providers [com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@723e88f9, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@5f0fd5a0, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@64e7619d] for service pers.masteryourself.study.dubbo.api.DemoService method sayHello on consumer 192.168.89.1 use dubbo version 2.6.5, but no luck to perform the invocation. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&mock=fail%3Areturn+null&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, cause: Waiting server-side response timeout by scan timer. start time: 2019-12-08 12:12:26.048, end time: 2019-12-08 12:12:27.073, client elapsed: 0 ms, server elapsed: 1025 ms, timeout: 1000 ms, request: Request [id=7, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880
at com.alibaba.dubbo.rpc.cluster.support.FailfastClusterInvoker.doInvoke(FailfastClusterInvoker.java:53)
at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:244)
at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:85)
at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
at com.alibaba.dubbo.common.bytecode.proxy0.sayHello(proxy0.java)
at pers.masteryourself.study.dubbo.consumer.Consumer.main(Consumer.java:24)
Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2019-12-08 12:12:26.048, end time: 2019-12-08 12:12:27.073, client elapsed: 0 ms, server elapsed: 1025 ms, timeout: 1000 ms, request: Request [id=7, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.returnFromResponse(DefaultFuture.java:243)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:162)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:135)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker.doInvoke(DubboInvoker.java:95)
at com.alibaba.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:155)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:54)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:49)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:77)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
at com.alibaba.dubbo.rpc.cluster.support.FailfastClusterInvoker.doInvoke(FailfastClusterInvoker.java:48)
... 5 more
null
2.6 服务消费者服务降级之屏蔽
- 屏蔽即直接走降级方法,不需要考虑调用是否失败
报错了:Failfast invoke providers dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224 RoundRobinLoadBalance select from all providers [com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@3697186, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@1583741e, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@5b367418] for service pers.masteryourself.study.dubbo.api.DemoService method sayHello on consumer 192.168.89.1 use dubbo version 2.6.5, but no luck to perform the invocation. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, cause: Waiting server-side response timeout. start time: 2019-12-08 12:22:39.469, end time: 2019-12-08 12:22:40.470, client elapsed: 0 ms, server elapsed: 1001 ms, timeout: 1000 ms, request: Request [id=211, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880
[08/12/19 12:22:41:470 CST] DubboClientHandler-192.168.89.1:20880-thread-1 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 12:22:41.470, response Response [id=211, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20880, exception=null]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880, dubbo version: 2.6.5, current host: 192.168.89.1
报错了:Failfast invoke providers dubbo://192.168.89.1:20881/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778267982&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224 RoundRobinLoadBalance select from all providers [com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@3697186, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@1583741e, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@5b367418] for service pers.masteryourself.study.dubbo.api.DemoService method sayHello on consumer 192.168.89.1 use dubbo version 2.6.5, but no luck to perform the invocation. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20881/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778267982&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, cause: Waiting server-side response timeout. start time: 2019-12-08 12:22:42.470, end time: 2019-12-08 12:22:43.472, client elapsed: 0 ms, server elapsed: 1002 ms, timeout: 1000 ms, request: Request [id=212, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62338 -> /192.168.89.1:20881
[08/12/19 12:22:43:829 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin], dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:22:43:857 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&mock=force%3Areturn+%22%E5%BC%BA%E5%88%B6%E8%BF%94%E5%9B%9E%E8%BF%99%E4%B8%AA%E5%80%BC%22, override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin], dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:22:44:471 CST] DubboClientHandler-192.168.89.1:20881-thread-1 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 12:22:44.471, response Response [id=212, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20881, exception=null]], channel: /192.168.89.1:62338 -> /192.168.89.1:20881, dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:22:45:472 CST] main INFO wrapper.MockClusterInvoker: [DUBBO] force-mock: sayHello force-mock enabled , url : zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&mock=force%3Areturn+%22%E5%BC%BA%E5%88%B6%E8%BF%94%E5%9B%9E%E8%BF%99%E4%B8%AA%E5%80%BC%22&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778268014&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, dubbo version: 2.6.5, current host: 192.168.89.1
强制返回这个值
[08/12/19 12:22:47:474 CST] main INFO wrapper.MockClusterInvoker: [DUBBO] force-mock: sayHello force-mock enabled , url : zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&mock=force%3Areturn+%22%E5%BC%BA%E5%88%B6%E8%BF%94%E5%9B%9E%E8%BF%99%E4%B8%AA%E5%80%BC%22&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778268014&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, dubbo version: 2.6.5, current host: 192.168.89.1
强制返回这个值
2.7 服务消费者动态配置
-
权重调节和负载均衡的功能属于动态配置
-
由于服务提供者 sleep 了 2 秒,导致消费者一直超时,这里用动态配置添加 demo-consumer 的超时时间
报错了:Failfast invoke providers dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224 RoundRobinLoadBalance select from all providers [com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@65b104b9, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@6c4980d3, com.alibaba.dubbo.registry.integration.RegistryDirectory$InvokerDelegate@327bcebd] for service pers.masteryourself.study.dubbo.api.DemoService method sayHello on consumer 192.168.89.1 use dubbo version 2.6.5, but no luck to perform the invocation. Last error is: Invoke remote method timeout. method: sayHello, provider: dubbo://192.168.89.1:20880/pers.masteryourself.study.dubbo.api.DemoService?anyhost=true&application=demo-consumer&bean.name=pers.masteryourself.study.dubbo.api.DemoService&check=false&cluster=failfast&dubbo=2.0.2&generic=false&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false®ister.ip=192.168.89.1&remote.timestamp=1575778266928&sayHello.loadbalance=roundrobin&side=consumer×tamp=1575778324224, cause: Waiting server-side response timeout by scan timer. start time: 2019-12-08 12:31:19.169, end time: 2019-12-08 12:31:20.182, client elapsed: 1 ms, server elapsed: 1012 ms, timeout: 1000 ms, request: Request [id=295, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=sayHello, parameterTypes=[class java.lang.String], arguments=[dubbo], attachments={path=pers.masteryourself.study.dubbo.api.DemoService, interface=pers.masteryourself.study.dubbo.api.DemoService, version=0.0.0}]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880
[08/12/19 12:31:21:170 CST] DubboClientHandler-192.168.89.1:20880-thread-2 WARN support.DefaultFuture: [DUBBO] The timeout response finally returned at 2019-12-08 12:31:21.170, response Response [id=295, version=null, status=20, event=false, error=null, result=RpcResult [result=你好:dubbo protocol is dubbo, address is 192.168.89.1:20880, exception=null]], channel: /192.168.89.1:62331 -> /192.168.89.1:20880, dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:31:21:939 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin], dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:31:21:977 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [override://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=configurators&dynamic=false&enabled=true&sayHello.timeout=2500, override://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=configurators&dynamic=false&enabled=true&sayHello.loadbalance=roundrobin], dubbo version: 2.6.5, current host: 192.168.89.1
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
2.8 服务消费者路由规则
-
访问控制的功能属于路由规则
-
给 demo-consumer 应用添加一条路由规则,让它只能消费到 20880 端口提供出来的服务
你好:dubbo protocol is dubbo, address is 192.168.89.1:20881
你好:dubbo protocol is dubbo, address is 192.168.89.1:20882
[08/12/19 12:34:43:837 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [empty://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224], dubbo version: 2.6.5, current host: 192.168.89.1
[08/12/19 12:34:43:877 CST] main-EventThread INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.89.1/pers.masteryourself.study.dubbo.api.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&cluster=failfast&dubbo=2.0.2&interface=pers.masteryourself.study.dubbo.api.DemoService&methods=sayHello&pid=27312&qos.enable=false&side=consumer×tamp=1575778324224, urls: [route://0.0.0.0/pers.masteryourself.study.dubbo.api.DemoService?category=routers&dynamic=false&enabled=true&force=false&name=test1&priority=0&router=condition&rule=consumer.application+%3D+demo-consumer+%3D%3E+provider.port+%3D+20880&runtime=false], dubbo version: 2.6.5, current host: 192.168.89.1
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880
你好:dubbo protocol is dubbo, address is 192.168.89.1:20880