Netty 集成google ProtoBuf 开发私有协议 之实战开发

虾米哥 阅读:621 2021-03-31 12:53:41 评论:0

本文基于Netty 集成google ProtoBuf 开发私有协议,进行实战功能开发

本文参考借鉴:https://blog.csdn.net/kunfeisang5551/article/details/107957256

第一步:新建自定义协议文本对象User.proto

syntax = "proto3"; 
//生成的包名,此处根据实际来修改 
option java_package = "com.netty.protobuf"; 
//类名 
option java_outer_classname="UserInfo"; 
 
message UserMsg{ 
  int32 id=1; 
  string name=2; 
  int32 age=3; 
  int32 state=4; 
} 

第二步:Java 项目集成Google ProtoBuf 功能添加如下jar 包依赖 

 
	<dependencies> 
	    <!-- netty 依赖包  --> 
		<dependency> 
			<groupId>io.netty</groupId> 
			<artifactId>netty-all</artifactId> 
			<version>4.1.42.Final</version> 
		</dependency> 
		<!-- ProtoBuf 自定义协议包  --> 
		<dependency> 
			<groupId>com.google.protobuf</groupId> 
			<artifactId>protobuf-java</artifactId> 
			<version>3.11.0</version> 
		</dependency> 
	</dependencies>

第三步:新建Netty 服务端程序启动入口及其消息处理器(Handler)

服务端代码:EchoServer.java

package com.netty.server.two; 
 
import com.netty.protobuf.UserInfo; 
import com.netty.server.two.handler.EchoServerHandler; 
import io.netty.bootstrap.ServerBootstrap; 
import io.netty.channel.ChannelFuture; 
import io.netty.channel.ChannelInitializer; 
import io.netty.channel.EventLoopGroup; 
import io.netty.channel.nio.NioEventLoopGroup; 
import io.netty.channel.socket.SocketChannel; 
import io.netty.channel.socket.nio.NioServerSocketChannel; 
import io.netty.handler.codec.protobuf.ProtobufDecoder; 
import io.netty.handler.codec.protobuf.ProtobufEncoder; 
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; 
import io.netty.handler.logging.LogLevel; 
import io.netty.handler.logging.LoggingHandler; 
 
public class EchoServer { 
	private int port=8081; 
	private void run() { 
		  EventLoopGroup bossGroup = new NioEventLoopGroup(); 
	      EventLoopGroup workerGroup = new NioEventLoopGroup(); 
	        try { 
	            //创建ServerBootstrap对象,它是Netty用于启动NIO服务端的辅助启动类,目的是降低服务端的开发复杂度。 
	            ServerBootstrap bootstrap = new ServerBootstrap(); 
 
	            bootstrap 
	                    //调用ServerBootstrap的group方法,将两个NIO线程组当作入参传递到ServerBootstrap中。 
	                    .group(bossGroup, workerGroup) 
	                    //接着设置创建的Channel为NioServerSocketChannel,它的功能对应于JDK NIO类库中的ServerSocketChannel类。 
	                    .channel(NioServerSocketChannel.class) 
	                    .handler(new LoggingHandler(LogLevel.INFO)) 
	                    //最后绑定I/O事件的处理类ChannelInitializer,它的作用类似于Reactor模式中的Handler类, 
	                    //主要用于处理网络I/O事件,例如记录日志、对消息进行编解码等。 
	                    .childHandler(new ChannelInitializer<SocketChannel>() { 
	                        @Override 
	                        protected void initChannel(SocketChannel socketChannel) throws Exception { 
	                            socketChannel.pipeline() 
	                                    //创建消息解码器,获取消息长度 
	                                    .addLast(new ProtobufVarint32FrameDecoder()) 
	                                    //指定消息类型 
	                                    .addLast(new ProtobufDecoder(UserInfo.UserMsg.getDefaultInstance())) 
	                                    //消息头置长度 
	                                    .addLast(new ProtobufVarint32LengthFieldPrepender()) 
	                                    //创建消息编码器 
	                                    .addLast(new ProtobufEncoder()) 
	                                    //消息的处理 
	                                    .addLast(new EchoServerHandler()); 
	                        } 
	                    }); 
	            //服务端启动辅助类配置完成之后,调用它的bind 方法绑定监听端口, 
	            //随后,调用它的同步阻塞方法sync等待绑定操作完成。 
	            //完成之后Netty会返回一个ChannelFuture, 它的功能类似于JDK的java.util.concurrent.Future,主要用于异步操作的通知回调。 
	            //使用f.channel.closeFuture().syncO方法进行阻塞,等待服务端链路关闭之后main函数才退出。 
	            ChannelFuture sync = bootstrap.bind(port).sync(); 
 
	            sync.channel().closeFuture().sync(); 
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } finally { 
	            //调用NIO线程组的shutdownGracefully 进行优雅退出,它会释放跟shutdownGracefully相关联的资源。 
	            bossGroup.shutdownGracefully(); 
	            workerGroup.shutdownGracefully(); 
	        } 
 
	} 
	 
