netty权威指南学习笔记七——编解码技术之GoogleProtobuf
首先我们来看一下protobuf的优点:
- 谷歌长期使用成熟度高;
- 跨语言支持多种语言如:C++,java,Python;
- 编码后消息更小,更利于存储传输;
- 编解码性能高;
- 支持不同协议版本的兼容性;
- 支持定义可选和必选字段;
接下来就让我们试用一下吧。
一、Protobuf开发环境搭建
下载Protobuf的Windows版本,本博主用的是protoc-3.6.1-win32.zip,解压后进入bin目录可以看到protoc.exe(下面的.proto文件是博主事先拉进来的)
然后我们打开cmd命令查看protoc是否安装成功,因本博主是尝试成功后接下来补充的博客,本博主配置了环境变量,但是运行成功后又把环境变量删掉了,仍可以编译生成java文件,所以可能不需要配置,但是读者如果没有配置运行不成功的话就还是配置一下。配置的方法就是在环境变量中path中添加protoc.exe所在的路径即可。
然后在cmd中输入命令 protoc和proto --version查看
OK,然后就可以对写的文件进行编译生成.java文件。在这里本博主遇到的几个坑,就是proto文件编写语法与java不同,比如,java字符串类型为String,赋值是加双引号的,而proto不认识,它的声明词是小写的string,其赋值是不需要加双引号的;在者也需要注意proto3也不同于proto2,采用proto3必须要写出其版本号syntax = "proto3";
具体变化可以参看https://www.cnblogs.com/asminfo/p/6782906.html的一部分。
接下来就可以用demo进行示范执行,代码如下:借用java模板加入进来,一定要注意语法不能写错否则执行命令就会报错
syntax = "proto3";
option java_package="com.protobuf";
option java_outer_classname="DemoProto"; message Demo{
int32 subReqID=1;
string userName=2;
string productName=3;
string address=4;
}
执行命令如下:protoc.exe ./源文件 --java_out=./+目标子文件夹
D:\>protoc ./protobufc/bin/Demo.proto --java_out=./ 这条命令执行后生成的com包在d:盘根目录下
D:\>protoc ./protobufc/bin/Demo.proto --java_out=./protobufc/bin/ 这条命令执行后生成的com包在d:盘protobufc/bin/ 下
C:\Users\litan>protoc ./d:protobufc/bin/Demo.proto --java_out=./ 这条命令执行报错说明执行命令的盘必须是源文件.proto和目标文件.java的同级或父级目录
./d:protobufc/bin/Demo.proto: No such file or directory 报错信息
以上我们将我们接下来需要的.proto转化为.java
这是因为代码里的String用了大写,大写改为小写后代码如下:
Req代码
syntax = "proto3";
option java_package="com.protobuf";
option java_outer_classname="SubscribeReqProto"; message SubscribeReq{
int32 subReqID=1;
string userName=2;
string productName=3;
repeated string address=4;
}
Resp代码
syntax = "proto3";
option java_package="com.protobuf";
option java_outer_classname="SubscribeRespProto"; message SubscribeResp{
int32 subReqID=1;
int32 respcode=2;
string desc=3;
}
执行
执行后的目录及代码
我们将后两个文件拷贝到相应工程中,发现报错,这里需要的是一个protobuf-java包,然后到官网找相应版本的包,该版本3.6.1的包在maven中可以找到,直接拿来用
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
maven自动更新完成后一切ok,到这里,protobuf环境已经可以使用了,即便将来再有新的pojo也可以轻松搞定。
二、测试protobuf的编解码效果
先看一下protobuf转为java后的代码,代码比较长折叠起来
Req代码
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: SubscribeReq.proto package com.protobuf; public final class SubscribeReqProto {
private SubscribeReqProto() {}
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 SubscribeReqOrBuilder extends
// @@protoc_insertion_point(interface_extends:SubscribeReq)
com.google.protobuf.MessageOrBuilder { /**
* <code>int32 subReqID = 1;</code>
*/
int getSubReqID(); /**
* <code>string userName = 2;</code>
*/
String getUserName();
/**
* <code>string userName = 2;</code>
*/
com.google.protobuf.ByteString
getUserNameBytes(); /**
* <code>string productName = 3;</code>
*/
String getProductName();
/**
* <code>string productName = 3;</code>
*/
com.google.protobuf.ByteString
getProductNameBytes(); /**
* <code>repeated string address = 4;</code>
*/
java.util.List<String>
getAddressList();
/**
* <code>repeated string address = 4;</code>
*/
int getAddressCount();
/**
* <code>repeated string address = 4;</code>
*/
String getAddress(int index);
/**
* <code>repeated string address = 4;</code>
*/
com.google.protobuf.ByteString
getAddressBytes(int index);
}
/**
* Protobuf type {@code SubscribeReq}
*/
public static final class SubscribeReq extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:SubscribeReq)
SubscribeReqOrBuilder {
private static final long serialVersionUID = 0L;
// Use SubscribeReq.newBuilder() to construct.
private SubscribeReq(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private SubscribeReq() {
subReqID_ = 0;
userName_ = "";
productName_ = "";
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
} @Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private SubscribeReq(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
this();
if (extensionRegistry == null) {
throw new NullPointerException();
}
int mutable_bitField0_ = 0;
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: { subReqID_ = input.readInt32();
break;
}
case 18: {
String s = input.readStringRequireUtf8(); userName_ = s;
break;
}
case 26: {
String s = input.readStringRequireUtf8(); productName_ = s;
break;
}
case 34: {
String s = input.readStringRequireUtf8();
if (!((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
address_ = new com.google.protobuf.LazyStringArrayList();
mutable_bitField0_ |= 0x00000008;
}
address_.add(s);
break;
}
default: {
if (!parseUnknownFieldProto3(
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 {
if (((mutable_bitField0_ & 0x00000008) == 0x00000008)) {
address_ = address_.getUnmodifiableView();
}
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return SubscribeReqProto.internal_static_SubscribeReq_descriptor;
} @Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeReqProto.internal_static_SubscribeReq_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeReq.class, Builder.class);
} private int bitField0_;
public static final int SUBREQID_FIELD_NUMBER = 1;
private int subReqID_;
/**
* <code>int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
} public static final int USERNAME_FIELD_NUMBER = 2;
private volatile Object userName_;
/**
* <code>string userName = 2;</code>
*/
public String getUserName() {
Object ref = userName_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
userName_ = s;
return s;
}
}
/**
* <code>string userName = 2;</code>
*/
public com.google.protobuf.ByteString
getUserNameBytes() {
Object ref = userName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
userName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} public static final int PRODUCTNAME_FIELD_NUMBER = 3;
private volatile Object productName_;
/**
* <code>string productName = 3;</code>
*/
public String getProductName() {
Object ref = productName_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
productName_ = s;
return s;
}
}
/**
* <code>string productName = 3;</code>
*/
public com.google.protobuf.ByteString
getProductNameBytes() {
Object ref = productName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
productName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} public static final int ADDRESS_FIELD_NUMBER = 4;
private com.google.protobuf.LazyStringList address_;
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ProtocolStringList
getAddressList() {
return address_;
}
/**
* <code>repeated string address = 4;</code>
*/
public int getAddressCount() {
return address_.size();
}
/**
* <code>repeated string address = 4;</code>
*/
public String getAddress(int index) {
return address_.get(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes(int index) {
return address_.getByteString(index);
} private byte memoizedIsInitialized = -1;
@Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false; memoizedIsInitialized = 1;
return true;
} @Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (subReqID_ != 0) {
output.writeInt32(1, subReqID_);
}
if (!getUserNameBytes().isEmpty()) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 2, userName_);
}
if (!getProductNameBytes().isEmpty()) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, productName_);
}
for (int i = 0; i < address_.size(); i++) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 4, address_.getRaw(i));
}
unknownFields.writeTo(output);
} @Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size; size = 0;
if (subReqID_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, subReqID_);
}
if (!getUserNameBytes().isEmpty()) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, userName_);
}
if (!getProductNameBytes().isEmpty()) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, productName_);
}
{
int dataSize = 0;
for (int i = 0; i < address_.size(); i++) {
dataSize += computeStringSizeNoTag(address_.getRaw(i));
}
size += dataSize;
size += 1 * getAddressList().size();
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
} @Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SubscribeReq)) {
return super.equals(obj);
}
SubscribeReq other = (SubscribeReq) obj; boolean result = true;
result = result && (getSubReqID()
== other.getSubReqID());
result = result && getUserName()
.equals(other.getUserName());
result = result && getProductName()
.equals(other.getProductName());
result = result && getAddressList()
.equals(other.getAddressList());
result = result && unknownFields.equals(other.unknownFields);
return result;
} @Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
hash = (37 * hash) + SUBREQID_FIELD_NUMBER;
hash = (53 * hash) + getSubReqID();
hash = (37 * hash) + USERNAME_FIELD_NUMBER;
hash = (53 * hash) + getUserName().hashCode();
hash = (37 * hash) + PRODUCTNAME_FIELD_NUMBER;
hash = (53 * hash) + getProductName().hashCode();
if (getAddressCount() > 0) {
hash = (37 * hash) + ADDRESS_FIELD_NUMBER;
hash = (53 * hash) + getAddressList().hashCode();
}
hash = (29 * hash) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
} public static SubscribeReq parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeReq parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeReq parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeReq parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeReq parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeReq parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeReq parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeReq 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 SubscribeReq parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static SubscribeReq 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 SubscribeReq parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeReq parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
} @Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(SubscribeReq prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
@Override
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
} @Override
protected Builder newBuilderForType(
BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code SubscribeReq}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:SubscribeReq)
SubscribeReqOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return SubscribeReqProto.internal_static_SubscribeReq_descriptor;
} @Override
protected FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeReqProto.internal_static_SubscribeReq_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeReq.class, Builder.class);
} // Construct using com.protobuf.SubscribeReqProto.SubscribeReq.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
} private Builder(
BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3
.alwaysUseFieldBuilders) {
}
}
@Override
public Builder clear() {
super.clear();
subReqID_ = 0; userName_ = ""; productName_ = ""; address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000008);
return this;
} @Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return SubscribeReqProto.internal_static_SubscribeReq_descriptor;
} @Override
public SubscribeReq getDefaultInstanceForType() {
return SubscribeReq.getDefaultInstance();
} @Override
public SubscribeReq build() {
SubscribeReq result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
} @Override
public SubscribeReq buildPartial() {
SubscribeReq result = new SubscribeReq(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
result.subReqID_ = subReqID_;
result.userName_ = userName_;
result.productName_ = productName_;
if (((bitField0_ & 0x00000008) == 0x00000008)) {
address_ = address_.getUnmodifiableView();
bitField0_ = (bitField0_ & ~0x00000008);
}
result.address_ = address_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
} @Override
public Builder clone() {
return (Builder) super.clone();
}
@Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return (Builder) super.setField(field, value);
}
@Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return (Builder) super.clearField(field);
}
@Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return (Builder) super.clearOneof(oneof);
}
@Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, Object value) {
return (Builder) super.setRepeatedField(field, index, value);
}
@Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return (Builder) super.addRepeatedField(field, value);
}
@Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof SubscribeReq) {
return mergeFrom((SubscribeReq)other);
} else {
super.mergeFrom(other);
return this;
}
} public Builder mergeFrom(SubscribeReq other) {
if (other == SubscribeReq.getDefaultInstance()) return this;
if (other.getSubReqID() != 0) {
setSubReqID(other.getSubReqID());
}
if (!other.getUserName().isEmpty()) {
userName_ = other.userName_;
onChanged();
}
if (!other.getProductName().isEmpty()) {
productName_ = other.productName_;
onChanged();
}
if (!other.address_.isEmpty()) {
if (address_.isEmpty()) {
address_ = other.address_;
bitField0_ = (bitField0_ & ~0x00000008);
} else {
ensureAddressIsMutable();
address_.addAll(other.address_);
}
onChanged();
}
this.mergeUnknownFields(other.unknownFields);
onChanged();
return this;
} @Override
public final boolean isInitialized() {
return true;
} @Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
SubscribeReq parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (SubscribeReq) e.getUnfinishedMessage();
throw e.unwrapIOException();
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_; private int subReqID_ ;
/**
* <code>int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
}
/**
* <code>int32 subReqID = 1;</code>
*/
public Builder setSubReqID(int value) { subReqID_ = value;
onChanged();
return this;
}
/**
* <code>int32 subReqID = 1;</code>
*/
public Builder clearSubReqID() { subReqID_ = 0;
onChanged();
return this;
} private Object userName_ = "";
/**
* <code>string userName = 2;</code>
*/
public String getUserName() {
Object ref = userName_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
userName_ = s;
return s;
} else {
return (String) ref;
}
}
/**
* <code>string userName = 2;</code>
*/
public com.google.protobuf.ByteString
getUserNameBytes() {
Object ref = userName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
userName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>string userName = 2;</code>
*/
public Builder setUserName(
String value) {
if (value == null) {
throw new NullPointerException();
} userName_ = value;
onChanged();
return this;
}
/**
* <code>string userName = 2;</code>
*/
public Builder clearUserName() { userName_ = getDefaultInstance().getUserName();
onChanged();
return this;
}
/**
* <code>string userName = 2;</code>
*/
public Builder setUserNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value); userName_ = value;
onChanged();
return this;
} private Object productName_ = "";
/**
* <code>string productName = 3;</code>
*/
public String getProductName() {
Object ref = productName_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
productName_ = s;
return s;
} else {
return (String) ref;
}
}
/**
* <code>string productName = 3;</code>
*/
public com.google.protobuf.ByteString
getProductNameBytes() {
Object ref = productName_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
productName_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>string productName = 3;</code>
*/
public Builder setProductName(
String value) {
if (value == null) {
throw new NullPointerException();
} productName_ = value;
onChanged();
return this;
}
/**
* <code>string productName = 3;</code>
*/
public Builder clearProductName() { productName_ = getDefaultInstance().getProductName();
onChanged();
return this;
}
/**
* <code>string productName = 3;</code>
*/
public Builder setProductNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value); productName_ = value;
onChanged();
return this;
} private com.google.protobuf.LazyStringList address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
private void ensureAddressIsMutable() {
if (!((bitField0_ & 0x00000008) == 0x00000008)) {
address_ = new com.google.protobuf.LazyStringArrayList(address_);
bitField0_ |= 0x00000008;
}
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ProtocolStringList
getAddressList() {
return address_.getUnmodifiableView();
}
/**
* <code>repeated string address = 4;</code>
*/
public int getAddressCount() {
return address_.size();
}
/**
* <code>repeated string address = 4;</code>
*/
public String getAddress(int index) {
return address_.get(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public com.google.protobuf.ByteString
getAddressBytes(int index) {
return address_.getByteString(index);
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder setAddress(
int index, String value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.set(index, value);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAddress(
String value) {
if (value == null) {
throw new NullPointerException();
}
ensureAddressIsMutable();
address_.add(value);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAllAddress(
Iterable<String> values) {
ensureAddressIsMutable();
com.google.protobuf.AbstractMessageLite.Builder.addAll(
values, address_);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder clearAddress() {
address_ = com.google.protobuf.LazyStringArrayList.EMPTY;
bitField0_ = (bitField0_ & ~0x00000008);
onChanged();
return this;
}
/**
* <code>repeated string address = 4;</code>
*/
public Builder addAddressBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value);
ensureAddressIsMutable();
address_.add(value);
onChanged();
return this;
}
@Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFieldsProto3(unknownFields);
} @Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
} // @@protoc_insertion_point(builder_scope:SubscribeReq)
} // @@protoc_insertion_point(class_scope:SubscribeReq)
private static final SubscribeReq DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new SubscribeReq();
} public static SubscribeReq getDefaultInstance() {
return DEFAULT_INSTANCE;
} private static final com.google.protobuf.Parser<SubscribeReq>
PARSER = new com.google.protobuf.AbstractParser<SubscribeReq>() {
@Override
public SubscribeReq parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new SubscribeReq(input, extensionRegistry);
}
}; public static com.google.protobuf.Parser<SubscribeReq> parser() {
return PARSER;
} @Override
public com.google.protobuf.Parser<SubscribeReq> getParserForType() {
return PARSER;
} @Override
public SubscribeReq getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
} } private static final com.google.protobuf.Descriptors.Descriptor
internal_static_SubscribeReq_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_SubscribeReq_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\022SubscribeReq.proto\"X\n\014SubscribeReq\022\020\n\010" +
"subReqID\030\001 \001(\005\022\020\n\010userName\030\002 \001(\t\022\023\n\013prod" +
"uctName\030\003 \001(\t\022\017\n\007address\030\004 \003(\tB!\n\014com.pr" +
"otobufB\021SubscribeReqProtob\006proto3"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
internal_static_SubscribeReq_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_SubscribeReq_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_SubscribeReq_descriptor,
new String[] { "SubReqID", "UserName", "ProductName", "Address", });
} // @@protoc_insertion_point(outer_class_scope)
}
Resp代码
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: SubscribeResp.proto package com.protobuf; public final class SubscribeRespProto {
private SubscribeRespProto() {}
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 SubscribeRespOrBuilder extends
// @@protoc_insertion_point(interface_extends:SubscribeResp)
com.google.protobuf.MessageOrBuilder { /**
* <code>int32 subReqID = 1;</code>
*/
int getSubReqID(); /**
* <code>int32 respcode = 2;</code>
*/
int getRespcode(); /**
* <code>string desc = 3;</code>
*/
String getDesc();
/**
* <code>string desc = 3;</code>
*/
com.google.protobuf.ByteString
getDescBytes();
}
/**
* Protobuf type {@code SubscribeResp}
*/
public static final class SubscribeResp extends
com.google.protobuf.GeneratedMessageV3 implements
// @@protoc_insertion_point(message_implements:SubscribeResp)
SubscribeRespOrBuilder {
private static final long serialVersionUID = 0L;
// Use SubscribeResp.newBuilder() to construct.
private SubscribeResp(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
super(builder);
}
private SubscribeResp() {
subReqID_ = 0;
respcode_ = 0;
desc_ = "";
} @Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private SubscribeResp(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
this();
if (extensionRegistry == null) {
throw new NullPointerException();
}
int mutable_bitField0_ = 0;
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: { subReqID_ = input.readInt32();
break;
}
case 16: { respcode_ = input.readInt32();
break;
}
case 26: {
String s = input.readStringRequireUtf8(); desc_ = s;
break;
}
default: {
if (!parseUnknownFieldProto3(
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 SubscribeRespProto.internal_static_SubscribeResp_descriptor;
} @Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeRespProto.internal_static_SubscribeResp_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeResp.class, Builder.class);
} public static final int SUBREQID_FIELD_NUMBER = 1;
private int subReqID_;
/**
* <code>int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
} public static final int RESPCODE_FIELD_NUMBER = 2;
private int respcode_;
/**
* <code>int32 respcode = 2;</code>
*/
public int getRespcode() {
return respcode_;
} public static final int DESC_FIELD_NUMBER = 3;
private volatile Object desc_;
/**
* <code>string desc = 3;</code>
*/
public String getDesc() {
Object ref = desc_;
if (ref instanceof String) {
return (String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
desc_ = s;
return s;
}
}
/**
* <code>string desc = 3;</code>
*/
public com.google.protobuf.ByteString
getDescBytes() {
Object ref = desc_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
desc_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} private byte memoizedIsInitialized = -1;
@Override
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false; memoizedIsInitialized = 1;
return true;
} @Override
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
if (subReqID_ != 0) {
output.writeInt32(1, subReqID_);
}
if (respcode_ != 0) {
output.writeInt32(2, respcode_);
}
if (!getDescBytes().isEmpty()) {
com.google.protobuf.GeneratedMessageV3.writeString(output, 3, desc_);
}
unknownFields.writeTo(output);
} @Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) return size; size = 0;
if (subReqID_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(1, subReqID_);
}
if (respcode_ != 0) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, respcode_);
}
if (!getDescBytes().isEmpty()) {
size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, desc_);
}
size += unknownFields.getSerializedSize();
memoizedSize = size;
return size;
} @Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof SubscribeResp)) {
return super.equals(obj);
}
SubscribeResp other = (SubscribeResp) obj; boolean result = true;
result = result && (getSubReqID()
== other.getSubReqID());
result = result && (getRespcode()
== other.getRespcode());
result = result && getDesc()
.equals(other.getDesc());
result = result && unknownFields.equals(other.unknownFields);
return result;
} @Override
public int hashCode() {
if (memoizedHashCode != 0) {
return memoizedHashCode;
}
int hash = 41;
hash = (19 * hash) + getDescriptor().hashCode();
hash = (37 * hash) + SUBREQID_FIELD_NUMBER;
hash = (53 * hash) + getSubReqID();
hash = (37 * hash) + RESPCODE_FIELD_NUMBER;
hash = (53 * hash) + getRespcode();
hash = (37 * hash) + DESC_FIELD_NUMBER;
hash = (53 * hash) + getDesc().hashCode();
hash = (29 * hash) + unknownFields.hashCode();
memoizedHashCode = hash;
return hash;
} public static SubscribeResp parseFrom(
java.nio.ByteBuffer data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeResp parseFrom(
java.nio.ByteBuffer data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeResp parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeResp parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeResp parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static SubscribeResp parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static SubscribeResp parseFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeResp 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 SubscribeResp parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseDelimitedWithIOException(PARSER, input);
}
public static SubscribeResp 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 SubscribeResp parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input);
}
public static SubscribeResp parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return com.google.protobuf.GeneratedMessageV3
.parseWithIOException(PARSER, input, extensionRegistry);
} @Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {
return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(SubscribeResp prototype) {
return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
}
@Override
public Builder toBuilder() {
return this == DEFAULT_INSTANCE
? new Builder() : new Builder().mergeFrom(this);
} @Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code SubscribeResp}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:SubscribeResp)
SubscribeRespOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return SubscribeRespProto.internal_static_SubscribeResp_descriptor;
} @Override
protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internalGetFieldAccessorTable() {
return SubscribeRespProto.internal_static_SubscribeResp_fieldAccessorTable
.ensureFieldAccessorsInitialized(
SubscribeResp.class, Builder.class);
} // Construct using com.protobuf.SubscribeRespProto.SubscribeResp.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
} private Builder(
com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessageV3
.alwaysUseFieldBuilders) {
}
}
@Override
public Builder clear() {
super.clear();
subReqID_ = 0; respcode_ = 0; desc_ = ""; return this;
} @Override
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return SubscribeRespProto.internal_static_SubscribeResp_descriptor;
} @Override
public SubscribeResp getDefaultInstanceForType() {
return SubscribeResp.getDefaultInstance();
} @Override
public SubscribeResp build() {
SubscribeResp result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
} @Override
public SubscribeResp buildPartial() {
SubscribeResp result = new SubscribeResp(this);
result.subReqID_ = subReqID_;
result.respcode_ = respcode_;
result.desc_ = desc_;
onBuilt();
return result;
} @Override
public Builder clone() {
return (Builder) super.clone();
}
@Override
public Builder setField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return (Builder) super.setField(field, value);
}
@Override
public Builder clearField(
com.google.protobuf.Descriptors.FieldDescriptor field) {
return (Builder) super.clearField(field);
}
@Override
public Builder clearOneof(
com.google.protobuf.Descriptors.OneofDescriptor oneof) {
return (Builder) super.clearOneof(oneof);
}
@Override
public Builder setRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
int index, Object value) {
return (Builder) super.setRepeatedField(field, index, value);
}
@Override
public Builder addRepeatedField(
com.google.protobuf.Descriptors.FieldDescriptor field,
Object value) {
return (Builder) super.addRepeatedField(field, value);
}
@Override
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof SubscribeResp) {
return mergeFrom((SubscribeResp)other);
} else {
super.mergeFrom(other);
return this;
}
} public Builder mergeFrom(SubscribeResp other) {
if (other == SubscribeResp.getDefaultInstance()) return this;
if (other.getSubReqID() != 0) {
setSubReqID(other.getSubReqID());
}
if (other.getRespcode() != 0) {
setRespcode(other.getRespcode());
}
if (!other.getDesc().isEmpty()) {
desc_ = other.desc_;
onChanged();
}
this.mergeUnknownFields(other.unknownFields);
onChanged();
return this;
} @Override
public final boolean isInitialized() {
return true;
} @Override
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
SubscribeResp parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (SubscribeResp) e.getUnfinishedMessage();
throw e.unwrapIOException();
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
} private int subReqID_ ;
/**
* <code>int32 subReqID = 1;</code>
*/
public int getSubReqID() {
return subReqID_;
}
/**
* <code>int32 subReqID = 1;</code>
*/
public Builder setSubReqID(int value) { subReqID_ = value;
onChanged();
return this;
}
/**
* <code>int32 subReqID = 1;</code>
*/
public Builder clearSubReqID() { subReqID_ = 0;
onChanged();
return this;
} private int respcode_ ;
/**
* <code>int32 respcode = 2;</code>
*/
public int getRespcode() {
return respcode_;
}
/**
* <code>int32 respcode = 2;</code>
*/
public Builder setRespcode(int value) { respcode_ = value;
onChanged();
return this;
}
/**
* <code>int32 respcode = 2;</code>
*/
public Builder clearRespcode() { respcode_ = 0;
onChanged();
return this;
} private Object desc_ = "";
/**
* <code>string desc = 3;</code>
*/
public String getDesc() {
Object ref = desc_;
if (!(ref instanceof String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
String s = bs.toStringUtf8();
desc_ = s;
return s;
} else {
return (String) ref;
}
}
/**
* <code>string desc = 3;</code>
*/
public com.google.protobuf.ByteString
getDescBytes() {
Object ref = desc_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(String) ref);
desc_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>string desc = 3;</code>
*/
public Builder setDesc(
String value) {
if (value == null) {
throw new NullPointerException();
} desc_ = value;
onChanged();
return this;
}
/**
* <code>string desc = 3;</code>
*/
public Builder clearDesc() { desc_ = getDefaultInstance().getDesc();
onChanged();
return this;
}
/**
* <code>string desc = 3;</code>
*/
public Builder setDescBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value); desc_ = value;
onChanged();
return this;
}
@Override
public final Builder setUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.setUnknownFieldsProto3(unknownFields);
} @Override
public final Builder mergeUnknownFields(
final com.google.protobuf.UnknownFieldSet unknownFields) {
return super.mergeUnknownFields(unknownFields);
} // @@protoc_insertion_point(builder_scope:SubscribeResp)
} // @@protoc_insertion_point(class_scope:SubscribeResp)
private static final SubscribeResp DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = new SubscribeResp();
} public static SubscribeResp getDefaultInstance() {
return DEFAULT_INSTANCE;
} private static final com.google.protobuf.Parser<SubscribeResp>
PARSER = new com.google.protobuf.AbstractParser<SubscribeResp>() {
@Override
public SubscribeResp parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new SubscribeResp(input, extensionRegistry);
}
}; public static com.google.protobuf.Parser<SubscribeResp> parser() {
return PARSER;
} @Override
public com.google.protobuf.Parser<SubscribeResp> getParserForType() {
return PARSER;
} @Override
public SubscribeResp getDefaultInstanceForType() {
return DEFAULT_INSTANCE;
} } private static final com.google.protobuf.Descriptors.Descriptor
internal_static_SubscribeResp_descriptor;
private static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_SubscribeResp_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\023SubscribeResp.proto\"A\n\rSubscribeResp\022\020" +
"\n\010subReqID\030\001 \001(\005\022\020\n\010respcode\030\002 \001(\005\022\014\n\004de" +
"sc\030\003 \001(\tB\"\n\014com.protobufB\022SubscribeRespP" +
"rotob\006proto3"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
internal_static_SubscribeResp_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_SubscribeResp_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_SubscribeResp_descriptor,
new String[] { "SubReqID", "Respcode", "Desc", });
} // @@protoc_insertion_point(outer_class_scope)
}
测试代码
package com.protobuf; import com.google.protobuf.InvalidProtocolBufferException; import java.util.ArrayList;
import java.util.List; public class TestSubScribeReqProto {
private static byte[] encode(SubscribeReqProto.SubscribeReq req){
return req.toByteArray();
}
private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
return SubscribeReqProto.SubscribeReq.parseFrom(body);
}
private static SubscribeReqProto.SubscribeReq createSubscribeReq(){
SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
builder.setSubReqID(1);
builder.setUserName("醉逍遥");
builder.setProductName("Netty buffer");
List<String> address = new ArrayList<>();
address.add("beijing tiananmen");
address.add("shanghai huangpujiang");
address.add("tianjin binhaixinqu");
address.add("shenzhen houhai");
builder.addAllAddress(address);
return builder.build();
} public static void main(String[] args) throws InvalidProtocolBufferException {
SubscribeReqProto.SubscribeReq req = createSubscribeReq();
System.out.println("解码之前:"+req.toString());
byte[] b = encode(req);
System.out.println("编码后:"+b.toString());
SubscribeReqProto.SubscribeReq req1 = decode(b);
System.out.println("解码后:"+req1);
System.out.println("解码前后是否相等:"+req.equals(req1));
}
}
运行结果
这里运行后已经完全没问题了,只是在userName上的汉字没有显示为汉字,在这里暂时不理会他。
三、采用netty进行实例演示
服务端代码
package com.protobuf; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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 SubReqServer {
public void bind(int port) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup,workGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder())
.addLast(new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()))
.addLast(new ProtobufVarint32LengthFieldPrepender())
.addLast(new ProtobufEncoder())
.addLast(new SubReqServerHandler());
}
}); ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
} public static void main(String[] args) throws InterruptedException {
int port =8080;
if (args!=null&&args.length>0){
try{
port = Integer.valueOf(args[0]);
}catch (NumberFormatException e){ }
}
new SubReqServer().bind(port);
}
}
服务端Handler代码
package com.protobuf; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; public class SubReqServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq) msg;
if ("zuixiaoyao".equals(req.getUserName())){
System.out.println("服务端接收到客户端SubcribeReq的请求:【"+req.toString()+"】");
ctx.writeAndFlush(resp(req.getSubReqID()));
}else {
System.out.println("用户名不相同!");
} } private SubscribeRespProto.SubscribeResp resp(int subReqID) {
SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder();
builder.setRespcode(0);
builder.setSubReqID(subReqID);
builder.setDesc("order success!");
return builder.build();
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
客户端代码
package com.protobuf; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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 SubReqClient {
public void connect(int port, String host) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY,true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder())
.addLast(new ProtobufDecoder(SubscribeRespProto.SubscribeResp.getDefaultInstance()))
.addLast(new ProtobufVarint32LengthFieldPrepender())
.addLast(new ProtobufEncoder())
.addLast(new SubReqClientHandler());
}
});
ChannelFuture f = b.connect(host,port).sync();
f.channel().closeFuture().sync();
}finally {
group.shutdownGracefully();
} } public static void main(String[] args) throws InterruptedException {
int port = 8080;
try{
if(args.length>0 && args!=null){
port = Integer.valueOf(args[0]);
}
}catch (NumberFormatException e){ }
new SubReqClient().connect(port,"127.0.0.1");
}
}
客户端Handler代码
package com.protobuf; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.ArrayList;
import java.util.List; public class SubReqClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for(int i=0;i<10;i++){
ctx.write(subReq(i));
}
ctx.flush();
} private SubscribeReqProto.SubscribeReq subReq(int i) {
SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
builder.setUserName("zuixiaoyao");
builder.setProductName("xiguahong");
builder.setSubReqID(i);
List<String> address = new ArrayList<>();
address.add("tianjin hongqiao");
address.add("henan luoyang");
address.add("shenzhen luohu");
address.add("hebei qinhuangdao");
builder.addAllAddress(address);
return builder.build();
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("接收服务端相应:【"+msg+"】");
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
运行结果:
服务端
D:\java\java1.8\jdk1.8\bin\java.exe "-javaagent:D:\我的软件\itSoft\IntelliJ IDEA\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar=63669:D:\我的软件\itSoft\IntelliJ IDEA\IntelliJ IDEA 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath D:\java\java1.8\jdk1.8\jre\lib\charsets.jar;D:\java\java1.8\jdk1.8\jre\lib\deploy.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\cldrdata.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\dnsns.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\jaccess.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\jfxrt.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\localedata.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\nashorn.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunec.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\zipfs.jar;D:\java\java1.8\jdk1.8\jre\lib\javaws.jar;D:\java\java1.8\jdk1.8\jre\lib\jce.jar;D:\java\java1.8\jdk1.8\jre\lib\jfr.jar;D:\java\java1.8\jdk1.8\jre\lib\jfxswt.jar;D:\java\java1.8\jdk1.8\jre\lib\jsse.jar;D:\java\java1.8\jdk1.8\jre\lib\management-agent.jar;D:\java\java1.8\jdk1.8\jre\lib\plugin.jar;D:\java\java1.8\jdk1.8\jre\lib\resources.jar;D:\java\java1.8\jdk1.8\jre\lib\rt.jar;D:\netty\target\classes;D:\jboss-marshalling-1.3.0.CR9.jar;D:\jboss-marshalling-serial-1.3.0.CR9.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-data-redis\2.0.3.RELEASE\spring-boot-starter-data-redis-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.3.RELEASE\spring-boot-starter-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot\2.0.3.RELEASE\spring-boot-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.3.RELEASE\spring-boot-autoconfigure-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.3.RELEASE\spring-boot-starter-logging-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\litan\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\litan\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\litan\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\litan\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\litan\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\litan\.m2\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;C:\Users\litan\.m2\repository\org\springframework\data\spring-data-redis\2.0.8.RELEASE\spring-data-redis-2.0.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\data\spring-data-keyvalue\2.0.8.RELEASE\spring-data-keyvalue-2.0.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\data\spring-data-commons\2.0.8.RELEASE\spring-data-commons-2.0.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-tx\5.0.7.RELEASE\spring-tx-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-oxm\5.0.7.RELEASE\spring-oxm-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-aop\5.0.7.RELEASE\spring-aop-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-context-support\5.0.7.RELEASE\spring-context-support-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\litan\.m2\repository\io\lettuce\lettuce-core\5.0.4.RELEASE\lettuce-core-5.0.4.RELEASE.jar;C:\Users\litan\.m2\repository\io\projectreactor\reactor-core\3.1.8.RELEASE\reactor-core-3.1.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar;C:\Users\litan\.m2\repository\io\netty\netty-common\4.1.25.Final\netty-common-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-transport\4.1.25.Final\netty-transport-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-buffer\4.1.25.Final\netty-buffer-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-resolver\4.1.25.Final\netty-resolver-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-handler\4.1.25.Final\netty-handler-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-codec\4.1.25.Final\netty-codec-4.1.25.Final.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.0.3.RELEASE\spring-boot-starter-web-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.0.3.RELEASE\spring-boot-starter-json-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.0.3.RELEASE\spring-boot-starter-tomcat-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.31\tomcat-embed-core-8.5.31.jar;C:\Users\litan\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.31\tomcat-embed-el-8.5.31.jar;C:\Users\litan\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.31\tomcat-embed-websocket-8.5.31.jar;C:\Users\litan\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.10.Final\hibernate-validator-6.0.10.Final.jar;C:\Users\litan\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\litan\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\litan\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\litan\.m2\repository\org\springframework\spring-web\5.0.7.RELEASE\spring-web-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-beans\5.0.7.RELEASE\spring-beans-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-webmvc\5.0.7.RELEASE\spring-webmvc-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-context\5.0.7.RELEASE\spring-context-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-expression\5.0.7.RELEASE\spring-expression-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\litan\.m2\repository\org\springframework\spring-core\5.0.7.RELEASE\spring-core-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-jcl\5.0.7.RELEASE\spring-jcl-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\io\netty\netty-all\4.1.25.Final\netty-all-4.1.25.Final.jar;C:\Users\litan\.m2\repository\org\msgpack\msgpack\0.6.11\msgpack-0.6.11.jar;C:\Users\litan\.m2\repository\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar;C:\Users\litan\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;C:\Users\litan\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar com.protobuf.SubReqServer
21:00:33.990 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
21:00:34.000 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
21:00:34.122 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
21:00:34.122 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
21:00:34.184 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
21:00:34.186 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
21:00:34.187 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
21:00:34.189 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
21:00:34.191 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
21:00:34.192 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
21:00:34.194 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
21:00:34.196 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\litan\AppData\Local\Temp (java.io.tmpdir)
21:00:34.197 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
21:00:34.200 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
21:00:34.200 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1890582528 bytes
21:00:34.200 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
21:00:34.201 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
21:00:34.227 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
21:00:35.015 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 19192 (auto-detected)
21:00:35.020 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
21:00:35.020 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
21:00:35.530 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
21:00:35.531 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
21:00:36.148 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:c0:00:01 (auto-detected)
21:00:36.167 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
21:00:36.167 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
21:00:36.187 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
21:00:36.187 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
21:00:36.277 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
21:00:36.277 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
21:00:36.277 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
21:00:36.327 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x9032f6b1] REGISTERED
21:00:36.330 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x9032f6b1] BIND: 0.0.0.0/0.0.0.0:8080
21:00:36.334 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x9032f6b1, L:/0:0:0:0:0:0:0:0:8080] ACTIVE
21:00:40.586 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x9032f6b1, L:/0:0:0:0:0:0:0:0:8080] READ: [id: 0xe585fe82, L:/127.0.0.1:8080 - R:/127.0.0.1:63737]
21:00:40.587 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x9032f6b1, L:/0:0:0:0:0:0:0:0:8080] READ COMPLETE
21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
21:00:40.742 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
21:00:40.744 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@7d129f4c
服务端接收到客户端SubcribeReq的请求:【userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 1
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 2
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 3
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 4
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 5
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 6
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 7
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 8
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
服务端接收到客户端SubcribeReq的请求:【subReqID: 9
userName: "zuixiaoyao"
productName: "xiguahong"
address: "tianjin hongqiao"
address: "henan luoyang"
address: "shenzhen luohu"
address: "hebei qinhuangdao"
】
客户端
D:\java\java1.8\jdk1.8\bin\java.exe "-javaagent:D:\我的软件\itSoft\IntelliJ IDEA\IntelliJ IDEA 2018.1.5\lib\idea_rt.jar=63711:D:\我的软件\itSoft\IntelliJ IDEA\IntelliJ IDEA 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath D:\java\java1.8\jdk1.8\jre\lib\charsets.jar;D:\java\java1.8\jdk1.8\jre\lib\deploy.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\cldrdata.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\dnsns.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\jaccess.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\jfxrt.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\localedata.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\nashorn.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunec.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\java\java1.8\jdk1.8\jre\lib\ext\zipfs.jar;D:\java\java1.8\jdk1.8\jre\lib\javaws.jar;D:\java\java1.8\jdk1.8\jre\lib\jce.jar;D:\java\java1.8\jdk1.8\jre\lib\jfr.jar;D:\java\java1.8\jdk1.8\jre\lib\jfxswt.jar;D:\java\java1.8\jdk1.8\jre\lib\jsse.jar;D:\java\java1.8\jdk1.8\jre\lib\management-agent.jar;D:\java\java1.8\jdk1.8\jre\lib\plugin.jar;D:\java\java1.8\jdk1.8\jre\lib\resources.jar;D:\java\java1.8\jdk1.8\jre\lib\rt.jar;D:\netty\target\classes;D:\jboss-marshalling-1.3.0.CR9.jar;D:\jboss-marshalling-serial-1.3.0.CR9.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-data-redis\2.0.3.RELEASE\spring-boot-starter-data-redis-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter\2.0.3.RELEASE\spring-boot-starter-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot\2.0.3.RELEASE\spring-boot-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.3.RELEASE\spring-boot-autoconfigure-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.0.3.RELEASE\spring-boot-starter-logging-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\litan\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\litan\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\litan\.m2\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\litan\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\litan\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\litan\.m2\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;C:\Users\litan\.m2\repository\org\springframework\data\spring-data-redis\2.0.8.RELEASE\spring-data-redis-2.0.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\data\spring-data-keyvalue\2.0.8.RELEASE\spring-data-keyvalue-2.0.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\data\spring-data-commons\2.0.8.RELEASE\spring-data-commons-2.0.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-tx\5.0.7.RELEASE\spring-tx-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-oxm\5.0.7.RELEASE\spring-oxm-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-aop\5.0.7.RELEASE\spring-aop-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-context-support\5.0.7.RELEASE\spring-context-support-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\litan\.m2\repository\io\lettuce\lettuce-core\5.0.4.RELEASE\lettuce-core-5.0.4.RELEASE.jar;C:\Users\litan\.m2\repository\io\projectreactor\reactor-core\3.1.8.RELEASE\reactor-core-3.1.8.RELEASE.jar;C:\Users\litan\.m2\repository\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar;C:\Users\litan\.m2\repository\io\netty\netty-common\4.1.25.Final\netty-common-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-transport\4.1.25.Final\netty-transport-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-buffer\4.1.25.Final\netty-buffer-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-resolver\4.1.25.Final\netty-resolver-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-handler\4.1.25.Final\netty-handler-4.1.25.Final.jar;C:\Users\litan\.m2\repository\io\netty\netty-codec\4.1.25.Final\netty-codec-4.1.25.Final.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.0.3.RELEASE\spring-boot-starter-web-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.0.3.RELEASE\spring-boot-starter-json-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;C:\Users\litan\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;C:\Users\litan\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.0.3.RELEASE\spring-boot-starter-tomcat-2.0.3.RELEASE.jar;C:\Users\litan\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.31\tomcat-embed-core-8.5.31.jar;C:\Users\litan\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.31\tomcat-embed-el-8.5.31.jar;C:\Users\litan\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.31\tomcat-embed-websocket-8.5.31.jar;C:\Users\litan\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.10.Final\hibernate-validator-6.0.10.Final.jar;C:\Users\litan\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\litan\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\litan\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\litan\.m2\repository\org\springframework\spring-web\5.0.7.RELEASE\spring-web-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-beans\5.0.7.RELEASE\spring-beans-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-webmvc\5.0.7.RELEASE\spring-webmvc-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-context\5.0.7.RELEASE\spring-context-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-expression\5.0.7.RELEASE\spring-expression-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\litan\.m2\repository\org\springframework\spring-core\5.0.7.RELEASE\spring-core-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\org\springframework\spring-jcl\5.0.7.RELEASE\spring-jcl-5.0.7.RELEASE.jar;C:\Users\litan\.m2\repository\io\netty\netty-all\4.1.25.Final\netty-all-4.1.25.Final.jar;C:\Users\litan\.m2\repository\org\msgpack\msgpack\0.6.11\msgpack-0.6.11.jar;C:\Users\litan\.m2\repository\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar;C:\Users\litan\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;C:\Users\litan\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar com.protobuf.SubReqClient
21:00:38.579 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
21:00:38.595 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
21:00:38.674 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
21:00:38.674 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
21:00:38.723 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
21:00:38.740 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
21:00:38.740 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
21:00:38.744 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
21:00:38.746 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
21:00:38.748 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
21:00:38.749 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
21:00:38.752 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\litan\AppData\Local\Temp (java.io.tmpdir)
21:00:38.753 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
21:00:38.755 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
21:00:38.755 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1890582528 bytes
21:00:38.756 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
21:00:38.757 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
21:00:38.784 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
21:00:39.434 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 10388 (auto-detected)
21:00:39.437 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
21:00:39.437 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
21:00:39.752 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
21:00:39.753 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
21:00:40.352 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:c0:00:01 (auto-detected)
21:00:40.363 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
21:00:40.363 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
21:00:40.388 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
21:00:40.388 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
21:00:40.468 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
21:00:40.469 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
21:00:40.469 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
21:00:40.682 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
21:00:40.684 [nioEventLoopGroup-2-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@a74f5ae
21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
接收服务端相应:【desc: "order success!"
】
接收服务端相应:【subReqID: 1
desc: "order success!"
】
接收服务端相应:【subReqID: 2
desc: "order success!"
】
接收服务端相应:【subReqID: 3
desc: "order success!"
】
接收服务端相应:【subReqID: 4
desc: "order success!"
】
接收服务端相应:【subReqID: 5
desc: "order success!"
】
接收服务端相应:【subReqID: 6
desc: "order success!"
】
接收服务端相应:【subReqID: 7
desc: "order success!"
】
接收服务端相应:【subReqID: 8
desc: "order success!"
】
接收服务端相应:【subReqID: 9
desc: "order success!"
】
至此,全部完成,折叠的代码主要为运行的不是很需要重点阅读的代码,折叠起来以减小篇幅,在书写代码的过程中,客户端和服务端编解码等框架在pipeline.add时候的对称性要注意,否则会导致服务跑不起来。
补充一下书上的注意事项:
ProtobufDecoder仅仅负责解码,它不支持读半包,因此,在ProtobufDecoder前面,一定要有能够处理读半包的解码器,有以下三种方式可以选择。
1.使用Netty提供的ProtobufVarint32FrameDecoder,它可以处理半包消息;
2.继承Netty提供的通用半包解码器LenthFieldBasedFrameDecoder;
3.继承ByteToMessageDecoder类,自己处理半包消息。
netty权威指南学习笔记七——编解码技术之GoogleProtobuf的更多相关文章
- netty权威指南学习笔记六——编解码技术之MessagePack
编解码技术主要应用在网络传输中,将对象比如BOJO进行编解码以利于网络中进行传输.平常我们也会将编解码说成是序列化/反序列化 定义:当进行远程跨进程服务调用时,需要把被传输的java对象编码为字节数组 ...
- netty权威指南学习笔记八——编解码技术之JBoss Marshalling
JBoss Marshalling 是一个java序列化包,对JDK默认的序列化框架进行了优化,但又保持跟java.io.Serializable接口的兼容,同时增加了一些可调参数和附加特性,这些参数 ...
- netty权威指南学习笔记二——netty入门应用
经过了前面的NIO基础知识准备,我们已经对NIO有了较大了解,现在就进入netty的实际应用中来看看吧.重点体会整个过程. 按照权威指南写程序的过程中,发现一些问题:当我们在定义handler继承Ch ...
- netty权威指南学习笔记五——分隔符和定长解码器的应用
TCP以流的方式进行数据传输,上层应用协议为了对消息进行区分,通常采用以下4中方式: 消息长度固定,累计读取到长度综合为定长LEN的报文后,就认为读取到了一个完整的消息,将计数器置位,重新开始读取下一 ...
- netty权威指南学习笔记四——TCP粘包/拆包之粘包问题解决
发生了粘包,我们需要将其清晰的进行拆包处理,这里采用LineBasedFrameDecoder来解决 LineBasedFrameDecoder的工作原理是它依次遍历ByteBuf中的可读字节,判断看 ...
- netty权威指南学习笔记三——TCP粘包/拆包之粘包现象
TCP是个流协议,流没有一定界限.TCP底层不了解业务,他会根据TCP缓冲区的实际情况进行包划分,在业务上,一个业务完整的包,可能会被TCP底层拆分为多个包进行发送,也可能多个小包组合成一个大的数据包 ...
- netty权威指南学习笔记一——NIO入门(3)NIO
经过前面的铺垫,在这一节我们进入NIO编程,NIO弥补了原来同步阻塞IO的不足,他提供了高速的.面向块的I/O,NIO中加入的Buffer缓冲区,体现了与原I/O的一个重要区别.在面向流的I/O中,可 ...
- netty权威指南学习笔记一——NIO入门(1)BIO
公司的一些项目采用了netty框架,为了加速适应公司开发,本博主认真学习netty框架,前一段时间主要看了看书,发现编程这东西,不上手还是觉得差点什么,于是为了加深理解,深入学习,本博主还是决定多动手 ...
- IDA Pro 权威指南学习笔记(七) - 其他 IDA 显示窗口
默认情况下 IDA 不会打开这些窗口,可通过 View -> Open Subviews 命令打开 Strings 窗口(Strings) Strings 窗口中显示的是从二进制文件中提取出的一 ...
随机推荐
- JSON转换的实现
String转成JSON这个依赖很重要,我们将围绕fastjson中的JSONObject这个类来谈转换 <dependency> <groupId>com.alibaba&l ...
- java各种面试问题
二.Java多线程相关 线程池的原理,为什么要创建线程池?创建线程池的方式: 线程的生命周期,什么时候会出现僵死进程: 说说线程安全问题,什么实现线程安全,如何实现线程安全: 创建线程池有哪几个核心参 ...
- IDEA导入项目后,导入artifacts 方法 以及 Spring的配置文件找不到的解决方法
我们一般选择 open 项目,如果没有artifacts 的添加选项,我们就要选择 import 项目. 如果没有artifacts ,项目下面会有错误提示,点击错误提示Fix,设置里面导入artif ...
- Spring Schedule 实现定时任务
很多时候我们都需要为系统建立一个定时任务来帮我们做一些事情,SpringBoot 已经帮我们实现好了一个,我们只需要直接使用即可,当然你也可以不用 SpringBoot 自带的定时任务,整合 Quar ...
- Python 操作rabbitmq中的confirm模式的错误
今天使用rabbitmq的confirm模式,书上介绍的是pika版本是0.9.6,但是我用的是别的版本,发现这样的错误 Traceback (most recent call last): Fil ...
- 【剑指Offer面试编程题】题目1516:调整数组顺序使奇数位于偶数前面--九度OJ
题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 输入: 每个输 ...
- jQuery设置input的type属性
$("#inputName").attr("type","text");
- 1-7SpringBoot之表单验证@Valid
SpringBoot提供了强大的表单验证功能实现,给我们省去了写验证的麻烦: 这里我们给下实例,提交一个有姓名和年龄的表单添加功能, 要求姓名不能为空,年龄必须是不小于18 : 我们先新建一个Stud ...
- MVC5仓库管理系统
下载
- 题解 P3258 【[JLOI2014]松鼠的新家】
树链剖分板子题 先说点别的 小熊维尼啊,嘿嘿嘿. 写题经历 悲惨命运:树剖调了2天,一直90分,死活不AC,调出了心病,快下课时改了一下数据范围,A了--.(刚开始数组开了800100,改120010 ...