dubbo服務(wù)引用創(chuàng)建Invoker代理對象
創(chuàng)建Invoker代理
ReferenceConfig#createProxy方法的結(jié)尾處,將FailoverClusterInvoker創(chuàng)建DemoService接口的動態(tài)代理。
proxyFactory.getProxy(invoker);
proxyFactory也是一個帶有Adaptive注解方法的SPI類。
默認(rèn)實(shí)現(xiàn)JavassistProxyFactory
@SPI("javassist")
public interface ProxyFactory {
/**
* create proxy.
*
* @param invoker
* @return proxy
*/
@Adaptive({Constants.PROXY_KEY})
<T> T getProxy(Invoker<T> invoker) throws RpcException;
/**
* create invoker.
*
* @param <T>
* @param proxy
* @param type
* @param url
* @return invoker
*/
@Adaptive({Constants.PROXY_KEY})
<T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) throws RpcException;
}在JavassistProxyFactory#getProxy中
這個地方生成代理的方法非常類似Java的原生的動態(tài)代理java.lang.reflect.Proxy,大家感興趣的可以自己去看一下,這個地方暫不繼續(xù)寫下去了。
InvokerInvocationHandler
我們主要看一下InvokerInvocationHandler。
這樣在執(zhí)行接口的方法時(shí),將方法名與入?yún)?gòu)造成一個RpcInvocation作為入?yún)?,傳遞到Invoker的invoke函數(shù)中去。
public class InvokerInvocationHandler implements InvocationHandler {
private final Invoker<?> invoker;
public InvokerInvocationHandler(Invoker<?> handler) {
this.invoker = handler;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
if (method.getDeclaringClass() == Object.class) {
return method.invoke(invoker, args);
}
if ("toString".equals(methodName) && parameterTypes.length == 0) {
return invoker.toString();
}
if ("hashCode".equals(methodName) && parameterTypes.length == 0) {
return invoker.hashCode();
}
if ("equals".equals(methodName) && parameterTypes.length == 1) {
return invoker.equals(args[0]);
}
return invoker.invoke(new RpcInvocation(method, args)).recreate();
}
}總結(jié)
我們都說,使用RPC框架時(shí),調(diào)用外部服務(wù)就像調(diào)用本地服務(wù)一樣方便,那么如何實(shí)現(xiàn)服務(wù)接口的注入的呢?本文真是從這個角度出發(fā),講解了dubbo服務(wù)引用時(shí),是怎么注入接口的,并講解了dubbo是通過Invoker調(diào)用外部服務(wù)的,更多關(guān)于dubbo創(chuàng)建Invoker代理對象的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Docker部署ElasticSearch和ElasticSearch-Head的實(shí)現(xiàn)
這篇文章主要介紹了Docker部署ElasticSearch和ElasticSearch-Head的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Docker搭建Skywalking的實(shí)現(xiàn)示例
本文主要介紹了Docker搭建Skywalking的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
ubuntu vps安裝docker報(bào)錯:Cannot connect to t
這篇文章主要介紹了解決ubuntu vps安裝docker時(shí)報(bào)錯:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.問題的相關(guān)資料,文中介紹非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04
Docker鏡像+nginx 部署 vue 項(xiàng)目的方法
這篇文章主要介紹了Docker鏡像+nginx 部署 vue 項(xiàng)目的方法,幫助大家更好的使用docke鏡像,感興趣的朋友可以了解下2020-10-10
docker搭建redis哨兵集群并且整合springboot的實(shí)現(xiàn)
本文主要介紹了docker搭建redis哨兵集群并且整合springboot的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
在Docker中安裝Elasticsearch7.6.2的教程
這篇文章主要介紹了在Docker中安裝Elasticsearch7.6.2的教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
使用Portainer部署Docker容器的項(xiàng)目實(shí)踐
這篇文章主要介紹了使用Portainer部署Docker容器的項(xiàng)目實(shí)踐,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