	public static void main(String[] args) { 
		new EchoServer().run(); 
	} 
} 

服务端消息处理器:EchoServerHandler.java

package com.netty.server.two.handler; 
 
import com.netty.protobuf.UserInfo; 
import io.netty.channel.ChannelHandlerContext; 
import io.netty.channel.ChannelInboundHandlerAdapter; 
import io.netty.util.ReferenceCountUtil; 
 
public class EchoServerHandler extends ChannelInboundHandlerAdapter { 
	int count; 
 
	//当通道激活时 
    @Override 
    public void channelActive(ChannelHandlerContext ctx) throws Exception { 
        System.out.println("连接的客户端地址:" + ctx.channel().remoteAddress()); 
        super.channelActive(ctx); 
    } 
 
	//当通道读到数据时 
    @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
        try { 
        	//拿到解码后的消息 
        	UserInfo.UserMsg userMsg = (UserInfo.UserMsg) msg; 
            System.out.println("Receive:[" + userMsg.toString() + "],count:[" + (++count) + "]"); 
            UserInfo.UserMsg.Builder builder =  userMsg.toBuilder().setState(1); 
            ctx.writeAndFlush(builder); 
        } finally { 
            ReferenceCountUtil.release(msg); 
        } 
 
 
    } 
 
	//当通道异常时 
    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
        cause.printStackTrace(); 
        ctx.close(); 
    } 
} 

第四步:新建Netty 客户端程序启动入口及其消息处理器(Handler)

客户端程序入口:Echoclient.java

package com.netty.client.two; 
 
import com.netty.client.two.handler.EchoClientHandler; 
import com.netty.protobuf.UserInfo; 
 
import io.netty.bootstrap.Bootstrap; 
import io.netty.channel.ChannelFuture; 
import io.netty.channel.ChannelInitializer; 
import io.netty.channel.EventLoopGroup; 
import io.netty.channel.nio.NioEventLoopGroup; 
import io.netty.channel.socket.SocketChannel; 
import io.netty.channel.socket.nio.NioSocketChannel; 
import io.netty.handler.codec.protobuf.ProtobufDecoder; 
import io.netty.handler.codec.protobuf.ProtobufEncoder; 
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; 
 
public class Echoclient { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		int port = 8081; 
		EventLoopGroup group = new NioEventLoopGroup(); 
        try { 
            Bootstrap b = new Bootstrap(); 
            b.group(group) 
                    .channel(NioSocketChannel.class) 
                    .handler(new ChannelInitializer<SocketChannel>() { 
                        @Override 
                        protected void initChannel(SocketChannel socketChannel) throws Exception { 
                            socketChannel.pipeline() 
                                    .addLast(new ProtobufVarint32FrameDecoder()) 
                                    .addLast(new ProtobufDecoder(UserInfo.UserMsg.getDefaultInstance())) 
                                    .addLast(new ProtobufVarint32LengthFieldPrepender()) 
                                    .addLast(new ProtobufEncoder()) 
                                    .addLast(new EchoClientHandler()); 
                        } 
                    }); 
            ChannelFuture f = b.connect("127.0.0.1", port).sync(); 
 
            f.channel().closeFuture().sync(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            group.shutdownGracefully(); 
        } 
	} 
 
} 

客户端消息处理器:EchoClientHandler.java

package com.netty.client.two.handler; 
 
import com.netty.protobuf.UserInfo; 
 
import io.netty.channel.ChannelHandlerContext; 
import io.netty.channel.ChannelInboundHandlerAdapter; 
import io.netty.util.ReferenceCountUtil; 
 
public class EchoClientHandler extends ChannelInboundHandlerAdapter { 
	int count; 
    String echo_req = "Hi,zhouchengxi,welcome to netty"; 
    UserInfo.UserMsg.Builder builder = UserInfo.UserMsg.newBuilder(); 
 
