当前位置:   article > 正文

基于SpringBoot开发gRpc服务接口_@grpcservice

@grpcservice

开发一个gRpc接口包括两部分 一是编写proto文件,借助插件生成编译后的文件。二是基于编译后的文件编写gRpc服务实现类和方法。

声明proto文件

下面示例创建一个 chainDismantleBillService.proto 文件

  1. syntax = "proto3";
  2. option java_package = "com.gRpc.demo.bill";
  3. //声明gRpc服务名 关键词 service
  4. service ChainBillService {
  5. //声明gRpc服务方法 关键词 rpc 参数包括入参和出参
  6. rpc queryBillDetail (billRequest) returns (queryBillDetailRes) {};
  7. }
  8. //声明gRpc方法中用到的参数结构 类型包括 uint64 uint32 string double 等
  9. message billRequest {
  10. uint64 groupID = 2;
  11. uint32 billStatus = 3;
  12. string auditBy = 4;
  13. string billIDs = 5;
  14. }
  15. message queryDismantleBillListReq {
  16. CommRequest commRequest = 1;
  17. uint64 groupID = 2;
  18. string allotIDs = 3;
  19. uint32 pageNo = 10;
  20. uint32 pageSize = 11;
  21. string auditBy = 12;
  22. uint64 demandID = 13;
  23. string goodsIDs = 14;
  24. string supplierIDs = 15;
  25. string billType = 16;
  26. }
  27. message queryBillDetailRes {
  28. string code = 1;
  29. string msg = 2;
  30. bool success = 3;
  31. //复合结构 声明Data类型并在queryBillDetailRes结构下引用该数据类型
  32. message Data {
  33. ChainBill bill = 1;
  34. //集合类型 关键字 repeated
  35. repeated ChainBillDetail details = 2;
  36. }
  37. Data data = 4;
  38. }
  39. message ChainBill {
  40. uint64 billID = 1 ;
  41. uint64 groupID = 2 ;/*集团ID*/
  42. uint32 demandType = 3 ;
  43. uint64 demandID = 4 ;
  44. string demandName = 5 ;
  45. string billNo = 6 ;
  46. uint32 billType = 7 ;
  47. uint32 billStatus = 8 ;
  48. uint64 billDate = 9 ;
  49. }
  50. message ChainBillDetail{
  51. uint64 billDetailID = 1;/*billDetailID*/
  52. uint64 groupID = 2;/*集团ID*/
  53. string billNo = 6;/*订单单号*/
  54. uint32 billType = 7;/*订单类型,( 0 订货单、1 退货单、2 采购申请单、3 采购订单、4 到货单、5 验货单、6 结算单、7 付款单)*/
  55. string goodsName = 22;/*物品名称*/
  56. double goodsNum = 26;/*物品数量*/
  57. double taxPrice = 27;/*含税单价*/
  58. double taxAmount = 28;/*含税金额*/
  59. string orgCode = 52;/*订单发起组织的编码*/
  60. uint32 isCreateBill = 53;/*是否生成订货单*/
  61. double rateValue = 54;/*供应商税率*/
  62. double assistUnitper = 55;/*辅助单位转换率*/
  63. string actionBy = 56;/*操作人字段*/
  64. }

在pom文件中添加proto编译插件

  1. <build>
  2. <plugins>
  3. <!-- protobuf 生成bean 的插件 -->
  4. <plugin>
  5. <groupId>org.xolstice.maven.plugins</groupId>
  6. <artifactId>protobuf-maven-plugin</artifactId>
  7. <version>0.5.0</version>
  8. <extensions>true</extensions>
  9. <configuration>
  10. <protocArtifact>com.google.protobuf:protoc:3.4.0:exe:${os.detected.classifier}</protocArtifact>
  11. <clearOutputDirectory>false</clearOutputDirectory>
  12. <outputDirectory>src/main/java</outputDirectory><!-- 指定编译文件生成的目录位置 -->
  13. <pluginId>grpc-java</pluginId>
  14. <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.8.0:exe:${os.detected.classifier}</pluginArtifact>
  15. </configuration>
  16. <executions>
  17. <execution>
  18. <goals>
  19. <goal>compile</goal>
  20. <goal>compile-custom</goal>
  21. </goals>
  22. </execution>
  23. </executions>
  24. </plugin>
  25. </plugins>
  26. </build>

执行maven的install命令或单独执行maven中的Plugins中的插件,生成编译后的java文件。

编译后会生成两个文件,位置位于上面proto文件中option java_package指定的目录下。

 

编写gRpc服务实现类

新建服务实现类,继承上面生成的与proto同名的类的的内部抽象类(以ImplBase结尾),并给服务实现类添加@GRpcService注解,指定grpcServiceOuterClass属性为上面生成的与proto同名的类。

  1. @GRpcService(grpcServiceOuterClass = ChainDismantleBillServiceGrpc.class)
  2. public class ChainDismantleBillGrpcService extends ChainDismantleBillServiceGrpc.ChainDismantleBillServiceImplBase {
  3. @Autowired
  4. private ChainDismantleBillService chainDismantleBillService;
  5. //重写proto文件中同名的方法
  6. @Override
  7. public void queryDismantleBillDetail(ChainDismantleBillServiceOuterClass.billRequest request, StreamObserver<ChainDismantleBillServiceOuterClass.queryBillDetailRes> responseObserver) {
  8. ChainDismantleBillServiceOuterClass.queryDismantleBillDetailRes.Builder response = ChainDismantleBillServiceOuterClass.queryDismantleBillDetailRes.newBuilder();
  9. try {
  10. Long groupID = request.getGroupID();
  11. String billIDs = request.getBillIDs();
  12. ChainDismantleBillServiceOuterClass.queryDismantleBillDetailRes.Data.Builder data = chainDismantleBillService.queryDismantleBillDetail(groupID,billIDs);
  13. response.setData(data);
  14. response.setCode(BasicErrorCode.SUCCESS.getCode()).setSuccess(BasicErrorCode.SUCCESS.isSuccess()).setMsg(BasicErrorCode.SUCCESS.getMessage());
  15. } catch (Exception e) {
  16. GlobalGrpcException.handlerException(e, request, request.getCommRequest(), responseObserver);
  17. return;
  18. }
  19. //注意gRpc返回值的赋值方式 把结果赋值给responseObserver
  20. responseObserver.onNext(response.build());
  21. responseObserver.onCompleted();
  22. }
  23. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/128700
推荐阅读
相关标签
  

闽ICP备14008679号