当前位置: 编码机 >> 编码机介绍 >> Netty如何做到单机秒级接收35
-前言-
单纯netty结合protostuff进行rpc对象传输的demo网上有很多,大部分都是一个模子刻出来的,一开始我也是抄了一个,本地测试畅通无阻,未发生任何异常。
部署预发环境,进行压测后,问题巨多,各种报错层出不穷。当然,压测时我用的数据量大、发送请求非常密集,单机是每秒前ms发送2万个对象,其他ms歇息,死循环发送,共计40台机器作为客户端,同时往2台nettyServer服务器发送对象,那么平均每个server每秒大概要接收40万个对象,由于后面还有业务逻辑,逻辑每秒只能处理35万实测。
对于网上的代码,进行了多次修改,反复测试,最终是达到了不报错无异常,单机秒级接收35万个对象以上,故写篇文章记录一下,文中代码会和线上逻辑保持一致。
-Protostuff序列化和反序列化-
这个没什么特殊的,网上找个工具类就好了。
引入pom:
protostuff.version1.7.2/protostuff.versiondependencygroupIdio.protostuff/groupIdartifactIdprotostuff-core/artifactIdversion{protostuff.version}/version/dependencydependencygroupIdio.protostuff/groupIdartifactIdprotostuff-runtime/artifactIdversion{protostuff.version}/version/dependency
publicclassProtostuffUtils{/***避免每次序列化都重新申请Buffer空间*这句话在实际生产上没有意义,耗时减少的极小,但高并发下,如果还用这个buffer,会报异常说buffer还没清空,就又被使用了*///privatestaticLinkedBufferbuffer=LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);/***缓存Schema*/privatestaticMapClass?,Schema?schemaCache=newConcurrentHashMap();/***序列化方法,把指定对象序列化成字节数组**
paramobj*paramT*return*/SuppressWarnings("unchecked")publicstaticTbyte[]serialize(Tobj){ClassTclazz=(ClassT)obj.getClass();SchemaTschema=getSchema(clazz);LinkedBufferbuffer=LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);byte[]data;try{data=ProtobufIOUtil.toByteArray(obj,schema,buffer);//data=ProtostuffIOUtil.toByteArray(obj,schema,buffer);}finally{buffer.clear();}returndata;}/***反序列化方法,将字节数组反序列化成指定Class类型**paramdata*paramclazz*paramT*return*/publicstaticTTdeserialize(byte[]data,ClassTclazz){SchemaTschema=getSchema(clazz);Tobj=schema.newMessage();ProtobufIOUtil.mergeFrom(data,obj,schema);//ProtostuffIOUtil.mergeFrom(data,obj,schema);returnobj;}SuppressWarnings("unchecked")privatestaticTSchemaTgetSchema(ClassTclazz){SchemaTschema=(SchemaT)schemaCache.get(clazz);if(Objects.isNull(schema)){//这个schema通过RuntimeSchema进行懒创建并缓存//所以可以一直调用RuntimeSchema.getSchema(),这个方法是线程安全的schema=RuntimeSchema.getSchema(clazz);if(Objects.nonNull(schema)){schemaCache.put(clazz,schema);}}returnschema;}}此处有坑,就是最上面大部分网上代码都是用了static的buffer。在单线程情况下没有问题。在多线程情况下,非常容易出现buffer一次使用后尚未被clear,就再次被另一个线程使用,会抛异常。而所谓的避免每次都申请buffer空间,实测性能影响极其微小。
另里面两次ProtostuffIOUtil都改成了ProtobufIOUtil,因为也是出过异常,修改后未见有异常。
-自定义序列化方式-
解码器decoder:
import
转载请注明:http://www.aideyishus.com/lkyy/372.html