Dubbo基于Javassist实现动态代理分析

1
2
3
4
5
org.apache.dubbo.common.bytecode.Proxy	//生成代理类
org.apache.dubbo.common.bytecode.ClassGenerator //基于javassist封装

org.apache.dubbo.common.utils.ClassHelper; //工具类
org.apache.dubbo.common.utils.ReflectUtils; //工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// create ProxyInstance class.
//生成代理类的实例
String pcn = pkg + ".proxy" + id;
ccp.setClassName(pcn);
ccp.addField("public static java.lang.reflect.Method[] methods;");
ccp.addField("private " + InvocationHandler.class.getName() + " handler;");
ccp.addConstructor(Modifier.PUBLIC, new Class<?>[]{InvocationHandler.class}, new Class<?>[0], "handler=$1;");
ccp.addDefaultConstructor();
Class<?> clazz = ccp.toClass();
clazz.getField("methods").set(null, methods.toArray(new Method[0]));

// create Proxy class.
//生成代理类,通过调用newInstance()方法获取实例
String fcn = Proxy.class.getName() + id;
ccm = ClassGenerator.newInstance(cl);
ccm.setClassName(fcn);
ccm.addDefaultConstructor();
ccm.setSuperClass(Proxy.class);
ccm.addMethod("public Object newInstance(" + InvocationHandler.class.getName() + " h){ return new " + pcn + "($1); }");
Class<?> pc = ccm.toClass();
proxy = (Proxy) pc.newInstance();

基于Spring Cloud搭建微服务架构完整示例

ddd

Netty数据容器---ByteBuf

ByteBuf工作原理

ByteBuf维护了两个不同的索引:一个用于读取,一个用于写入。当从ByteBuf读取数据时,它的readerINdex将会递增已被读取的字节数。同样的,当你写入ByteBuf的时候,它的writerIndex也会被递增。

上图中表示的是一个读索引和写索引都设置为0的16字节ByteBuf,若果试图访问超出writerIndex范围的数据将会触发一个IndexOutOfBoundsException异常。

Netty内置传输方式

Netty中内置的传输方式主要包括:NIO、Epoll、OIO、Local和Embedded等方式,总结如下:

名称包名描述
NIOio.netty.channel.socket.io使用java.nio.channels包作为基础,基于选择器的方式
Epollio.netty.channel.epoll由JNI驱动的epoll()和非阻塞IO,这种传输只有Linux才支持,比NIO传输速度更快,而且是完全非阻塞的
OIOio.netty.channel.socket.oio使用java.net包作为基础
Localio.netty.channel.local可以在VM内部通过管道进行通信的本地传输
Embeddedio.netty.channel.socket.embeddedEmbedded 传输,允许使用 ChannelHandler 而又不需要一个真正的基于网络的传输,测试ChannelHandler实现的时候非常有用

Netty组件介绍

下面将介绍Netty中所包含的各种组件,主要包括:Channel、EventLoop、ChannelFuture、ChannelHandler和ChannelPipeline等。

Channel、EventLoop和ChannelFuture

Channel-EventLoop-ChannelFuture

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×