dubbo source read -ProxyFactory (k) of the local stub StubProxyFactoryWrapper

Explanation

Wrapper can be called as soon as look at: https: //www.cnblogs.com/LQBlog/p/12470179.html#autoid-2-0-0

 

/**
 * StubProxyFactoryWrapper
 */
public class StubProxyFactoryWrapper implements ProxyFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(StubProxyFactoryWrapper.class);

    private final ProxyFactory proxyFactory;

    private Protocol protocol;

    //注意构造函数
    public StubProxyFactoryWrapper(ProxyFactory proxyFactory) {
        this.proxyFactory = proxyFactory;
    }

    public void setProtocol(Protocol protocol) {
        this.protocol = protocol;
    }

    @Override
    public <T> T getProxy(Invoker<T> invoker, boolean generic) throws RpcException {
        return proxyFactory.getProxy(invoker, generic);
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public <T> T getProxy(Invoker<T> invoker) throws RpcException {
        //获得代理对象
        T proxy = proxyFactory.getProxy(invoker);
        //如果不是泛型化
        if (GenericService.class != invoker.getInterface()) {
            // get stub configuration if not configured to obtain local (compatible with the old configuration) 
            String stub = invoker.getUrl () getParameter (Constants.STUB_KEY, invoker.getUrl () getParameter (Constants.LOCAL_KEY).);.
             // if there is configured 
            if (ConfigUtils.isNotEmpty (Stub)) {
                 // Get Interface 
                Class serviceType = <?> invoker.getInterface ();
                 // whether it is true or configured default 
                iF (ConfigUtils.isDefault (Stub)) {
                     iF (invoker.getUrl ( ) .hasParameter (Constants.STUB_KEY)) {
                         // Get the name of the interface the Stub + 
                        Stub serviceType.getName = () + "the Stub" ; 
                    }else {
                        //获取接口名字+Local
                        stub = serviceType.getName() + "Local";
                    }
                }
                try {
                    //反射获取class
                    Class<?> stubClass = ReflectUtils.forName(stub);
                    if (!serviceType.isAssignableFrom(stubClass)) {
                        throw new IllegalStateException("The stub implementation class " + stubClass.getName() + " not implement interface " + serviceType.getName());
                    }
                    try{
                         // Stub class must contain this type of constructor proxy interface 
                        the Constructor constructor = <?> ReflectUtils.findConstructor (stubClass, serviceType);
                         // create an instance of the proxy class and passing 
                        Proxy = (T) Constructor.newInstance ( new new Object [ ] Proxy {});
                         // Export-Service Stub 
                        the URL URL = invoker.getUrl ();
                         // if there is an event listener arranged dubbo.stub.event 
                        iF (url.getParameter (Constants.STUB_EVENT_KEY, Constants.DEFAULT_STUB_EVENT)) { 
                            URL= url.addParameter(Constants.STUB_EVENT_METHODS_KEY, StringUtils.join(Wrapper.getWrapper(proxy.getClass()).getDeclaredMethodNames(), ","));
                            url = url.addParameter(Constants.IS_SERVER_KEY, Boolean.FALSE.toString());
                            try {
                                export(proxy, (Class) invoker.getInterface(), url);
                            } catch (Exception e) {
                                LOGGER.error("export a stub service error.", e);
                            }
                        }
                    } catch (NoSuchMethodException e) {
                        throw new IllegalStateException("No such constructor \"public " + stubClass.getSimpleName() + "(" + serviceType.getName() + ")\" in stub implementation class " + stubClass.getName(), e);
                    }
                } catch (Throwable t) {
                    LOGGER.error("Failed to create stub implementation class " + stub + " in consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", cause: " + t.getMessage(), t);
                    eventual return proxy class is Stub//
        }
            }                }the ignore//

        
        return proxy;
    }

    @Override
    public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) throws RpcException {
        return proxyFactory.getInvoker(proxy, type, url);
    }

    private <T> Exporter<T> export(T instance, Class<T> type, URL url) {
        return protocol.export(proxyFactory.getInvoker(instance, type, url));
    }

}

 

Guess you like

Origin www.cnblogs.com/LQBlog/p/12510187.html