After the remote service, the client usually only has the interface, and the implementation is all on the server side, but the provider sometimes wants to execute some logic on the client side.
Do ThreadLocal cache, verify parameters in advance, forge fault-tolerant data after call failure, etc. At this time, you need to bring a Stub in the API, and the client generates a Proxy instance, which will pass the Proxy to the Stub through the constructor 1, and then pass the The Stub is exposed to the user, and the Stub can decide whether to call the Proxy.
<dubbo:consumer interface="com.foo.BarService" stub="true" />
or
<dubbo:consumer interface="com.foo.BarService" stub="com.foo.BarServiceStub" />
package com.foo;
public class BarServiceStub implements BarService {
private final BarService barService;
// The constructor passes in the real remote proxy object
public BarServiceStub(BarService barService){
this. barService = barService;
}
public String sayHello(String name) {
// This code is executed on the client side, you can do ThreadLocal local cache on the client side, or pre-verify whether the parameters are legal, etc.
try {
return barService.sayHello(name);
} catch (Exception e) {
// You are fault tolerant and can do any AOP interception
return "fault tolerance data";
}
}
}