Local stub

After the remote service, the client is usually only interface, and achieve full server side, but there are times when the provider client also wanted to execute part of the logic, such as: do ThreadLocal caching, authentication parameters in advance, the call fails forged fault-tolerant data etc., in this case it is necessary to bring the Stub API, the client generates a Proxy instance, Proxy will pass through the constructor Stub, Stub then exposed to the user, you can decide whether to go Stub Proxy tone. Stub must have a constructor can be passed in the Proxy.

 

Local stub execution method

public class UserServiceStub implements UserService {

    private final UserService userService;
    public UserServiceStub(UserService userService) {
        // TODO Auto-generated constructor stub
        this.userService = userService;
    }
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        // TODO Auto-generated method stub
        System.out.println("调用本地存根");
        if(StringUtils.isEmpty(userId)){
            return userService.getUserAddressList(userId);
        }else{
            return null;
        }
    }
}

Profiles

<dubbo:reference interface="com.moon.user_service_gmall.service.UserService" 
        id="userService" timeout="3000" retries="3" version="*" stub="com.moon.service.UserServiceStub">
        <dubbo:method name="getUserAddressList" timeout="2000"></dubbo:method>
</dubbo:reference>

 

Guess you like

Origin www.cnblogs.com/menbo/p/11002200.html