    @Override 
    public void channelActive(ChannelHandlerContext ctx) throws Exception { 
        for (int i = 1; i <= 10; i++) { 
            UserInfo.UserMsg fangyan = builder.setId(1).setAge(i).setName("zhouchengxi" + i).build(); 
            ctx.writeAndFlush(fangyan); 
        } 
    } 
 
    @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
        try { 
            UserInfo.UserMsg userMsg = (UserInfo.UserMsg) msg; 
            System.out.println("Server:[" + userMsg.toString() + "]count:[" + (++count) + "]"); 
        } finally { 
            ReferenceCountUtil.release(msg); 
        } 
    } 
 
    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
        cause.printStackTrace(); 
        ctx.close(); 
    } 
} 

第五步:自定义协议对应Java实体对象

// Generated by the protocol buffer compiler.  DO NOT EDIT! 
// source: User.proto 
 
package com.netty.protobuf; 
 
public final class UserInfo { 
  private UserInfo() {} 
  public static void registerAllExtensions( 
      com.google.protobuf.ExtensionRegistryLite registry) { 
  } 
 
  public static void registerAllExtensions( 
      com.google.protobuf.ExtensionRegistry registry) { 
    registerAllExtensions( 
        (com.google.protobuf.ExtensionRegistryLite) registry); 
  } 
  public interface UserMsgOrBuilder extends 
      // @@protoc_insertion_point(interface_extends:UserMsg) 
      com.google.protobuf.MessageOrBuilder { 
 
    /** 
     * <code>int32 id = 1;</code> 
     * @return The id. 
     */ 
    int getId(); 
 
    /** 
     * <code>string name = 2;</code> 
     * @return The name. 
     */ 
    java.lang.String getName(); 
    /** 
     * <code>string name = 2;</code> 
     * @return The bytes for name. 
     */ 
    com.google.protobuf.ByteString 
        getNameBytes(); 
 
    /** 
     * <code>int32 age = 3;</code> 
     * @return The age. 
     */ 
    int getAge(); 
 
    /** 
     * <code>int32 state = 4;</code> 
     * @return The state. 
     */ 
    int getState(); 
  } 
  /** 
   * Protobuf type {@code UserMsg} 
   */ 
  public static final class UserMsg extends 
      com.google.protobuf.GeneratedMessageV3 implements 
      // @@protoc_insertion_point(message_implements:UserMsg) 
      UserMsgOrBuilder { 
  private static final long serialVersionUID = 0L; 
    // Use UserMsg.newBuilder() to construct. 
    private UserMsg(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) { 
      super(builder); 
    } 
    private UserMsg() { 
      name_ = ""; 
    } 
 
    @java.lang.Override 
    @SuppressWarnings({"unused"}) 
    protected java.lang.Object newInstance( 
        UnusedPrivateParameter unused) { 
      return new UserMsg(); 
    } 
 
    @java.lang.Override 
    public final com.google.protobuf.UnknownFieldSet 
    getUnknownFields() { 
      return this.unknownFields; 
    } 
    private UserMsg( 
        com.google.protobuf.CodedInputStream input, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      this(); 
      if (extensionRegistry == null) { 
        throw new java.lang.NullPointerException(); 
      } 
      com.google.protobuf.UnknownFieldSet.Builder unknownFields = 
          com.google.protobuf.UnknownFieldSet.newBuilder(); 
      try { 
        boolean done = false; 
        while (!done) { 
          int tag = input.readTag(); 
          switch (tag) { 
            case 0: 
              done = true; 
              break; 
            case 8: { 
 
              id_ = input.readInt32(); 
              break; 
            } 
            case 18: { 
              java.lang.String s = input.readStringRequireUtf8(); 
 
              name_ = s; 
              break; 
            } 
            case 24: { 
 
              age_ = input.readInt32(); 
              break; 
            } 
            case 32: { 
 
              state_ = input.readInt32(); 
              break; 
            } 
            default: { 
              if (!parseUnknownField( 
                  input, unknownFields, extensionRegistry, tag)) { 
                done = true; 
              } 
              break; 
            } 
          } 
        } 
      } catch (com.google.protobuf.InvalidProtocolBufferException e) { 
        throw e.setUnfinishedMessage(this); 
      } catch (java.io.IOException e) { 
        throw new com.google.protobuf.InvalidProtocolBufferException( 
            e).setUnfinishedMessage(this); 
      } finally { 
        this.unknownFields = unknownFields.build(); 
        makeExtensionsImmutable(); 
      } 
    } 
    public static final com.google.protobuf.Descriptors.Descriptor 
        getDescriptor() { 
      return com.netty.protobuf.UserInfo.internal_static_UserMsg_descriptor; 
    } 
 
    @java.lang.Override 
    protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 
        internalGetFieldAccessorTable() { 
      return com.netty.protobuf.UserInfo.internal_static_UserMsg_fieldAccessorTable 
          .ensureFieldAccessorsInitialized( 
              com.netty.protobuf.UserInfo.UserMsg.class, com.netty.protobuf.UserInfo.UserMsg.Builder.class); 
    } 
 
    public static final int ID_FIELD_NUMBER = 1; 
    private int id_; 
    /** 
     * <code>int32 id = 1;</code> 
     * @return The id. 
     */ 
    @java.lang.Override 
    public int getId() { 
      return id_; 
    } 
 
    public static final int NAME_FIELD_NUMBER = 2; 
    private volatile java.lang.Object name_; 
    /** 
     * <code>string name = 2;</code> 
     * @return The name. 
     */ 
    @java.lang.Override 
    public java.lang.String getName() { 
      java.lang.Object ref = name_; 
      if (ref instanceof java.lang.String) { 
        return (java.lang.String) ref; 
      } else { 
        com.google.protobuf.ByteString bs =  
            (com.google.protobuf.ByteString) ref; 
        java.lang.String s = bs.toStringUtf8(); 
        name_ = s; 
        return s; 
      } 
    } 
    /** 
     * <code>string name = 2;</code> 
     * @return The bytes for name. 
     */ 
    @java.lang.Override 
    public com.google.protobuf.ByteString 
        getNameBytes() { 
      java.lang.Object ref = name_; 
      if (ref instanceof java.lang.String) { 
        com.google.protobuf.ByteString b =  
            com.google.protobuf.ByteString.copyFromUtf8( 
                (java.lang.String) ref); 
        name_ = b; 
        return b; 
      } else { 
        return (com.google.protobuf.ByteString) ref; 
      } 
    } 
 
    public static final int AGE_FIELD_NUMBER = 3; 
    private int age_; 
    /** 
     * <code>int32 age = 3;</code> 
     * @return The age. 
     */ 
    @java.lang.Override 
    public int getAge() { 
      return age_; 
    } 
 
    public static final int STATE_FIELD_NUMBER = 4; 
    private int state_; 
    /** 
     * <code>int32 state = 4;</code> 
     * @return The state. 
     */ 
    @java.lang.Override 
    public int getState() { 
      return state_; 
    } 
 
    private byte memoizedIsInitialized = -1; 
    @java.lang.Override 
    public final boolean isInitialized() { 
      byte isInitialized = memoizedIsInitialized; 
      if (isInitialized == 1) return true; 
      if (isInitialized == 0) return false; 
 
      memoizedIsInitialized = 1; 
      return true; 
    } 
 
    @java.lang.Override 
    public void writeTo(com.google.protobuf.CodedOutputStream output) 
                        throws java.io.IOException { 
      if (id_ != 0) { 
        output.writeInt32(1, id_); 
      } 
      if (!getNameBytes().isEmpty()) { 
        com.google.protobuf.GeneratedMessageV3.writeString(output, 2, name_); 
      } 
      if (age_ != 0) { 
        output.writeInt32(3, age_); 
      } 
      if (state_ != 0) { 
        output.writeInt32(4, state_); 
      } 
      unknownFields.writeTo(output); 
    } 
 
    @java.lang.Override 
    public int getSerializedSize() { 
      int size = memoizedSize; 
      if (size != -1) return size; 
 
      size = 0; 
      if (id_ != 0) { 
        size += com.google.protobuf.CodedOutputStream 
          .computeInt32Size(1, id_); 
      } 
      if (!getNameBytes().isEmpty()) { 
        size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, name_); 
      } 
      if (age_ != 0) { 
        size += com.google.protobuf.CodedOutputStream 
          .computeInt32Size(3, age_); 
      } 
      if (state_ != 0) { 
        size += com.google.protobuf.CodedOutputStream 
          .computeInt32Size(4, state_); 
      } 
      size += unknownFields.getSerializedSize(); 
      memoizedSize = size; 
      return size; 
    } 
 
