In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the method of Wrapper.getWrapper generation agent of JavaAssist in Dubbo". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of Wrapper.getWrapper generation agent of JavaAssist in Dubbo".
1. GetWrapper method entry private void exportLocal (URL url) {if (! Constants.LOCAL_PROTOCOL.equalsIgnoreCase (url.getProtocol () {URL local = URL.valueOf (url.toFullString ()) .setProtocol (Constants.LOCAL_PROTOCOL) .setHost (LOCALHOST) .setPort (0); ServiceClassHolder.getInstance () .pushServiceClass (getServiceClass (ref)) / / the getInvoker method of proxyFactory will be called here. ProxyFactory is an adaptive extension object, and the default is JavassistProxyFactory / / so here you will go to its getInvoker method Exporter exporter = protocol.export (proxyFactory.getInvoker (ref, (Class) interfaceClass, local); exporters.add (exporter) }} / / Let's take a look at the specific parameters passed in when calling getInvoker, as shown in the following figure public Invoker getInvoker (T proxy, Class type, URL url) {final Wrapper wrapper = Wrapper.getWrapper (proxy.getClass (). GetName (). IndexOf ('$')
< 0 ? proxy.getClass() : type); return new AbstractProxyInvoker(proxy, type, url) { @Override protected Object doInvoke(T proxy, String methodName,Class[] parameterTypes,Object[] arguments) throws Throwable { return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments); } };} 从图中可以看出,传进来的proxy是DemoServiceImpl的一个实例对象,而type是DemoService接口的class对象,可以猜测下面的Wrapper.getWrapper方法就是为该实例对象生成代理对象。下面具体分析。 2、生成Wrapper类public static Wrapper getWrapper(Class c) { while (ClassGenerator.isDynamicClass(c)) c = c.getSuperclass(); if (c == Object.class) return OBJECT_WRAPPER; // 缓存作用 Wrapper ret = WRAPPER_MAP.get(c); if (ret == null) { // 生成Wrapper类,这边逻辑比较难懂,下面先通过arthas生成具体的Wrapper类观察一下 ret = makeWrapper(c); WRAPPER_MAP.put(c, ret); } return ret;} 上述通过makeWrapper(c)方法会生成具体的Wrapper类,这里面代码比较长,它是通过javassist构建 Class。下面先通过阿里开源的arthas工具查看它生成的Wrapper类到底是什么? Arthas 用户文档。首先启动Provider服务提供者程序,将断点放置到ret = makerWrapper(c)这里,确定这里的c是DemoServiceImpl,然后运行程序至下一步,观察ret的名字。如下图 可以发现生成的Wrapper类是叫Wrapper1,然后放开断点,让程序走完,服务提供者处于运行状态。下图通过Arthas查看生成的Wrapper1到底是什么? 首先下载它的jar包,下载地址。然后打开cmd,执行以下命令。注意2是对应Provider程序的序号,需要和你自己程序对应。输入2后可以发现连上了Arthas。然后使用sc命令查找Wrapper1具体对应的是哪个class文件,最后使用jad命令反编译,查看Wrapper1类对应的Java文件具体是什么?Finally, we can get the Java file for Wrapper1, as follows
Package com.alibaba.dubbo.common.bytecode;import com.alibaba.dubbo.common.bytecode.ClassGenerator;import com.alibaba.dubbo.common.bytecode.NoSuchMethodException;import com.alibaba.dubbo.common.bytecode.NoSuchPropertyException;import com.alibaba.dubbo.common.bytecode.Wrapper;import com.alibaba.dubbo.demo.provider.DemoServiceImpl;import java.lang.reflect.InvocationTargetException;import java.util.Map;public class Wrapper1 extends Wrapper implements ClassGenerator.DC {public static String [] pns; public static Map pts; public static String [] mns Public static String [] dmns; public static Class [] mts0; public String [] getPropertyNames () {return pns;} public boolean hasProperty (String string) {return pts.containsKey (string);} public Class getPropertyType (String string) {return (Class) pts.get (string);} public String [] getMethodNames () {return mns } public String [] getDeclaredMethodNames () {return dmns;} public void setPropertyValue (Object object, String string, Object object2) {try {DemoServiceImpl demoServiceImpl = (DemoServiceImpl) object;} catch (Throwable throwable) {throw new IllegalArgumentException (throwable) } throw new NoSuchPropertyException (new StringBuffer (). Append ("Not found property\") .append (string) .append ("\" filed or setter method in class com.alibaba.dubbo.demo.provider.DemoServiceImpl. ") .toString ();} public Object getPropertyValue (Object object, String string) {try {DemoServiceImpl demoServiceImpl = (DemoServiceImpl) object } catch (Throwable throwable) {throw new IllegalArgumentException (throwable);} throw new NoSuchPropertyException (new StringBuffer (). Append ("Not found property\") .append (string) .append ("\" filed or setter method in class com.alibaba.dubbo.demo.provider.DemoServiceImpl. ") .toString ()) } / / this is the invokerMethod method of Wrapper object public Object invokeMethod (Object object, String string, Class [] arrclass, Object [] arrobject) throws InvocationTargetException {DemoServiceImpl demoServiceImpl; try {demoServiceImpl = (DemoServiceImpl) object;} catch (Throwable throwable) {throw new IllegalArgumentException (throwable) } try {if ("sayHello" .equals (string) & & arrclass.length = = 1) {/ / call the sayHello method of the DemoServiceImpl instance object and return the result to return demoServiceImpl.sayHello ((String) arrobject [0]);}} catch (Throwable throwable) {throw new InvocationTargetException (throwable) } throw new NoSuchMethodException (new StringBuffer (). Append ("Not found method\") .append (string) .append ("\" in class com.alibaba.dubbo.demo.provider.DemoServiceImpl. ") .toString ();}}
Now we can review the getInvoker method in JavassistProxyFactory, which first generates a Wrapper instance object, which is actually a proxy object of DemoServiceImpl, with an internal invokeMethod method. An anonymous Invoker instance is generated in the getInvoker method. When the doInvoke method of the Invoker is called externally, the wrapper.invokeMethod method will eventually be called, and inside the wrapper.invokerMethod is the target object called, that is, the sayHello method of DemoServiceImpl.
Public Invoker getInvoker (T proxy, Class type, URL url) {final Wrapper wrapper = Wrapper.getWrapper (proxy.getClass (). GetName (). IndexOf ('$') < 0? Proxy.getClass (): type); return new AbstractProxyInvoker (proxy, type, url) {@ Override protected Object doInvoke (T proxy, String methodName,Class [] parameterTypes,Object [] arguments) throws Throwable {return wrapper.invokeMethod (proxy, methodName, parameterTypes, arguments) };} 3. MakeWrapper method analysis
The makeWrapper method is to create the class file of the above Wrapper. When you read the code, it is easier to understand. If you don't analyze it, you can refer to the service export.
Thank you for your reading, the above is the content of "the method of Wrapper.getWrapper generation agent of JavaAssist in Dubbo". After the study of this article, I believe you have a deeper understanding of the method of generating agent of JavaAssist in Dubbo, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.