Netty support for protocol buffer Multi-Protocol (eight)
I. BACKGROUND
In the last blog post, the author has shown how to use protocol Buffer in Netty, but the attentive user may find that the obvious disadvantage is that our handler can only handle a particular type, and our project cannot have only one type, So how does this problem work out? Many do not say, the author directly on the code.
Two. Code implementation
2.1 The writing of the message
Syntax ="Proto2";p ackage com.rsy.netty.protobuf;option java_package="Com.rsy.netty.protobuf"; option Java_outer_classname="Datainfo"; message datas{enumDataType {PersonType=1; Dogtype=2; } Required DataType Data_type=1; oneof type_data{ Person person=2; Dog Dog=3; }}message person{required Int32 ID=1; OptionalstringName =2; enumGender {male=1; Female=2; } Optional Gender Gender=3;} Message Dog {RequiredfloatHeight =1; Optionalstringcolor =2; Optional Int64 Age=3;}
2.2 Generate Java code, no longer repeat it here.
2.3 Server-side boot code
Public classServertest { Public Static voidMain (string[] args)throwsException {eventloopgroup bossgroup=NewNioeventloopgroup (); Eventloopgroup Workergroup=NewNioeventloopgroup (); Try{serverbootstrap Serverbootstrap=NewServerbootstrap (); Serverbootstrap.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Handler (NewLogginghandler (Loglevel.info)). Childhandler (NewServerchannelinitilizer ()); Channelfuture channelfuture= Serverbootstrap.bind (8989). sync (); Channelfuture.channel (). Closefuture (). sync (); }finally{bossgroup.shutdowngracefully (); Workergroup.shutdowngracefully (); } }}
2.4 Service-side channel initialization code
Public classServerchannelinitilizerextendsChannelinitializer<socketchannel>{@Overrideprotected voidInitchannel (Socketchannel ch)throwsException {Channelpipeline pipeline=Ch.pipeline (); Pipeline.addlast ("Protobufvarint32framedecoder",NewProtobufvarint32framedecoder ()); Pipeline.addlast ("Protobufdecoder",NewProtobufdecoder (DataInfo.Datas.getDefaultInstance ())); Pipeline.addlast ("Protobufvarint32lengthfieldprepender",NewProtobufvarint32lengthfieldprepender ()); Pipeline.addlast ("Protobufencoder",NewProtobufencoder ()); Pipeline.addlast ("Serverhandler",NewServerhandler ()); }}
2.5 Service-Side handler code
Public classServerhandlerextendsSimplechannelinboundhandler<datainfo.datas>{@Overrideprotected voidChannelRead0 (Channelhandlercontext ctx, Datainfo.datas msg)throwsException {/*** Because the first person to write is the person.*/Datainfo.person P=Msg.getperson (); System.out.println (Msg.getdatatype (). toString ()); System.out.println (P.getid ()); System.out.println (P.getgender (). toString ()); System.out.println (P.getname ()); Datainfo.datas Data=DataInfo.Datas.newBuilder (). Setdatatype (Datatype.dogtype) . Setdog (DataInfo.Dog.newBuilder (). SetA GE (23). SetColor (Red). SetHeight (3.5f) . Build (); Ctx.writeandflush (data); }}
2.6 Client startup code
Public classClienttest { Public Static voidMain (string[] args)throwsException {eventloopgroup eventloopgroup=NewNioeventloopgroup (); Try{Bootstrap Bootstrap=NewBootstrap (); Bootstrap.group (Eventloopgroup). Channel (Niosocketchannel.class). Handler (NewClientchannelinitializer ()); Channelfuture channelfuture= Bootstrap.connect ("localhost", 8989). sync (); Channelfuture.channel (). Closefuture (). sync (); }finally{eventloopgroup.shutdowngracefully (); } }}
2.7 Client channel initialization code
Public classClientchannelinitializerextendsChannelinitializer<socketchannel>{@Overrideprotected voidInitchannel (Socketchannel ch)throwsException {Channelpipeline pipeline=Ch.pipeline (); Pipeline.addlast ("Protobufvarint32framedecoder",NewProtobufvarint32framedecoder ()); Pipeline.addlast ("Protobufdecoder",NewProtobufdecoder (DataInfo.Datas.getDefaultInstance ())); Pipeline.addlast ("Protobufvarint32lengthfieldprepender",NewProtobufvarint32lengthfieldprepender ()); Pipeline.addlast ("Protobufencoder",NewProtobufencoder ()); Pipeline.addlast ("ClientHandler",NewClientHandler ()); }}
2.8 Client Handler Processing code
Public classClientHandlerextendsSimplechannelinboundhandler<datainfo.datas>{@Overrideprotected voidChannelRead0 (Channelhandlercontext ctx, Datainfo.datas msg)throwsException {/*** The server writes back the dog.*/Datainfo.dog Dog=Msg.getdog (); System.out.println (Msg.getdatatype (). toString ()); System.out.println (Dog.getage ()); System.out.println (Dog.getcolor ()); System.out.println (Dog.getheight ()); } @Override Public voidChannelactive (Channelhandlercontext ctx)throwsException {datainfo.datas data=DataInfo.Datas.newBuilder (). Setdatatype (Datatype.persontype) . Setperson (DataInfo.Person.newBuilder () . SetId (23). Setgender (Gender.female). Set Name ("Zhangsan") . Build (); Ctx.writeandflush (data); }}
Three. Running
Run the service-side startup code, and then run the client startup code.
Netty support for protocol buffer Multi-Protocol (eight)