    @java.lang.Override 
    public boolean equals(final java.lang.Object obj) { 
      if (obj == this) { 
       return true; 
      } 
      if (!(obj instanceof com.netty.protobuf.UserInfo.UserMsg)) { 
        return super.equals(obj); 
      } 
      com.netty.protobuf.UserInfo.UserMsg other = (com.netty.protobuf.UserInfo.UserMsg) obj; 
 
      if (getId() 
          != other.getId()) return false; 
      if (!getName() 
          .equals(other.getName())) return false; 
      if (getAge() 
          != other.getAge()) return false; 
      if (getState() 
          != other.getState()) return false; 
      if (!unknownFields.equals(other.unknownFields)) return false; 
      return true; 
    } 
 
    @java.lang.Override 
    public int hashCode() { 
      if (memoizedHashCode != 0) { 
        return memoizedHashCode; 
      } 
      int hash = 41; 
      hash = (19 * hash) + getDescriptor().hashCode(); 
      hash = (37 * hash) + ID_FIELD_NUMBER; 
      hash = (53 * hash) + getId(); 
      hash = (37 * hash) + NAME_FIELD_NUMBER; 
      hash = (53 * hash) + getName().hashCode(); 
      hash = (37 * hash) + AGE_FIELD_NUMBER; 
      hash = (53 * hash) + getAge(); 
      hash = (37 * hash) + STATE_FIELD_NUMBER; 
      hash = (53 * hash) + getState(); 
      hash = (29 * hash) + unknownFields.hashCode(); 
      memoizedHashCode = hash; 
      return hash; 
    } 
 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        java.nio.ByteBuffer data) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      return PARSER.parseFrom(data); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        java.nio.ByteBuffer data, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      return PARSER.parseFrom(data, extensionRegistry); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        com.google.protobuf.ByteString data) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      return PARSER.parseFrom(data); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        com.google.protobuf.ByteString data, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      return PARSER.parseFrom(data, extensionRegistry); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom(byte[] data) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      return PARSER.parseFrom(data); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        byte[] data, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws com.google.protobuf.InvalidProtocolBufferException { 
      return PARSER.parseFrom(data, extensionRegistry); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom(java.io.InputStream input) 
        throws java.io.IOException { 
      return com.google.protobuf.GeneratedMessageV3 
          .parseWithIOException(PARSER, input); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        java.io.InputStream input, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws java.io.IOException { 
      return com.google.protobuf.GeneratedMessageV3 
          .parseWithIOException(PARSER, input, extensionRegistry); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseDelimitedFrom(java.io.InputStream input) 
        throws java.io.IOException { 
      return com.google.protobuf.GeneratedMessageV3 
          .parseDelimitedWithIOException(PARSER, input); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseDelimitedFrom( 
        java.io.InputStream input, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws java.io.IOException { 
      return com.google.protobuf.GeneratedMessageV3 
          .parseDelimitedWithIOException(PARSER, input, extensionRegistry); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        com.google.protobuf.CodedInputStream input) 
        throws java.io.IOException { 
      return com.google.protobuf.GeneratedMessageV3 
          .parseWithIOException(PARSER, input); 
    } 
    public static com.netty.protobuf.UserInfo.UserMsg parseFrom( 
        com.google.protobuf.CodedInputStream input, 
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
        throws java.io.IOException { 
      return com.google.protobuf.GeneratedMessageV3 
          .parseWithIOException(PARSER, input, extensionRegistry); 
    } 
 
    @java.lang.Override 
    public Builder newBuilderForType() { return newBuilder(); } 
    public static Builder newBuilder() { 
      return DEFAULT_INSTANCE.toBuilder(); 
    } 
    public static Builder newBuilder(com.netty.protobuf.UserInfo.UserMsg prototype) { 
      return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 
    } 
    @java.lang.Override 
    public Builder toBuilder() { 
      return this == DEFAULT_INSTANCE 
          ? new Builder() : new Builder().mergeFrom(this); 
    } 
 
    @java.lang.Override 
    protected Builder newBuilderForType( 
        com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 
      Builder builder = new Builder(parent); 
      return builder; 
    } 
    /** 
     * Protobuf type {@code UserMsg} 
     */ 
    public static final class Builder extends 
        com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements 
        // @@protoc_insertion_point(builder_implements:UserMsg) 
        com.netty.protobuf.UserInfo.UserMsgOrBuilder { 
      public static final com.google.protobuf.Descriptors.Descriptor 
          getDescriptor() { 
        return com.netty.protobuf.UserInfo.internal_static_UserMsg_descriptor; 
      } 
 
      @java.lang.Override 
      protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 
          internalGetFieldAccessorTable() { 
        return com.netty.protobuf.UserInfo.internal_static_UserMsg_fieldAccessorTable 
            .ensureFieldAccessorsInitialized( 
                com.netty.protobuf.UserInfo.UserMsg.class, com.netty.protobuf.UserInfo.UserMsg.Builder.class); 
      } 
 
      // Construct using com.netty.protobuf.UserInfo.UserMsg.newBuilder() 
      private Builder() { 
        maybeForceBuilderInitialization(); 
      } 
 
      private Builder( 
          com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 
        super(parent); 
        maybeForceBuilderInitialization(); 
      } 
      private void maybeForceBuilderInitialization() { 
        if (com.google.protobuf.GeneratedMessageV3 
                .alwaysUseFieldBuilders) { 
        } 
      } 
      @java.lang.Override 
      public Builder clear() { 
        super.clear(); 
        id_ = 0; 
 
        name_ = ""; 
 
        age_ = 0; 
 
        state_ = 0; 
 
        return this; 
      } 
 
      @java.lang.Override 
      public com.google.protobuf.Descriptors.Descriptor 
          getDescriptorForType() { 
        return com.netty.protobuf.UserInfo.internal_static_UserMsg_descriptor; 
      } 
 
      @java.lang.Override 
      public com.netty.protobuf.UserInfo.UserMsg getDefaultInstanceForType() { 
        return com.netty.protobuf.UserInfo.UserMsg.getDefaultInstance(); 
      } 
 
      @java.lang.Override 
      public com.netty.protobuf.UserInfo.UserMsg build() { 
        com.netty.protobuf.UserInfo.UserMsg result = buildPartial(); 
        if (!result.isInitialized()) { 
          throw newUninitializedMessageException(result); 
        } 
        return result; 
      } 
 
      @java.lang.Override 
      public com.netty.protobuf.UserInfo.UserMsg buildPartial() { 
        com.netty.protobuf.UserInfo.UserMsg result = new com.netty.protobuf.UserInfo.UserMsg(this); 
        result.id_ = id_; 
        result.name_ = name_; 
        result.age_ = age_; 
        result.state_ = state_; 
        onBuilt(); 
        return result; 
      } 
 
      @java.lang.Override 
      public Builder clone() { 
        return super.clone(); 
      } 
      @java.lang.Override 
      public Builder setField( 
          com.google.protobuf.Descriptors.FieldDescriptor field, 
          java.lang.Object value) { 
        return super.setField(field, value); 
      } 
      @java.lang.Override 
      public Builder clearField( 
          com.google.protobuf.Descriptors.FieldDescriptor field) { 
        return super.clearField(field); 
      } 
      @java.lang.Override 
      public Builder clearOneof( 
          com.google.protobuf.Descriptors.OneofDescriptor oneof) { 
        return super.clearOneof(oneof); 
      } 
      @java.lang.Override 
      public Builder setRepeatedField( 
          com.google.protobuf.Descriptors.FieldDescriptor field, 
          int index, java.lang.Object value) { 
        return super.setRepeatedField(field, index, value); 
      } 
      @java.lang.Override 
      public Builder addRepeatedField( 
          com.google.protobuf.Descriptors.FieldDescriptor field, 
          java.lang.Object value) { 
        return super.addRepeatedField(field, value); 
      } 
      @java.lang.Override 
      public Builder mergeFrom(com.google.protobuf.Message other) { 
        if (other instanceof com.netty.protobuf.UserInfo.UserMsg) { 
          return mergeFrom((com.netty.protobuf.UserInfo.UserMsg)other); 
        } else { 
          super.mergeFrom(other); 
          return this; 
        } 
      } 
 
      public Builder mergeFrom(com.netty.protobuf.UserInfo.UserMsg other) { 
        if (other == com.netty.protobuf.UserInfo.UserMsg.getDefaultInstance()) return this; 
        if (other.getId() != 0) { 
          setId(other.getId()); 
        } 
        if (!other.getName().isEmpty()) { 
          name_ = other.name_; 
          onChanged(); 
        } 
        if (other.getAge() != 0) { 
          setAge(other.getAge()); 
        } 
        if (other.getState() != 0) { 
          setState(other.getState()); 
        } 
        this.mergeUnknownFields(other.unknownFields); 
        onChanged(); 
        return this; 
      } 
 
      @java.lang.Override 
      public final boolean isInitialized() { 
        return true; 
      } 
 
      @java.lang.Override 
      public Builder mergeFrom( 
          com.google.protobuf.CodedInputStream input, 
          com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
          throws java.io.IOException { 
        com.netty.protobuf.UserInfo.UserMsg parsedMessage = null; 
        try { 
          parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); 
        } catch (com.google.protobuf.InvalidProtocolBufferException e) { 
          parsedMessage = (com.netty.protobuf.UserInfo.UserMsg) e.getUnfinishedMessage(); 
          throw e.unwrapIOException(); 
        } finally { 
          if (parsedMessage != null) { 
            mergeFrom(parsedMessage); 
          } 
        } 
        return this; 
      } 
 
      private int id_ ; 
      /** 
       * <code>int32 id = 1;</code> 
       * @return The id. 
       */ 
      @java.lang.Override 
      public int getId() { 
        return id_; 
      } 
      /** 
       * <code>int32 id = 1;</code> 
       * @param value The id to set. 
       * @return This builder for chaining. 
       */ 
      public Builder setId(int value) { 
         
        id_ = value; 
        onChanged(); 
        return this; 
      } 
      /** 
       * <code>int32 id = 1;</code> 
       * @return This builder for chaining. 
       */ 
      public Builder clearId() { 
         
        id_ = 0; 
        onChanged(); 
        return this; 
      } 
 
      private java.lang.Object name_ = ""; 
      /** 
       * <code>string name = 2;</code> 
       * @return The name. 
       */ 
      public java.lang.String getName() { 
        java.lang.Object ref = name_; 
        if (!(ref instanceof java.lang.String)) { 
          com.google.protobuf.ByteString bs = 
              (com.google.protobuf.ByteString) ref; 
          java.lang.String s = bs.toStringUtf8(); 
          name_ = s; 
          return s; 
        } else { 
          return (java.lang.String) ref; 
        } 
      } 
      /** 
       * <code>string name = 2;</code> 
       * @return The bytes for name. 
       */ 
      public com.google.protobuf.ByteString 
          getNameBytes() { 
        java.lang.Object ref = name_; 
        if (ref instanceof String) { 
          com.google.protobuf.ByteString b =  
              com.google.protobuf.ByteString.copyFromUtf8( 
                  (java.lang.String) ref); 
          name_ = b; 
          return b; 
        } else { 
          return (com.google.protobuf.ByteString) ref; 
        } 
      } 
      /** 
       * <code>string name = 2;</code> 
       * @param value The name to set. 
       * @return This builder for chaining. 
       */ 
      public Builder setName( 
          java.lang.String value) { 
        if (value == null) { 
    throw new NullPointerException(); 
  } 
   
        name_ = value; 
        onChanged(); 
        return this; 
      } 
      /** 
       * <code>string name = 2;</code> 
       * @return This builder for chaining. 
       */ 
      public Builder clearName() { 
         
        name_ = getDefaultInstance().getName(); 
        onChanged(); 
        return this; 
      } 
      /** 
       * <code>string name = 2;</code> 
       * @param value The bytes for name to set. 
       * @return This builder for chaining. 
       */ 
      public Builder setNameBytes( 
          com.google.protobuf.ByteString value) { 
        if (value == null) { 
    throw new NullPointerException(); 
  } 
  checkByteStringIsUtf8(value); 
         
        name_ = value; 
        onChanged(); 
        return this; 
      } 
 
      private int age_ ; 
      /** 
       * <code>int32 age = 3;</code> 
       * @return The age. 
       */ 
      @java.lang.Override 
      public int getAge() { 
        return age_; 
      } 
      /** 
       * <code>int32 age = 3;</code> 
       * @param value The age to set. 
       * @return This builder for chaining. 
       */ 
      public Builder setAge(int value) { 
         
        age_ = value; 
        onChanged(); 
        return this; 
      } 
      /** 
       * <code>int32 age = 3;</code> 
       * @return This builder for chaining. 
       */ 
      public Builder clearAge() { 
         
        age_ = 0; 
        onChanged(); 
        return this; 
      } 
 
      private int state_ ; 
      /** 
       * <code>int32 state = 4;</code> 
       * @return The state. 
       */ 
      @java.lang.Override 
      public int getState() { 
        return state_; 
      } 
      /** 
       * <code>int32 state = 4;</code> 
       * @param value The state to set. 
       * @return This builder for chaining. 
       */ 
      public Builder setState(int value) { 
         
        state_ = value; 
        onChanged(); 
        return this; 
      } 
      /** 
       * <code>int32 state = 4;</code> 
       * @return This builder for chaining. 
       */ 
      public Builder clearState() { 
         
        state_ = 0; 
        onChanged(); 
        return this; 
      } 
      @java.lang.Override 
      public final Builder setUnknownFields( 
          final com.google.protobuf.UnknownFieldSet unknownFields) { 
        return super.setUnknownFields(unknownFields); 
      } 
 
      @java.lang.Override 
      public final Builder mergeUnknownFields( 
          final com.google.protobuf.UnknownFieldSet unknownFields) { 
        return super.mergeUnknownFields(unknownFields); 
      } 
 
 
      // @@protoc_insertion_point(builder_scope:UserMsg) 
    } 
 
    // @@protoc_insertion_point(class_scope:UserMsg) 
    private static final com.netty.protobuf.UserInfo.UserMsg DEFAULT_INSTANCE; 
    static { 
      DEFAULT_INSTANCE = new com.netty.protobuf.UserInfo.UserMsg(); 
    } 
 
    public static com.netty.protobuf.UserInfo.UserMsg getDefaultInstance() { 
      return DEFAULT_INSTANCE; 
    } 
 
    private static final com.google.protobuf.Parser<UserMsg> 
        PARSER = new com.google.protobuf.AbstractParser<UserMsg>() { 
      @java.lang.Override 
      public UserMsg parsePartialFrom( 
          com.google.protobuf.CodedInputStream input, 
          com.google.protobuf.ExtensionRegistryLite extensionRegistry) 
          throws com.google.protobuf.InvalidProtocolBufferException { 
        return new UserMsg(input, extensionRegistry); 
      } 
    }; 
 
    public static com.google.protobuf.Parser<UserMsg> parser() { 
      return PARSER; 
    } 
 
    @java.lang.Override 
    public com.google.protobuf.Parser<UserMsg> getParserForType() { 
      return PARSER; 
    } 
 
    @java.lang.Override 
    public com.netty.protobuf.UserInfo.UserMsg getDefaultInstanceForType() { 
      return DEFAULT_INSTANCE; 
    } 
 
  } 
 
  private static final com.google.protobuf.Descriptors.Descriptor 
    internal_static_UserMsg_descriptor; 
  private static final  
    com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 
      internal_static_UserMsg_fieldAccessorTable; 
 
  public static com.google.protobuf.Descriptors.FileDescriptor 
      getDescriptor() { 
    return descriptor; 
  } 
  private static  com.google.protobuf.Descriptors.FileDescriptor 
      descriptor; 
  static { 
    java.lang.String[] descriptorData = { 
      "\n\nUser.proto\"?\n\007UserMsg\022\n\n\002id\030\001 \001(\005\022\014\n\004n" + 
      "ame\030\002 \001(\t\022\013\n\003age\030\003 \001(\005\022\r\n\005state\030\004 \001(\005B\036\n" + 
      "\022com.netty.protobufB\010UserInfob\006proto3" 
    }; 
    descriptor = com.google.protobuf.Descriptors.FileDescriptor 
      .internalBuildGeneratedFileFrom(descriptorData, 
        new com.google.protobuf.Descriptors.FileDescriptor[] { 
        }); 
    internal_static_UserMsg_descriptor = 
      getDescriptor().getMessageTypes().get(0); 
    internal_static_UserMsg_fieldAccessorTable = new 
      com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 
        internal_static_UserMsg_descriptor, 
        new java.lang.String[] { "Id", "Name", "Age", "State", }); 
  } 
 
  // @@protoc_insertion_point(outer_class_scope) 
} 

效果截图:

服务端:

客户端:

补充:针对自定义协议实体对象。

Java 代码示例:

// 按照定义的数据结构,创建一个对象 
UserInfo.UserMsg.Builder userInfo = UserInfo.UserMsg.newBuilder(); 
userInfo.setId(1); 
userInfo.setName("zhouchenxi"); 
userInfo.setAge(2); 
UserInfo.UserMsg userMsg = userInfo.build(); 
// 将数据写到输出流 
ByteArrayOutputStream output = new ByteArrayOutputStream(); 
userMsg.writeTo(output); 
// 将数据序列化后发送 
byte[] byteArray = output.toByteArray(); 
// 接收到流并读取 
ByteArrayInputStream input = new ByteArrayInputStream(byteArray); 
// 反序列化 
UserInfo.UserMsg userInfo2 = UserInfo.UserMsg.parseFrom(input); 
System.out.println("id:" + userInfo2.getId()); 
System.out.println("name:" + userInfo2.getName()); 
System.out.println("age:" + userInfo2.getAge()); 
 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号