首先我们来看一下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模板加入进来,一定要注意语法不能写错否则执行命令就会报错
1 syntax = "proto3"; 2 option java_package="com.protobuf"; 3 option java_outer_classname="DemoProto"; 4 5 message Demo{ 6 int32 subReqID=1; 7 string userName=2; 8 string productName=3; 9 string address=4; 10 }
执行命令如下: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代码
1 syntax = "proto3"; 2 option java_package="com.protobuf"; 3 option java_outer_classname="SubscribeReqProto"; 4 5 message SubscribeReq{ 6 int32 subReqID=1; 7 string userName=2; 8 string productName=3; 9 repeated string address=4; 10 }
Resp代码
1 syntax = "proto3"; 2 option java_package="com.protobuf"; 3 option java_outer_classname="SubscribeRespProto"; 4 5 message SubscribeResp{ 6 int32 subReqID=1; 7 int32 respcode=2; 8 string desc=3; 9 }
执行
执行后的目录及代码
我们将后两个文件拷贝到相应工程中,发现报错,这里需要的是一个protobuf-java包,然后到官网找相应版本的包,该版本3.6.1的包在maven中可以找到,直接拿来用
1 <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java --> 2 <dependency> 3 <groupId>com.google.protobuf</groupId> 4 <artifactId>protobuf-java</artifactId> 5 <version>3.6.1</version> 6 </dependency>
maven自动更新完成后一切ok,到这里,protobuf环境已经可以使用了,即便将来再有新的pojo也可以轻松搞定。
二、测试protobuf的编解码效果
先看一下protobuf转为java后的代码,代码比较长折叠起来
Req代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // Generated by the protocol buffer compiler. DO NOT EDIT! 2 // source: SubscribeReq.proto 3 4 package com.protobuf; 5 6 public final class SubscribeReqProto { 7 private SubscribeReqProto() {} 8 public static void registerAllExtensions( 9 com.google.protobuf.ExtensionRegistryLite registry) { 10 } 11 12 public static void registerAllExtensions( 13 com.google.protobuf.ExtensionRegistry registry) { 14 registerAllExtensions( 15 (com.google.protobuf.ExtensionRegistryLite) registry); 16 } 17 public interface SubscribeReqOrBuilder extends 18 // @@protoc_insertion_point(interface_extends:SubscribeReq) 19 com.google.protobuf.MessageOrBuilder { 20 21 /** 22 * <code>int32 subReqID = 1;</code> 23 */ 24 int getSubReqID(); 25 26 /** 27 * <code>string userName = 2;</code> 28 */ 29 String getUserName(); 30 /** 31 * <code>string userName = 2;</code> 32 */ 33 com.google.protobuf.ByteString 34 getUserNameBytes(); 35 36 /** 37 * <code>string productName = 3;</code> 38 */ 39 String getProductName(); 40 /** 41 * <code>string productName = 3;</code> 42 */ 43 com.google.protobuf.ByteString 44 getProductNameBytes(); 45 46 /** 47 * <code>repeated string address = 4;</code> 48 */ 49 java.util.List<String> 50 getAddressList(); 51 /** 52 * <code>repeated string address = 4;</code> 53 */ 54 int getAddressCount(); 55 /** 56 * <code>repeated string address = 4;</code> 57 */ 58 String getAddress(int index); 59 /** 60 * <code>repeated string address = 4;</code> 61 */ 62 com.google.protobuf.ByteString 63 getAddressBytes(int index); 64 } 65 /** 66 * Protobuf type {@code SubscribeReq} 67 */ 68 public static final class SubscribeReq extends 69 com.google.protobuf.GeneratedMessageV3 implements 70 // @@protoc_insertion_point(message_implements:SubscribeReq) 71 SubscribeReqOrBuilder { 72 private static final long serialVersionUID = 0L; 73 // Use SubscribeReq.newBuilder() to construct. 74 private SubscribeReq(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) { 75 super(builder); 76 } 77 private SubscribeReq() { 78 subReqID_ = 0; 79 userName_ = ""; 80 productName_ = ""; 81 address_ = com.google.protobuf.LazyStringArrayList.EMPTY; 82 } 83 84 @Override 85 public final com.google.protobuf.UnknownFieldSet 86 getUnknownFields() { 87 return this.unknownFields; 88 } 89 private SubscribeReq( 90 com.google.protobuf.CodedInputStream input, 91 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 92 throws com.google.protobuf.InvalidProtocolBufferException { 93 this(); 94 if (extensionRegistry == null) { 95 throw new NullPointerException(); 96 } 97 int mutable_bitField0_ = 0; 98 com.google.protobuf.UnknownFieldSet.Builder unknownFields = 99 com.google.protobuf.UnknownFieldSet.newBuilder(); 100 try { 101 boolean done = false; 102 while (!done) { 103 int tag = input.readTag(); 104 switch (tag) { 105 case 0: 106 done = true; 107 break; 108 case 8: { 109 110 subReqID_ = input.readInt32(); 111 break; 112 } 113 case 18: { 114 String s = input.readStringRequireUtf8(); 115 116 userName_ = s; 117 break; 118 } 119 case 26: { 120 String s = input.readStringRequireUtf8(); 121 122 productName_ = s; 123 break; 124 } 125 case 34: { 126 String s = input.readStringRequireUtf8(); 127 if (!((mutable_bitField0_ & 0x00000008) == 0x00000008)) { 128 address_ = new com.google.protobuf.LazyStringArrayList(); 129 mutable_bitField0_ |= 0x00000008; 130 } 131 address_.add(s); 132 break; 133 } 134 default: { 135 if (!parseUnknownFieldProto3( 136 input, unknownFields, extensionRegistry, tag)) { 137 done = true; 138 } 139 break; 140 } 141 } 142 } 143 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 144 throw e.setUnfinishedMessage(this); 145 } catch (java.io.IOException e) { 146 throw new com.google.protobuf.InvalidProtocolBufferException( 147 e).setUnfinishedMessage(this); 148 } finally { 149 if (((mutable_bitField0_ & 0x00000008) == 0x00000008)) { 150 address_ = address_.getUnmodifiableView(); 151 } 152 this.unknownFields = unknownFields.build(); 153 makeExtensionsImmutable(); 154 } 155 } 156 public static final com.google.protobuf.Descriptors.Descriptor 157 getDescriptor() { 158 return SubscribeReqProto.internal_static_SubscribeReq_descriptor; 159 } 160 161 @Override 162 protected FieldAccessorTable 163 internalGetFieldAccessorTable() { 164 return SubscribeReqProto.internal_static_SubscribeReq_fieldAccessorTable 165 .ensureFieldAccessorsInitialized( 166 SubscribeReq.class, Builder.class); 167 } 168 169 private int bitField0_; 170 public static final int SUBREQID_FIELD_NUMBER = 1; 171 private int subReqID_; 172 /** 173 * <code>int32 subReqID = 1;</code> 174 */ 175 public int getSubReqID() { 176 return subReqID_; 177 } 178 179 public static final int USERNAME_FIELD_NUMBER = 2; 180 private volatile Object userName_; 181 /** 182 * <code>string userName = 2;</code> 183 */ 184 public String getUserName() { 185 Object ref = userName_; 186 if (ref instanceof String) { 187 return (String) ref; 188 } else { 189 com.google.protobuf.ByteString bs = 190 (com.google.protobuf.ByteString) ref; 191 String s = bs.toStringUtf8(); 192 userName_ = s; 193 return s; 194 } 195 } 196 /** 197 * <code>string userName = 2;</code> 198 */ 199 public com.google.protobuf.ByteString 200 getUserNameBytes() { 201 Object ref = userName_; 202 if (ref instanceof String) { 203 com.google.protobuf.ByteString b = 204 com.google.protobuf.ByteString.copyFromUtf8( 205 (String) ref); 206 userName_ = b; 207 return b; 208 } else { 209 return (com.google.protobuf.ByteString) ref; 210 } 211 } 212 213 public static final int PRODUCTNAME_FIELD_NUMBER = 3; 214 private volatile Object productName_; 215 /** 216 * <code>string productName = 3;</code> 217 */ 218 public String getProductName() { 219 Object ref = productName_; 220 if (ref instanceof String) { 221 return (String) ref; 222 } else { 223 com.google.protobuf.ByteString bs = 224 (com.google.protobuf.ByteString) ref; 225 String s = bs.toStringUtf8(); 226 productName_ = s; 227 return s; 228 } 229 } 230 /** 231 * <code>string productName = 3;</code> 232 */ 233 public com.google.protobuf.ByteString 234 getProductNameBytes() { 235 Object ref = productName_; 236 if (ref instanceof String) { 237 com.google.protobuf.ByteString b = 238 com.google.protobuf.ByteString.copyFromUtf8( 239 (String) ref); 240 productName_ = b; 241 return b; 242 } else { 243 return (com.google.protobuf.ByteString) ref; 244 } 245 } 246 247 public static final int ADDRESS_FIELD_NUMBER = 4; 248 private com.google.protobuf.LazyStringList address_; 249 /** 250 * <code>repeated string address = 4;</code> 251 */ 252 public com.google.protobuf.ProtocolStringList 253 getAddressList() { 254 return address_; 255 } 256 /** 257 * <code>repeated string address = 4;</code> 258 */ 259 public int getAddressCount() { 260 return address_.size(); 261 } 262 /** 263 * <code>repeated string address = 4;</code> 264 */ 265 public String getAddress(int index) { 266 return address_.get(index); 267 } 268 /** 269 * <code>repeated string address = 4;</code> 270 */ 271 public com.google.protobuf.ByteString 272 getAddressBytes(int index) { 273 return address_.getByteString(index); 274 } 275 276 private byte memoizedIsInitialized = -1; 277 @Override 278 public final boolean isInitialized() { 279 byte isInitialized = memoizedIsInitialized; 280 if (isInitialized == 1) return true; 281 if (isInitialized == 0) return false; 282 283 memoizedIsInitialized = 1; 284 return true; 285 } 286 287 @Override 288 public void writeTo(com.google.protobuf.CodedOutputStream output) 289 throws java.io.IOException { 290 if (subReqID_ != 0) { 291 output.writeInt32(1, subReqID_); 292 } 293 if (!getUserNameBytes().isEmpty()) { 294 com.google.protobuf.GeneratedMessageV3.writeString(output, 2, userName_); 295 } 296 if (!getProductNameBytes().isEmpty()) { 297 com.google.protobuf.GeneratedMessageV3.writeString(output, 3, productName_); 298 } 299 for (int i = 0; i < address_.size(); i++) { 300 com.google.protobuf.GeneratedMessageV3.writeString(output, 4, address_.getRaw(i)); 301 } 302 unknownFields.writeTo(output); 303 } 304 305 @Override 306 public int getSerializedSize() { 307 int size = memoizedSize; 308 if (size != -1) return size; 309 310 size = 0; 311 if (subReqID_ != 0) { 312 size += com.google.protobuf.CodedOutputStream 313 .computeInt32Size(1, subReqID_); 314 } 315 if (!getUserNameBytes().isEmpty()) { 316 size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, userName_); 317 } 318 if (!getProductNameBytes().isEmpty()) { 319 size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, productName_); 320 } 321 { 322 int dataSize = 0; 323 for (int i = 0; i < address_.size(); i++) { 324 dataSize += computeStringSizeNoTag(address_.getRaw(i)); 325 } 326 size += dataSize; 327 size += 1 * getAddressList().size(); 328 } 329 size += unknownFields.getSerializedSize(); 330 memoizedSize = size; 331 return size; 332 } 333 334 @Override 335 public boolean equals(final Object obj) { 336 if (obj == this) { 337 return true; 338 } 339 if (!(obj instanceof SubscribeReq)) { 340 return super.equals(obj); 341 } 342 SubscribeReq other = (SubscribeReq) obj; 343 344 boolean result = true; 345 result = result && (getSubReqID() 346 == other.getSubReqID()); 347 result = result && getUserName() 348 .equals(other.getUserName()); 349 result = result && getProductName() 350 .equals(other.getProductName()); 351 result = result && getAddressList() 352 .equals(other.getAddressList()); 353 result = result && unknownFields.equals(other.unknownFields); 354 return result; 355 } 356 357 @Override 358 public int hashCode() { 359 if (memoizedHashCode != 0) { 360 return memoizedHashCode; 361 } 362 int hash = 41; 363 hash = (19 * hash) + getDescriptor().hashCode(); 364 hash = (37 * hash) + SUBREQID_FIELD_NUMBER; 365 hash = (53 * hash) + getSubReqID(); 366 hash = (37 * hash) + USERNAME_FIELD_NUMBER; 367 hash = (53 * hash) + getUserName().hashCode(); 368 hash = (37 * hash) + PRODUCTNAME_FIELD_NUMBER; 369 hash = (53 * hash) + getProductName().hashCode(); 370 if (getAddressCount() > 0) { 371 hash = (37 * hash) + ADDRESS_FIELD_NUMBER; 372 hash = (53 * hash) + getAddressList().hashCode(); 373 } 374 hash = (29 * hash) + unknownFields.hashCode(); 375 memoizedHashCode = hash; 376 return hash; 377 } 378 379 public static SubscribeReq parseFrom( 380 java.nio.ByteBuffer data) 381 throws com.google.protobuf.InvalidProtocolBufferException { 382 return PARSER.parseFrom(data); 383 } 384 public static SubscribeReq parseFrom( 385 java.nio.ByteBuffer data, 386 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 387 throws com.google.protobuf.InvalidProtocolBufferException { 388 return PARSER.parseFrom(data, extensionRegistry); 389 } 390 public static SubscribeReq parseFrom( 391 com.google.protobuf.ByteString data) 392 throws com.google.protobuf.InvalidProtocolBufferException { 393 return PARSER.parseFrom(data); 394 } 395 public static SubscribeReq parseFrom( 396 com.google.protobuf.ByteString data, 397 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 398 throws com.google.protobuf.InvalidProtocolBufferException { 399 return PARSER.parseFrom(data, extensionRegistry); 400 } 401 public static SubscribeReq parseFrom(byte[] data) 402 throws com.google.protobuf.InvalidProtocolBufferException { 403 return PARSER.parseFrom(data); 404 } 405 public static SubscribeReq parseFrom( 406 byte[] data, 407 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 408 throws com.google.protobuf.InvalidProtocolBufferException { 409 return PARSER.parseFrom(data, extensionRegistry); 410 } 411 public static SubscribeReq parseFrom(java.io.InputStream input) 412 throws java.io.IOException { 413 return com.google.protobuf.GeneratedMessageV3 414 .parseWithIOException(PARSER, input); 415 } 416 public static SubscribeReq parseFrom( 417 java.io.InputStream input, 418 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 419 throws java.io.IOException { 420 return com.google.protobuf.GeneratedMessageV3 421 .parseWithIOException(PARSER, input, extensionRegistry); 422 } 423 public static SubscribeReq parseDelimitedFrom(java.io.InputStream input) 424 throws java.io.IOException { 425 return com.google.protobuf.GeneratedMessageV3 426 .parseDelimitedWithIOException(PARSER, input); 427 } 428 public static SubscribeReq parseDelimitedFrom( 429 java.io.InputStream input, 430 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 431 throws java.io.IOException { 432 return com.google.protobuf.GeneratedMessageV3 433 .parseDelimitedWithIOException(PARSER, input, extensionRegistry); 434 } 435 public static SubscribeReq parseFrom( 436 com.google.protobuf.CodedInputStream input) 437 throws java.io.IOException { 438 return com.google.protobuf.GeneratedMessageV3 439 .parseWithIOException(PARSER, input); 440 } 441 public static SubscribeReq parseFrom( 442 com.google.protobuf.CodedInputStream input, 443 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 444 throws java.io.IOException { 445 return com.google.protobuf.GeneratedMessageV3 446 .parseWithIOException(PARSER, input, extensionRegistry); 447 } 448 449 @Override 450 public Builder newBuilderForType() { return newBuilder(); } 451 public static Builder newBuilder() { 452 return DEFAULT_INSTANCE.toBuilder(); 453 } 454 public static Builder newBuilder(SubscribeReq prototype) { 455 return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 456 } 457 @Override 458 public Builder toBuilder() { 459 return this == DEFAULT_INSTANCE 460 ? new Builder() : new Builder().mergeFrom(this); 461 } 462 463 @Override 464 protected Builder newBuilderForType( 465 BuilderParent parent) { 466 Builder builder = new Builder(parent); 467 return builder; 468 } 469 /** 470 * Protobuf type {@code SubscribeReq} 471 */ 472 public static final class Builder extends 473 com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements 474 // @@protoc_insertion_point(builder_implements:SubscribeReq) 475 SubscribeReqOrBuilder { 476 public static final com.google.protobuf.Descriptors.Descriptor 477 getDescriptor() { 478 return SubscribeReqProto.internal_static_SubscribeReq_descriptor; 479 } 480 481 @Override 482 protected FieldAccessorTable 483 internalGetFieldAccessorTable() { 484 return SubscribeReqProto.internal_static_SubscribeReq_fieldAccessorTable 485 .ensureFieldAccessorsInitialized( 486 SubscribeReq.class, Builder.class); 487 } 488 489 // Construct using com.protobuf.SubscribeReqProto.SubscribeReq.newBuilder() 490 private Builder() { 491 maybeForceBuilderInitialization(); 492 } 493 494 private Builder( 495 BuilderParent parent) { 496 super(parent); 497 maybeForceBuilderInitialization(); 498 } 499 private void maybeForceBuilderInitialization() { 500 if (com.google.protobuf.GeneratedMessageV3 501 .alwaysUseFieldBuilders) { 502 } 503 } 504 @Override 505 public Builder clear() { 506 super.clear(); 507 subReqID_ = 0; 508 509 userName_ = ""; 510 511 productName_ = ""; 512 513 address_ = com.google.protobuf.LazyStringArrayList.EMPTY; 514 bitField0_ = (bitField0_ & ~0x00000008); 515 return this; 516 } 517 518 @Override 519 public com.google.protobuf.Descriptors.Descriptor 520 getDescriptorForType() { 521 return SubscribeReqProto.internal_static_SubscribeReq_descriptor; 522 } 523 524 @Override 525 public SubscribeReq getDefaultInstanceForType() { 526 return SubscribeReq.getDefaultInstance(); 527 } 528 529 @Override 530 public SubscribeReq build() { 531 SubscribeReq result = buildPartial(); 532 if (!result.isInitialized()) { 533 throw newUninitializedMessageException(result); 534 } 535 return result; 536 } 537 538 @Override 539 public SubscribeReq buildPartial() { 540 SubscribeReq result = new SubscribeReq(this); 541 int from_bitField0_ = bitField0_; 542 int to_bitField0_ = 0; 543 result.subReqID_ = subReqID_; 544 result.userName_ = userName_; 545 result.productName_ = productName_; 546 if (((bitField0_ & 0x00000008) == 0x00000008)) { 547 address_ = address_.getUnmodifiableView(); 548 bitField0_ = (bitField0_ & ~0x00000008); 549 } 550 result.address_ = address_; 551 result.bitField0_ = to_bitField0_; 552 onBuilt(); 553 return result; 554 } 555 556 @Override 557 public Builder clone() { 558 return (Builder) super.clone(); 559 } 560 @Override 561 public Builder setField( 562 com.google.protobuf.Descriptors.FieldDescriptor field, 563 Object value) { 564 return (Builder) super.setField(field, value); 565 } 566 @Override 567 public Builder clearField( 568 com.google.protobuf.Descriptors.FieldDescriptor field) { 569 return (Builder) super.clearField(field); 570 } 571 @Override 572 public Builder clearOneof( 573 com.google.protobuf.Descriptors.OneofDescriptor oneof) { 574 return (Builder) super.clearOneof(oneof); 575 } 576 @Override 577 public Builder setRepeatedField( 578 com.google.protobuf.Descriptors.FieldDescriptor field, 579 int index, Object value) { 580 return (Builder) super.setRepeatedField(field, index, value); 581 } 582 @Override 583 public Builder addRepeatedField( 584 com.google.protobuf.Descriptors.FieldDescriptor field, 585 Object value) { 586 return (Builder) super.addRepeatedField(field, value); 587 } 588 @Override 589 public Builder mergeFrom(com.google.protobuf.Message other) { 590 if (other instanceof SubscribeReq) { 591 return mergeFrom((SubscribeReq)other); 592 } else { 593 super.mergeFrom(other); 594 return this; 595 } 596 } 597 598 public Builder mergeFrom(SubscribeReq other) { 599 if (other == SubscribeReq.getDefaultInstance()) return this; 600 if (other.getSubReqID() != 0) { 601 setSubReqID(other.getSubReqID()); 602 } 603 if (!other.getUserName().isEmpty()) { 604 userName_ = other.userName_; 605 onChanged(); 606 } 607 if (!other.getProductName().isEmpty()) { 608 productName_ = other.productName_; 609 onChanged(); 610 } 611 if (!other.address_.isEmpty()) { 612 if (address_.isEmpty()) { 613 address_ = other.address_; 614 bitField0_ = (bitField0_ & ~0x00000008); 615 } else { 616 ensureAddressIsMutable(); 617 address_.addAll(other.address_); 618 } 619 onChanged(); 620 } 621 this.mergeUnknownFields(other.unknownFields); 622 onChanged(); 623 return this; 624 } 625 626 @Override 627 public final boolean isInitialized() { 628 return true; 629 } 630 631 @Override 632 public Builder mergeFrom( 633 com.google.protobuf.CodedInputStream input, 634 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 635 throws java.io.IOException { 636 SubscribeReq parsedMessage = null; 637 try { 638 parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); 639 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 640 parsedMessage = (SubscribeReq) e.getUnfinishedMessage(); 641 throw e.unwrapIOException(); 642 } finally { 643 if (parsedMessage != null) { 644 mergeFrom(parsedMessage); 645 } 646 } 647 return this; 648 } 649 private int bitField0_; 650 651 private int subReqID_ ; 652 /** 653 * <code>int32 subReqID = 1;</code> 654 */ 655 public int getSubReqID() { 656 return subReqID_; 657 } 658 /** 659 * <code>int32 subReqID = 1;</code> 660 */ 661 public Builder setSubReqID(int value) { 662 663 subReqID_ = value; 664 onChanged(); 665 return this; 666 } 667 /** 668 * <code>int32 subReqID = 1;</code> 669 */ 670 public Builder clearSubReqID() { 671 672 subReqID_ = 0; 673 onChanged(); 674 return this; 675 } 676 677 private Object userName_ = ""; 678 /** 679 * <code>string userName = 2;</code> 680 */ 681 public String getUserName() { 682 Object ref = userName_; 683 if (!(ref instanceof String)) { 684 com.google.protobuf.ByteString bs = 685 (com.google.protobuf.ByteString) ref; 686 String s = bs.toStringUtf8(); 687 userName_ = s; 688 return s; 689 } else { 690 return (String) ref; 691 } 692 } 693 /** 694 * <code>string userName = 2;</code> 695 */ 696 public com.google.protobuf.ByteString 697 getUserNameBytes() { 698 Object ref = userName_; 699 if (ref instanceof String) { 700 com.google.protobuf.ByteString b = 701 com.google.protobuf.ByteString.copyFromUtf8( 702 (String) ref); 703 userName_ = b; 704 return b; 705 } else { 706 return (com.google.protobuf.ByteString) ref; 707 } 708 } 709 /** 710 * <code>string userName = 2;</code> 711 */ 712 public Builder setUserName( 713 String value) { 714 if (value == null) { 715 throw new NullPointerException(); 716 } 717 718 userName_ = value; 719 onChanged(); 720 return this; 721 } 722 /** 723 * <code>string userName = 2;</code> 724 */ 725 public Builder clearUserName() { 726 727 userName_ = getDefaultInstance().getUserName(); 728 onChanged(); 729 return this; 730 } 731 /** 732 * <code>string userName = 2;</code> 733 */ 734 public Builder setUserNameBytes( 735 com.google.protobuf.ByteString value) { 736 if (value == null) { 737 throw new NullPointerException(); 738 } 739 checkByteStringIsUtf8(value); 740 741 userName_ = value; 742 onChanged(); 743 return this; 744 } 745 746 private Object productName_ = ""; 747 /** 748 * <code>string productName = 3;</code> 749 */ 750 public String getProductName() { 751 Object ref = productName_; 752 if (!(ref instanceof String)) { 753 com.google.protobuf.ByteString bs = 754 (com.google.protobuf.ByteString) ref; 755 String s = bs.toStringUtf8(); 756 productName_ = s; 757 return s; 758 } else { 759 return (String) ref; 760 } 761 } 762 /** 763 * <code>string productName = 3;</code> 764 */ 765 public com.google.protobuf.ByteString 766 getProductNameBytes() { 767 Object ref = productName_; 768 if (ref instanceof String) { 769 com.google.protobuf.ByteString b = 770 com.google.protobuf.ByteString.copyFromUtf8( 771 (String) ref); 772 productName_ = b; 773 return b; 774 } else { 775 return (com.google.protobuf.ByteString) ref; 776 } 777 } 778 /** 779 * <code>string productName = 3;</code> 780 */ 781 public Builder setProductName( 782 String value) { 783 if (value == null) { 784 throw new NullPointerException(); 785 } 786 787 productName_ = value; 788 onChanged(); 789 return this; 790 } 791 /** 792 * <code>string productName = 3;</code> 793 */ 794 public Builder clearProductName() { 795 796 productName_ = getDefaultInstance().getProductName(); 797 onChanged(); 798 return this; 799 } 800 /** 801 * <code>string productName = 3;</code> 802 */ 803 public Builder setProductNameBytes( 804 com.google.protobuf.ByteString value) { 805 if (value == null) { 806 throw new NullPointerException(); 807 } 808 checkByteStringIsUtf8(value); 809 810 productName_ = value; 811 onChanged(); 812 return this; 813 } 814 815 private com.google.protobuf.LazyStringList address_ = com.google.protobuf.LazyStringArrayList.EMPTY; 816 private void ensureAddressIsMutable() { 817 if (!((bitField0_ & 0x00000008) == 0x00000008)) { 818 address_ = new com.google.protobuf.LazyStringArrayList(address_); 819 bitField0_ |= 0x00000008; 820 } 821 } 822 /** 823 * <code>repeated string address = 4;</code> 824 */ 825 public com.google.protobuf.ProtocolStringList 826 getAddressList() { 827 return address_.getUnmodifiableView(); 828 } 829 /** 830 * <code>repeated string address = 4;</code> 831 */ 832 public int getAddressCount() { 833 return address_.size(); 834 } 835 /** 836 * <code>repeated string address = 4;</code> 837 */ 838 public String getAddress(int index) { 839 return address_.get(index); 840 } 841 /** 842 * <code>repeated string address = 4;</code> 843 */ 844 public com.google.protobuf.ByteString 845 getAddressBytes(int index) { 846 return address_.getByteString(index); 847 } 848 /** 849 * <code>repeated string address = 4;</code> 850 */ 851 public Builder setAddress( 852 int index, String value) { 853 if (value == null) { 854 throw new NullPointerException(); 855 } 856 ensureAddressIsMutable(); 857 address_.set(index, value); 858 onChanged(); 859 return this; 860 } 861 /** 862 * <code>repeated string address = 4;</code> 863 */ 864 public Builder addAddress( 865 String value) { 866 if (value == null) { 867 throw new NullPointerException(); 868 } 869 ensureAddressIsMutable(); 870 address_.add(value); 871 onChanged(); 872 return this; 873 } 874 /** 875 * <code>repeated string address = 4;</code> 876 */ 877 public Builder addAllAddress( 878 Iterable<String> values) { 879 ensureAddressIsMutable(); 880 com.google.protobuf.AbstractMessageLite.Builder.addAll( 881 values, address_); 882 onChanged(); 883 return this; 884 } 885 /** 886 * <code>repeated string address = 4;</code> 887 */ 888 public Builder clearAddress() { 889 address_ = com.google.protobuf.LazyStringArrayList.EMPTY; 890 bitField0_ = (bitField0_ & ~0x00000008); 891 onChanged(); 892 return this; 893 } 894 /** 895 * <code>repeated string address = 4;</code> 896 */ 897 public Builder addAddressBytes( 898 com.google.protobuf.ByteString value) { 899 if (value == null) { 900 throw new NullPointerException(); 901 } 902 checkByteStringIsUtf8(value); 903 ensureAddressIsMutable(); 904 address_.add(value); 905 onChanged(); 906 return this; 907 } 908 @Override 909 public final Builder setUnknownFields( 910 final com.google.protobuf.UnknownFieldSet unknownFields) { 911 return super.setUnknownFieldsProto3(unknownFields); 912 } 913 914 @Override 915 public final Builder mergeUnknownFields( 916 final com.google.protobuf.UnknownFieldSet unknownFields) { 917 return super.mergeUnknownFields(unknownFields); 918 } 919 920 921 // @@protoc_insertion_point(builder_scope:SubscribeReq) 922 } 923 924 // @@protoc_insertion_point(class_scope:SubscribeReq) 925 private static final SubscribeReq DEFAULT_INSTANCE; 926 static { 927 DEFAULT_INSTANCE = new SubscribeReq(); 928 } 929 930 public static SubscribeReq getDefaultInstance() { 931 return DEFAULT_INSTANCE; 932 } 933 934 private static final com.google.protobuf.Parser<SubscribeReq> 935 PARSER = new com.google.protobuf.AbstractParser<SubscribeReq>() { 936 @Override 937 public SubscribeReq parsePartialFrom( 938 com.google.protobuf.CodedInputStream input, 939 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 940 throws com.google.protobuf.InvalidProtocolBufferException { 941 return new SubscribeReq(input, extensionRegistry); 942 } 943 }; 944 945 public static com.google.protobuf.Parser<SubscribeReq> parser() { 946 return PARSER; 947 } 948 949 @Override 950 public com.google.protobuf.Parser<SubscribeReq> getParserForType() { 951 return PARSER; 952 } 953 954 @Override 955 public SubscribeReq getDefaultInstanceForType() { 956 return DEFAULT_INSTANCE; 957 } 958 959 } 960 961 private static final com.google.protobuf.Descriptors.Descriptor 962 internal_static_SubscribeReq_descriptor; 963 private static final 964 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 965 internal_static_SubscribeReq_fieldAccessorTable; 966 967 public static com.google.protobuf.Descriptors.FileDescriptor 968 getDescriptor() { 969 return descriptor; 970 } 971 private static com.google.protobuf.Descriptors.FileDescriptor 972 descriptor; 973 static { 974 String[] descriptorData = { 975 "\n\022SubscribeReq.proto\"X\n\014SubscribeReq\022\020\n\010" + 976 "subReqID\030\001 \001(\005\022\020\n\010userName\030\002 \001(\t\022\023\n\013prod" + 977 "uctName\030\003 \001(\t\022\017\n\007address\030\004 \003(\tB!\n\014com.pr" + 978 "otobufB\021SubscribeReqProtob\006proto3" 979 }; 980 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = 981 new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { 982 public com.google.protobuf.ExtensionRegistry assignDescriptors( 983 com.google.protobuf.Descriptors.FileDescriptor root) { 984 descriptor = root; 985 return null; 986 } 987 }; 988 com.google.protobuf.Descriptors.FileDescriptor 989 .internalBuildGeneratedFileFrom(descriptorData, 990 new com.google.protobuf.Descriptors.FileDescriptor[] { 991 }, assigner); 992 internal_static_SubscribeReq_descriptor = 993 getDescriptor().getMessageTypes().get(0); 994 internal_static_SubscribeReq_fieldAccessorTable = new 995 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 996 internal_static_SubscribeReq_descriptor, 997 new String[] { "SubReqID", "UserName", "ProductName", "Address", }); 998 } 999 1000 // @@protoc_insertion_point(outer_class_scope) 1001 }
Resp代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // Generated by the protocol buffer compiler. DO NOT EDIT! 2 // source: SubscribeResp.proto 3 4 package com.protobuf; 5 6 public final class SubscribeRespProto { 7 private SubscribeRespProto() {} 8 public static void registerAllExtensions( 9 com.google.protobuf.ExtensionRegistryLite registry) { 10 } 11 12 public static void registerAllExtensions( 13 com.google.protobuf.ExtensionRegistry registry) { 14 registerAllExtensions( 15 (com.google.protobuf.ExtensionRegistryLite) registry); 16 } 17 public interface SubscribeRespOrBuilder extends 18 // @@protoc_insertion_point(interface_extends:SubscribeResp) 19 com.google.protobuf.MessageOrBuilder { 20 21 /** 22 * <code>int32 subReqID = 1;</code> 23 */ 24 int getSubReqID(); 25 26 /** 27 * <code>int32 respcode = 2;</code> 28 */ 29 int getRespcode(); 30 31 /** 32 * <code>string desc = 3;</code> 33 */ 34 String getDesc(); 35 /** 36 * <code>string desc = 3;</code> 37 */ 38 com.google.protobuf.ByteString 39 getDescBytes(); 40 } 41 /** 42 * Protobuf type {@code SubscribeResp} 43 */ 44 public static final class SubscribeResp extends 45 com.google.protobuf.GeneratedMessageV3 implements 46 // @@protoc_insertion_point(message_implements:SubscribeResp) 47 SubscribeRespOrBuilder { 48 private static final long serialVersionUID = 0L; 49 // Use SubscribeResp.newBuilder() to construct. 50 private SubscribeResp(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) { 51 super(builder); 52 } 53 private SubscribeResp() { 54 subReqID_ = 0; 55 respcode_ = 0; 56 desc_ = ""; 57 } 58 59 @Override 60 public final com.google.protobuf.UnknownFieldSet 61 getUnknownFields() { 62 return this.unknownFields; 63 } 64 private SubscribeResp( 65 com.google.protobuf.CodedInputStream input, 66 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 67 throws com.google.protobuf.InvalidProtocolBufferException { 68 this(); 69 if (extensionRegistry == null) { 70 throw new NullPointerException(); 71 } 72 int mutable_bitField0_ = 0; 73 com.google.protobuf.UnknownFieldSet.Builder unknownFields = 74 com.google.protobuf.UnknownFieldSet.newBuilder(); 75 try { 76 boolean done = false; 77 while (!done) { 78 int tag = input.readTag(); 79 switch (tag) { 80 case 0: 81 done = true; 82 break; 83 case 8: { 84 85 subReqID_ = input.readInt32(); 86 break; 87 } 88 case 16: { 89 90 respcode_ = input.readInt32(); 91 break; 92 } 93 case 26: { 94 String s = input.readStringRequireUtf8(); 95 96 desc_ = s; 97 break; 98 } 99 default: { 100 if (!parseUnknownFieldProto3( 101 input, unknownFields, extensionRegistry, tag)) { 102 done = true; 103 } 104 break; 105 } 106 } 107 } 108 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 109 throw e.setUnfinishedMessage(this); 110 } catch (java.io.IOException e) { 111 throw new com.google.protobuf.InvalidProtocolBufferException( 112 e).setUnfinishedMessage(this); 113 } finally { 114 this.unknownFields = unknownFields.build(); 115 makeExtensionsImmutable(); 116 } 117 } 118 public static final com.google.protobuf.Descriptors.Descriptor 119 getDescriptor() { 120 return SubscribeRespProto.internal_static_SubscribeResp_descriptor; 121 } 122 123 @Override 124 protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 125 internalGetFieldAccessorTable() { 126 return SubscribeRespProto.internal_static_SubscribeResp_fieldAccessorTable 127 .ensureFieldAccessorsInitialized( 128 SubscribeResp.class, Builder.class); 129 } 130 131 public static final int SUBREQID_FIELD_NUMBER = 1; 132 private int subReqID_; 133 /** 134 * <code>int32 subReqID = 1;</code> 135 */ 136 public int getSubReqID() { 137 return subReqID_; 138 } 139 140 public static final int RESPCODE_FIELD_NUMBER = 2; 141 private int respcode_; 142 /** 143 * <code>int32 respcode = 2;</code> 144 */ 145 public int getRespcode() { 146 return respcode_; 147 } 148 149 public static final int DESC_FIELD_NUMBER = 3; 150 private volatile Object desc_; 151 /** 152 * <code>string desc = 3;</code> 153 */ 154 public String getDesc() { 155 Object ref = desc_; 156 if (ref instanceof String) { 157 return (String) ref; 158 } else { 159 com.google.protobuf.ByteString bs = 160 (com.google.protobuf.ByteString) ref; 161 String s = bs.toStringUtf8(); 162 desc_ = s; 163 return s; 164 } 165 } 166 /** 167 * <code>string desc = 3;</code> 168 */ 169 public com.google.protobuf.ByteString 170 getDescBytes() { 171 Object ref = desc_; 172 if (ref instanceof String) { 173 com.google.protobuf.ByteString b = 174 com.google.protobuf.ByteString.copyFromUtf8( 175 (String) ref); 176 desc_ = b; 177 return b; 178 } else { 179 return (com.google.protobuf.ByteString) ref; 180 } 181 } 182 183 private byte memoizedIsInitialized = -1; 184 @Override 185 public final boolean isInitialized() { 186 byte isInitialized = memoizedIsInitialized; 187 if (isInitialized == 1) return true; 188 if (isInitialized == 0) return false; 189 190 memoizedIsInitialized = 1; 191 return true; 192 } 193 194 @Override 195 public void writeTo(com.google.protobuf.CodedOutputStream output) 196 throws java.io.IOException { 197 if (subReqID_ != 0) { 198 output.writeInt32(1, subReqID_); 199 } 200 if (respcode_ != 0) { 201 output.writeInt32(2, respcode_); 202 } 203 if (!getDescBytes().isEmpty()) { 204 com.google.protobuf.GeneratedMessageV3.writeString(output, 3, desc_); 205 } 206 unknownFields.writeTo(output); 207 } 208 209 @Override 210 public int getSerializedSize() { 211 int size = memoizedSize; 212 if (size != -1) return size; 213 214 size = 0; 215 if (subReqID_ != 0) { 216 size += com.google.protobuf.CodedOutputStream 217 .computeInt32Size(1, subReqID_); 218 } 219 if (respcode_ != 0) { 220 size += com.google.protobuf.CodedOutputStream 221 .computeInt32Size(2, respcode_); 222 } 223 if (!getDescBytes().isEmpty()) { 224 size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, desc_); 225 } 226 size += unknownFields.getSerializedSize(); 227 memoizedSize = size; 228 return size; 229 } 230 231 @Override 232 public boolean equals(final Object obj) { 233 if (obj == this) { 234 return true; 235 } 236 if (!(obj instanceof SubscribeResp)) { 237 return super.equals(obj); 238 } 239 SubscribeResp other = (SubscribeResp) obj; 240 241 boolean result = true; 242 result = result && (getSubReqID() 243 == other.getSubReqID()); 244 result = result && (getRespcode() 245 == other.getRespcode()); 246 result = result && getDesc() 247 .equals(other.getDesc()); 248 result = result && unknownFields.equals(other.unknownFields); 249 return result; 250 } 251 252 @Override 253 public int hashCode() { 254 if (memoizedHashCode != 0) { 255 return memoizedHashCode; 256 } 257 int hash = 41; 258 hash = (19 * hash) + getDescriptor().hashCode(); 259 hash = (37 * hash) + SUBREQID_FIELD_NUMBER; 260 hash = (53 * hash) + getSubReqID(); 261 hash = (37 * hash) + RESPCODE_FIELD_NUMBER; 262 hash = (53 * hash) + getRespcode(); 263 hash = (37 * hash) + DESC_FIELD_NUMBER; 264 hash = (53 * hash) + getDesc().hashCode(); 265 hash = (29 * hash) + unknownFields.hashCode(); 266 memoizedHashCode = hash; 267 return hash; 268 } 269 270 public static SubscribeResp parseFrom( 271 java.nio.ByteBuffer data) 272 throws com.google.protobuf.InvalidProtocolBufferException { 273 return PARSER.parseFrom(data); 274 } 275 public static SubscribeResp parseFrom( 276 java.nio.ByteBuffer data, 277 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 278 throws com.google.protobuf.InvalidProtocolBufferException { 279 return PARSER.parseFrom(data, extensionRegistry); 280 } 281 public static SubscribeResp parseFrom( 282 com.google.protobuf.ByteString data) 283 throws com.google.protobuf.InvalidProtocolBufferException { 284 return PARSER.parseFrom(data); 285 } 286 public static SubscribeResp parseFrom( 287 com.google.protobuf.ByteString data, 288 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 289 throws com.google.protobuf.InvalidProtocolBufferException { 290 return PARSER.parseFrom(data, extensionRegistry); 291 } 292 public static SubscribeResp parseFrom(byte[] data) 293 throws com.google.protobuf.InvalidProtocolBufferException { 294 return PARSER.parseFrom(data); 295 } 296 public static SubscribeResp parseFrom( 297 byte[] data, 298 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 299 throws com.google.protobuf.InvalidProtocolBufferException { 300 return PARSER.parseFrom(data, extensionRegistry); 301 } 302 public static SubscribeResp parseFrom(java.io.InputStream input) 303 throws java.io.IOException { 304 return com.google.protobuf.GeneratedMessageV3 305 .parseWithIOException(PARSER, input); 306 } 307 public static SubscribeResp parseFrom( 308 java.io.InputStream input, 309 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 310 throws java.io.IOException { 311 return com.google.protobuf.GeneratedMessageV3 312 .parseWithIOException(PARSER, input, extensionRegistry); 313 } 314 public static SubscribeResp parseDelimitedFrom(java.io.InputStream input) 315 throws java.io.IOException { 316 return com.google.protobuf.GeneratedMessageV3 317 .parseDelimitedWithIOException(PARSER, input); 318 } 319 public static SubscribeResp parseDelimitedFrom( 320 java.io.InputStream input, 321 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 322 throws java.io.IOException { 323 return com.google.protobuf.GeneratedMessageV3 324 .parseDelimitedWithIOException(PARSER, input, extensionRegistry); 325 } 326 public static SubscribeResp parseFrom( 327 com.google.protobuf.CodedInputStream input) 328 throws java.io.IOException { 329 return com.google.protobuf.GeneratedMessageV3 330 .parseWithIOException(PARSER, input); 331 } 332 public static SubscribeResp parseFrom( 333 com.google.protobuf.CodedInputStream input, 334 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 335 throws java.io.IOException { 336 return com.google.protobuf.GeneratedMessageV3 337 .parseWithIOException(PARSER, input, extensionRegistry); 338 } 339 340 @Override 341 public Builder newBuilderForType() { return newBuilder(); } 342 public static Builder newBuilder() { 343 return DEFAULT_INSTANCE.toBuilder(); 344 } 345 public static Builder newBuilder(SubscribeResp prototype) { 346 return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); 347 } 348 @Override 349 public Builder toBuilder() { 350 return this == DEFAULT_INSTANCE 351 ? new Builder() : new Builder().mergeFrom(this); 352 } 353 354 @Override 355 protected Builder newBuilderForType( 356 com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 357 Builder builder = new Builder(parent); 358 return builder; 359 } 360 /** 361 * Protobuf type {@code SubscribeResp} 362 */ 363 public static final class Builder extends 364 com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements 365 // @@protoc_insertion_point(builder_implements:SubscribeResp) 366 SubscribeRespOrBuilder { 367 public static final com.google.protobuf.Descriptors.Descriptor 368 getDescriptor() { 369 return SubscribeRespProto.internal_static_SubscribeResp_descriptor; 370 } 371 372 @Override 373 protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 374 internalGetFieldAccessorTable() { 375 return SubscribeRespProto.internal_static_SubscribeResp_fieldAccessorTable 376 .ensureFieldAccessorsInitialized( 377 SubscribeResp.class, Builder.class); 378 } 379 380 // Construct using com.protobuf.SubscribeRespProto.SubscribeResp.newBuilder() 381 private Builder() { 382 maybeForceBuilderInitialization(); 383 } 384 385 private Builder( 386 com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { 387 super(parent); 388 maybeForceBuilderInitialization(); 389 } 390 private void maybeForceBuilderInitialization() { 391 if (com.google.protobuf.GeneratedMessageV3 392 .alwaysUseFieldBuilders) { 393 } 394 } 395 @Override 396 public Builder clear() { 397 super.clear(); 398 subReqID_ = 0; 399 400 respcode_ = 0; 401 402 desc_ = ""; 403 404 return this; 405 } 406 407 @Override 408 public com.google.protobuf.Descriptors.Descriptor 409 getDescriptorForType() { 410 return SubscribeRespProto.internal_static_SubscribeResp_descriptor; 411 } 412 413 @Override 414 public SubscribeResp getDefaultInstanceForType() { 415 return SubscribeResp.getDefaultInstance(); 416 } 417 418 @Override 419 public SubscribeResp build() { 420 SubscribeResp result = buildPartial(); 421 if (!result.isInitialized()) { 422 throw newUninitializedMessageException(result); 423 } 424 return result; 425 } 426 427 @Override 428 public SubscribeResp buildPartial() { 429 SubscribeResp result = new SubscribeResp(this); 430 result.subReqID_ = subReqID_; 431 result.respcode_ = respcode_; 432 result.desc_ = desc_; 433 onBuilt(); 434 return result; 435 } 436 437 @Override 438 public Builder clone() { 439 return (Builder) super.clone(); 440 } 441 @Override 442 public Builder setField( 443 com.google.protobuf.Descriptors.FieldDescriptor field, 444 Object value) { 445 return (Builder) super.setField(field, value); 446 } 447 @Override 448 public Builder clearField( 449 com.google.protobuf.Descriptors.FieldDescriptor field) { 450 return (Builder) super.clearField(field); 451 } 452 @Override 453 public Builder clearOneof( 454 com.google.protobuf.Descriptors.OneofDescriptor oneof) { 455 return (Builder) super.clearOneof(oneof); 456 } 457 @Override 458 public Builder setRepeatedField( 459 com.google.protobuf.Descriptors.FieldDescriptor field, 460 int index, Object value) { 461 return (Builder) super.setRepeatedField(field, index, value); 462 } 463 @Override 464 public Builder addRepeatedField( 465 com.google.protobuf.Descriptors.FieldDescriptor field, 466 Object value) { 467 return (Builder) super.addRepeatedField(field, value); 468 } 469 @Override 470 public Builder mergeFrom(com.google.protobuf.Message other) { 471 if (other instanceof SubscribeResp) { 472 return mergeFrom((SubscribeResp)other); 473 } else { 474 super.mergeFrom(other); 475 return this; 476 } 477 } 478 479 public Builder mergeFrom(SubscribeResp other) { 480 if (other == SubscribeResp.getDefaultInstance()) return this; 481 if (other.getSubReqID() != 0) { 482 setSubReqID(other.getSubReqID()); 483 } 484 if (other.getRespcode() != 0) { 485 setRespcode(other.getRespcode()); 486 } 487 if (!other.getDesc().isEmpty()) { 488 desc_ = other.desc_; 489 onChanged(); 490 } 491 this.mergeUnknownFields(other.unknownFields); 492 onChanged(); 493 return this; 494 } 495 496 @Override 497 public final boolean isInitialized() { 498 return true; 499 } 500 501 @Override 502 public Builder mergeFrom( 503 com.google.protobuf.CodedInputStream input, 504 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 505 throws java.io.IOException { 506 SubscribeResp parsedMessage = null; 507 try { 508 parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); 509 } catch (com.google.protobuf.InvalidProtocolBufferException e) { 510 parsedMessage = (SubscribeResp) e.getUnfinishedMessage(); 511 throw e.unwrapIOException(); 512 } finally { 513 if (parsedMessage != null) { 514 mergeFrom(parsedMessage); 515 } 516 } 517 return this; 518 } 519 520 private int subReqID_ ; 521 /** 522 * <code>int32 subReqID = 1;</code> 523 */ 524 public int getSubReqID() { 525 return subReqID_; 526 } 527 /** 528 * <code>int32 subReqID = 1;</code> 529 */ 530 public Builder setSubReqID(int value) { 531 532 subReqID_ = value; 533 onChanged(); 534 return this; 535 } 536 /** 537 * <code>int32 subReqID = 1;</code> 538 */ 539 public Builder clearSubReqID() { 540 541 subReqID_ = 0; 542 onChanged(); 543 return this; 544 } 545 546 private int respcode_ ; 547 /** 548 * <code>int32 respcode = 2;</code> 549 */ 550 public int getRespcode() { 551 return respcode_; 552 } 553 /** 554 * <code>int32 respcode = 2;</code> 555 */ 556 public Builder setRespcode(int value) { 557 558 respcode_ = value; 559 onChanged(); 560 return this; 561 } 562 /** 563 * <code>int32 respcode = 2;</code> 564 */ 565 public Builder clearRespcode() { 566 567 respcode_ = 0; 568 onChanged(); 569 return this; 570 } 571 572 private Object desc_ = ""; 573 /** 574 * <code>string desc = 3;</code> 575 */ 576 public String getDesc() { 577 Object ref = desc_; 578 if (!(ref instanceof String)) { 579 com.google.protobuf.ByteString bs = 580 (com.google.protobuf.ByteString) ref; 581 String s = bs.toStringUtf8(); 582 desc_ = s; 583 return s; 584 } else { 585 return (String) ref; 586 } 587 } 588 /** 589 * <code>string desc = 3;</code> 590 */ 591 public com.google.protobuf.ByteString 592 getDescBytes() { 593 Object ref = desc_; 594 if (ref instanceof String) { 595 com.google.protobuf.ByteString b = 596 com.google.protobuf.ByteString.copyFromUtf8( 597 (String) ref); 598 desc_ = b; 599 return b; 600 } else { 601 return (com.google.protobuf.ByteString) ref; 602 } 603 } 604 /** 605 * <code>string desc = 3;</code> 606 */ 607 public Builder setDesc( 608 String value) { 609 if (value == null) { 610 throw new NullPointerException(); 611 } 612 613 desc_ = value; 614 onChanged(); 615 return this; 616 } 617 /** 618 * <code>string desc = 3;</code> 619 */ 620 public Builder clearDesc() { 621 622 desc_ = getDefaultInstance().getDesc(); 623 onChanged(); 624 return this; 625 } 626 /** 627 * <code>string desc = 3;</code> 628 */ 629 public Builder setDescBytes( 630 com.google.protobuf.ByteString value) { 631 if (value == null) { 632 throw new NullPointerException(); 633 } 634 checkByteStringIsUtf8(value); 635 636 desc_ = value; 637 onChanged(); 638 return this; 639 } 640 @Override 641 public final Builder setUnknownFields( 642 final com.google.protobuf.UnknownFieldSet unknownFields) { 643 return super.setUnknownFieldsProto3(unknownFields); 644 } 645 646 @Override 647 public final Builder mergeUnknownFields( 648 final com.google.protobuf.UnknownFieldSet unknownFields) { 649 return super.mergeUnknownFields(unknownFields); 650 } 651 652 653 // @@protoc_insertion_point(builder_scope:SubscribeResp) 654 } 655 656 // @@protoc_insertion_point(class_scope:SubscribeResp) 657 private static final SubscribeResp DEFAULT_INSTANCE; 658 static { 659 DEFAULT_INSTANCE = new SubscribeResp(); 660 } 661 662 public static SubscribeResp getDefaultInstance() { 663 return DEFAULT_INSTANCE; 664 } 665 666 private static final com.google.protobuf.Parser<SubscribeResp> 667 PARSER = new com.google.protobuf.AbstractParser<SubscribeResp>() { 668 @Override 669 public SubscribeResp parsePartialFrom( 670 com.google.protobuf.CodedInputStream input, 671 com.google.protobuf.ExtensionRegistryLite extensionRegistry) 672 throws com.google.protobuf.InvalidProtocolBufferException { 673 return new SubscribeResp(input, extensionRegistry); 674 } 675 }; 676 677 public static com.google.protobuf.Parser<SubscribeResp> parser() { 678 return PARSER; 679 } 680 681 @Override 682 public com.google.protobuf.Parser<SubscribeResp> getParserForType() { 683 return PARSER; 684 } 685 686 @Override 687 public SubscribeResp getDefaultInstanceForType() { 688 return DEFAULT_INSTANCE; 689 } 690 691 } 692 693 private static final com.google.protobuf.Descriptors.Descriptor 694 internal_static_SubscribeResp_descriptor; 695 private static final 696 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable 697 internal_static_SubscribeResp_fieldAccessorTable; 698 699 public static com.google.protobuf.Descriptors.FileDescriptor 700 getDescriptor() { 701 return descriptor; 702 } 703 private static com.google.protobuf.Descriptors.FileDescriptor 704 descriptor; 705 static { 706 String[] descriptorData = { 707 "\n\023SubscribeResp.proto\"A\n\rSubscribeResp\022\020" + 708 "\n\010subReqID\030\001 \001(\005\022\020\n\010respcode\030\002 \001(\005\022\014\n\004de" + 709 "sc\030\003 \001(\tB\"\n\014com.protobufB\022SubscribeRespP" + 710 "rotob\006proto3" 711 }; 712 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = 713 new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { 714 public com.google.protobuf.ExtensionRegistry assignDescriptors( 715 com.google.protobuf.Descriptors.FileDescriptor root) { 716 descriptor = root; 717 return null; 718 } 719 }; 720 com.google.protobuf.Descriptors.FileDescriptor 721 .internalBuildGeneratedFileFrom(descriptorData, 722 new com.google.protobuf.Descriptors.FileDescriptor[] { 723 }, assigner); 724 internal_static_SubscribeResp_descriptor = 725 getDescriptor().getMessageTypes().get(0); 726 internal_static_SubscribeResp_fieldAccessorTable = new 727 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 728 internal_static_SubscribeResp_descriptor, 729 new String[] { "SubReqID", "Respcode", "Desc", }); 730 } 731 732 // @@protoc_insertion_point(outer_class_scope) 733 }
测试代码
1 package com.protobuf; 2 3 import com.google.protobuf.InvalidProtocolBufferException; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 public class TestSubScribeReqProto { 9 private static byte[] encode(SubscribeReqProto.SubscribeReq req){ 10 return req.toByteArray(); 11 } 12 private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException { 13 return SubscribeReqProto.SubscribeReq.parseFrom(body); 14 } 15 private static SubscribeReqProto.SubscribeReq createSubscribeReq(){ 16 SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder(); 17 builder.setSubReqID(1); 18 builder.setUserName("醉逍遥"); 19 builder.setProductName("Netty buffer"); 20 List<String> address = new ArrayList<>(); 21 address.add("beijing tiananmen"); 22 address.add("shanghai huangpujiang"); 23 address.add("tianjin binhaixinqu"); 24 address.add("shenzhen houhai"); 25 builder.addAllAddress(address); 26 return builder.build(); 27 } 28 29 public static void main(String[] args) throws InvalidProtocolBufferException { 30 SubscribeReqProto.SubscribeReq req = createSubscribeReq(); 31 System.out.println("解码之前:"+req.toString()); 32 byte[] b = encode(req); 33 System.out.println("编码后:"+b.toString()); 34 SubscribeReqProto.SubscribeReq req1 = decode(b); 35 System.out.println("解码后:"+req1); 36 System.out.println("解码前后是否相等:"+req.equals(req1)); 37 } 38 }
运行结果
这里运行后已经完全没问题了,只是在userName上的汉字没有显示为汉字,在这里暂时不理会他。
三、采用netty进行实例演示
服务端代码
1 package com.protobuf; 2 3 import io.netty.bootstrap.ServerBootstrap; 4 import io.netty.channel.ChannelFuture; 5 import io.netty.channel.ChannelInitializer; 6 import io.netty.channel.ChannelOption; 7 import io.netty.channel.EventLoopGroup; 8 import io.netty.channel.nio.NioEventLoopGroup; 9 import io.netty.channel.socket.SocketChannel; 10 import io.netty.channel.socket.nio.NioServerSocketChannel; 11 import io.netty.handler.codec.protobuf.ProtobufDecoder; 12 import io.netty.handler.codec.protobuf.ProtobufEncoder; 13 import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 14 import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; 15 import io.netty.handler.logging.LogLevel; 16 import io.netty.handler.logging.LoggingHandler; 17 18 public class SubReqServer { 19 public void bind(int port) throws InterruptedException { 20 EventLoopGroup bossGroup = new NioEventLoopGroup(); 21 EventLoopGroup workGroup = new NioEventLoopGroup(); 22 try { 23 ServerBootstrap b = new ServerBootstrap(); 24 b.group(bossGroup,workGroup).channel(NioServerSocketChannel.class) 25 .option(ChannelOption.SO_BACKLOG,100) 26 .handler(new LoggingHandler(LogLevel.INFO)) 27 .childHandler(new ChannelInitializer<SocketChannel>() { 28 @Override 29 protected void initChannel(SocketChannel socketChannel) throws Exception { 30 socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder()) 31 .addLast(new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance())) 32 .addLast(new ProtobufVarint32LengthFieldPrepender()) 33 .addLast(new ProtobufEncoder()) 34 .addLast(new SubReqServerHandler()); 35 } 36 }); 37 38 ChannelFuture f = b.bind(port).sync(); 39 f.channel().closeFuture().sync(); 40 } finally { 41 bossGroup.shutdownGracefully(); 42 workGroup.shutdownGracefully(); 43 } 44 } 45 46 public static void main(String[] args) throws InterruptedException { 47 int port =8080; 48 if (args!=null&&args.length>0){ 49 try{ 50 port = Integer.valueOf(args[0]); 51 }catch (NumberFormatException e){ 52 53 } 54 } 55 new SubReqServer().bind(port); 56 } 57 }
服务端Handler代码
1 package com.protobuf; 2 3 import io.netty.channel.ChannelHandlerContext; 4 import io.netty.channel.ChannelInboundHandlerAdapter; 5 6 public class SubReqServerHandler extends ChannelInboundHandlerAdapter { 7 @Override 8 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 9 SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq) msg; 10 if ("zuixiaoyao".equals(req.getUserName())){ 11 System.out.println("服务端接收到客户端SubcribeReq的请求:【"+req.toString()+"】"); 12 ctx.writeAndFlush(resp(req.getSubReqID())); 13 }else { 14 System.out.println("用户名不相同!"); 15 } 16 17 } 18 19 private SubscribeRespProto.SubscribeResp resp(int subReqID) { 20 SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder(); 21 builder.setRespcode(0); 22 builder.setSubReqID(subReqID); 23 builder.setDesc("order success!"); 24 return builder.build(); 25 } 26 27 @Override 28 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { 29 ctx.flush(); 30 } 31 32 @Override 33 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 34 ctx.close(); 35 } 36 }
客户端代码
1 package com.protobuf; 2 3 import io.netty.bootstrap.Bootstrap; 4 import io.netty.channel.ChannelFuture; 5 import io.netty.channel.ChannelInitializer; 6 import io.netty.channel.ChannelOption; 7 import io.netty.channel.EventLoopGroup; 8 import io.netty.channel.nio.NioEventLoopGroup; 9 import io.netty.channel.socket.SocketChannel; 10 import io.netty.channel.socket.nio.NioSocketChannel; 11 import io.netty.handler.codec.protobuf.ProtobufDecoder; 12 import io.netty.handler.codec.protobuf.ProtobufEncoder; 13 import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder; 14 import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender; 15 16 public class SubReqClient { 17 public void connect(int port, String host) throws InterruptedException { 18 EventLoopGroup group = new NioEventLoopGroup(); 19 try { 20 Bootstrap b = new Bootstrap(); 21 b.group(group).channel(NioSocketChannel.class) 22 .option(ChannelOption.TCP_NODELAY,true) 23 .handler(new ChannelInitializer<SocketChannel>() { 24 @Override 25 public void initChannel(SocketChannel socketChannel) { 26 socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder()) 27 .addLast(new ProtobufDecoder(SubscribeRespProto.SubscribeResp.getDefaultInstance())) 28 .addLast(new ProtobufVarint32LengthFieldPrepender()) 29 .addLast(new ProtobufEncoder()) 30 .addLast(new SubReqClientHandler()); 31 } 32 }); 33 ChannelFuture f = b.connect(host,port).sync(); 34 f.channel().closeFuture().sync(); 35 }finally { 36 group.shutdownGracefully(); 37 } 38 39 } 40 41 public static void main(String[] args) throws InterruptedException { 42 int port = 8080; 43 try{ 44 if(args.length>0 && args!=null){ 45 port = Integer.valueOf(args[0]); 46 } 47 }catch (NumberFormatException e){ 48 49 } 50 new SubReqClient().connect(port,"127.0.0.1"); 51 } 52 }
客户端Handler代码
1 package com.protobuf; 2 3 import io.netty.channel.ChannelHandlerContext; 4 import io.netty.channel.ChannelInboundHandlerAdapter; 5 6 import java.util.ArrayList; 7 import java.util.List; 8 9 public class SubReqClientHandler extends ChannelInboundHandlerAdapter { 10 @Override 11 public void channelActive(ChannelHandlerContext ctx) throws Exception { 12 for(int i=0;i<10;i++){ 13 ctx.write(subReq(i)); 14 } 15 ctx.flush(); 16 } 17 18 private SubscribeReqProto.SubscribeReq subReq(int i) { 19 SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder(); 20 builder.setUserName("zuixiaoyao"); 21 builder.setProductName("xiguahong"); 22 builder.setSubReqID(i); 23 List<String> address = new ArrayList<>(); 24 address.add("tianjin hongqiao"); 25 address.add("henan luoyang"); 26 address.add("shenzhen luohu"); 27 address.add("hebei qinhuangdao"); 28 builder.addAllAddress(address); 29 return builder.build(); 30 } 31 32 @Override 33 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 34 System.out.println("接收服务端相应:【"+msg+"】"); 35 } 36 37 @Override 38 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { 39 ctx.flush(); 40 } 41 42 @Override 43 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 44 ctx.close(); 45 } 46 }
运行结果:
服务端
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 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 2 21:00:33.990 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework 3 21:00:34.000 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8 4 21:00:34.122 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false 5 21:00:34.122 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512 6 21:00:34.184 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows 7 21:00:34.186 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false 8 21:00:34.187 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8 9 21:00:34.189 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available 10 21:00:34.191 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available 11 21:00:34.192 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available 12 21:00:34.194 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available 13 21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true 14 21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9 15 21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available 16 21:00:34.195 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available 17 21:00:34.196 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\litan\AppData\Local\Temp (java.io.tmpdir) 18 21:00:34.197 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model) 19 21:00:34.200 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false 20 21:00:34.200 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1890582528 bytes 21 21:00:34.200 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1 22 21:00:34.201 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available 23 21:00:34.227 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available 24 21:00:35.015 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 19192 (auto-detected) 25 21:00:35.020 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false 26 21:00:35.020 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false 27 21:00:35.530 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1) 28 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 29 21:00:36.148 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:c0:00:01 (auto-detected) 30 21:00:36.167 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024 31 21:00:36.167 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096 32 21:00:36.187 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple 33 21:00:36.187 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4 34 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8 35 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8 36 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192 37 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11 38 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216 39 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512 40 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256 41 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64 42 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768 43 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192 44 21:00:36.258 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true 45 21:00:36.277 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled 46 21:00:36.277 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0 47 21:00:36.277 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384 48 21:00:36.327 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x9032f6b1] REGISTERED 49 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 50 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 51 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] 52 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 53 21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096 54 21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2 55 21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16 56 21:00:40.717 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8 57 21:00:40.742 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true 58 21:00:40.744 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@7d129f4c 59 服务端接收到客户端SubcribeReq的请求:【userName: "zuixiaoyao" 60 productName: "xiguahong" 61 address: "tianjin hongqiao" 62 address: "henan luoyang" 63 address: "shenzhen luohu" 64 address: "hebei qinhuangdao" 65 】 66 服务端接收到客户端SubcribeReq的请求:【subReqID: 1 67 userName: "zuixiaoyao" 68 productName: "xiguahong" 69 address: "tianjin hongqiao" 70 address: "henan luoyang" 71 address: "shenzhen luohu" 72 address: "hebei qinhuangdao" 73 】 74 服务端接收到客户端SubcribeReq的请求:【subReqID: 2 75 userName: "zuixiaoyao" 76 productName: "xiguahong" 77 address: "tianjin hongqiao" 78 address: "henan luoyang" 79 address: "shenzhen luohu" 80 address: "hebei qinhuangdao" 81 】 82 服务端接收到客户端SubcribeReq的请求:【subReqID: 3 83 userName: "zuixiaoyao" 84 productName: "xiguahong" 85 address: "tianjin hongqiao" 86 address: "henan luoyang" 87 address: "shenzhen luohu" 88 address: "hebei qinhuangdao" 89 】 90 服务端接收到客户端SubcribeReq的请求:【subReqID: 4 91 userName: "zuixiaoyao" 92 productName: "xiguahong" 93 address: "tianjin hongqiao" 94 address: "henan luoyang" 95 address: "shenzhen luohu" 96 address: "hebei qinhuangdao" 97 】 98 服务端接收到客户端SubcribeReq的请求:【subReqID: 5 99 userName: "zuixiaoyao" 100 productName: "xiguahong" 101 address: "tianjin hongqiao" 102 address: "henan luoyang" 103 address: "shenzhen luohu" 104 address: "hebei qinhuangdao" 105 】 106 服务端接收到客户端SubcribeReq的请求:【subReqID: 6 107 userName: "zuixiaoyao" 108 productName: "xiguahong" 109 address: "tianjin hongqiao" 110 address: "henan luoyang" 111 address: "shenzhen luohu" 112 address: "hebei qinhuangdao" 113 】 114 服务端接收到客户端SubcribeReq的请求:【subReqID: 7 115 userName: "zuixiaoyao" 116 productName: "xiguahong" 117 address: "tianjin hongqiao" 118 address: "henan luoyang" 119 address: "shenzhen luohu" 120 address: "hebei qinhuangdao" 121 】 122 服务端接收到客户端SubcribeReq的请求:【subReqID: 8 123 userName: "zuixiaoyao" 124 productName: "xiguahong" 125 address: "tianjin hongqiao" 126 address: "henan luoyang" 127 address: "shenzhen luohu" 128 address: "hebei qinhuangdao" 129 】 130 服务端接收到客户端SubcribeReq的请求:【subReqID: 9 131 userName: "zuixiaoyao" 132 productName: "xiguahong" 133 address: "tianjin hongqiao" 134 address: "henan luoyang" 135 address: "shenzhen luohu" 136 address: "hebei qinhuangdao" 137 】
客户端
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 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 2 21:00:38.579 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework 3 21:00:38.595 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8 4 21:00:38.674 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false 5 21:00:38.674 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512 6 21:00:38.723 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows 7 21:00:38.740 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false 8 21:00:38.740 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8 9 21:00:38.744 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available 10 21:00:38.746 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available 11 21:00:38.748 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available 12 21:00:38.749 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available 13 21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true 14 21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9 15 21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available 16 21:00:38.751 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available 17 21:00:38.752 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\litan\AppData\Local\Temp (java.io.tmpdir) 18 21:00:38.753 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model) 19 21:00:38.755 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false 20 21:00:38.755 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1890582528 bytes 21 21:00:38.756 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1 22 21:00:38.757 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available 23 21:00:38.784 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available 24 21:00:39.434 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 10388 (auto-detected) 25 21:00:39.437 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false 26 21:00:39.437 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false 27 21:00:39.752 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1) 28 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 29 21:00:40.352 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:c0:00:01 (auto-detected) 30 21:00:40.363 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024 31 21:00:40.363 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096 32 21:00:40.388 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple 33 21:00:40.388 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4 34 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8 35 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8 36 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192 37 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11 38 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216 39 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512 40 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256 41 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64 42 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768 43 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192 44 21:00:40.451 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true 45 21:00:40.468 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled 46 21:00:40.469 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0 47 21:00:40.469 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384 48 21:00:40.682 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true 49 21:00:40.684 [nioEventLoopGroup-2-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@a74f5ae 50 21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096 51 21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2 52 21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16 53 21:00:40.691 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8 54 接收服务端相应:【desc: "order success!" 55 】 56 接收服务端相应:【subReqID: 1 57 desc: "order success!" 58 】 59 接收服务端相应:【subReqID: 2 60 desc: "order success!" 61 】 62 接收服务端相应:【subReqID: 3 63 desc: "order success!" 64 】 65 接收服务端相应:【subReqID: 4 66 desc: "order success!" 67 】 68 接收服务端相应:【subReqID: 5 69 desc: "order success!" 70 】 71 接收服务端相应:【subReqID: 6 72 desc: "order success!" 73 】 74 接收服务端相应:【subReqID: 7 75 desc: "order success!" 76 】 77 接收服务端相应:【subReqID: 8 78 desc: "order success!" 79 】 80 接收服务端相应:【subReqID: 9 81 desc: "order success!" 82 】
至此,全部完成,折叠的代码主要为运行的不是很需要重点阅读的代码,折叠起来以减小篇幅,在书写代码的过程中,客户端和服务端编解码等框架在pipeline.add时候的对称性要注意,否则会导致服务跑不起来。
补充一下书上的注意事项:
ProtobufDecoder仅仅负责解码,它不支持读半包,因此,在ProtobufDecoder前面,一定要有能够处理读半包的解码器,有以下三种方式可以选择。
1.使用Netty提供的ProtobufVarint32FrameDecoder,它可以处理半包消息;
2.继承Netty提供的通用半包解码器LenthFieldBasedFrameDecoder;
3.继承ByteToMessageDecoder类,自己处理半包消息。