赞
踩
开发一个gRpc接口包括两部分 一是编写proto文件,借助插件生成编译后的文件。二是基于编译后的文件编写gRpc服务实现类和方法。
下面示例创建一个 chainDismantleBillService.proto 文件
- syntax = "proto3";
- option java_package = "com.gRpc.demo.bill";
-
- //声明gRpc服务名 关键词 service
- service ChainBillService {
- //声明gRpc服务方法 关键词 rpc 参数包括入参和出参
- rpc queryBillDetail (billRequest) returns (queryBillDetailRes) {};
- }
- //声明gRpc方法中用到的参数结构 类型包括 uint64 uint32 string double 等
- message billRequest {
- uint64 groupID = 2;
- uint32 billStatus = 3;
- string auditBy = 4;
- string billIDs = 5;
- }
- message queryDismantleBillListReq {
- CommRequest commRequest = 1;
- uint64 groupID = 2;
- string allotIDs = 3;
- uint32 pageNo = 10;
- uint32 pageSize = 11;
- string auditBy = 12;
- uint64 demandID = 13;
- string goodsIDs = 14;
- string supplierIDs = 15;
- string billType = 16;
- }
- message queryBillDetailRes {
- string code = 1;
- string msg = 2;
- bool success = 3;
- //复合结构 声明Data类型并在queryBillDetailRes结构下引用该数据类型
- message Data {
- ChainBill bill = 1;
- //集合类型 关键字 repeated
- repeated ChainBillDetail details = 2;
- }
- Data data = 4;
- }
-
- message ChainBill {
- uint64 billID = 1 ;
- uint64 groupID = 2 ;/*集团ID*/
- uint32 demandType = 3 ;
- uint64 demandID = 4 ;
- string demandName = 5 ;
- string billNo = 6 ;
- uint32 billType = 7 ;
- uint32 billStatus = 8 ;
- uint64 billDate = 9 ;
- }
- message ChainBillDetail{
- uint64 billDetailID = 1;/*billDetailID*/
- uint64 groupID = 2;/*集团ID*/
- string billNo = 6;/*订单单号*/
- uint32 billType = 7;/*订单类型,( 0 订货单、1 退货单、2 采购申请单、3 采购订单、4 到货单、5 验货单、6 结算单、7 付款单)*/
- string goodsName = 22;/*物品名称*/
- double goodsNum = 26;/*物品数量*/
- double taxPrice = 27;/*含税单价*/
- double taxAmount = 28;/*含税金额*/
- string orgCode = 52;/*订单发起组织的编码*/
- uint32 isCreateBill = 53;/*是否生成订货单*/
- double rateValue = 54;/*供应商税率*/
- double assistUnitper = 55;/*辅助单位转换率*/
- string actionBy = 56;/*操作人字段*/
- }
- <build>
- <plugins>
- <!-- protobuf 生成bean 的插件 -->
- <plugin>
- <groupId>org.xolstice.maven.plugins</groupId>
- <artifactId>protobuf-maven-plugin</artifactId>
- <version>0.5.0</version>
- <extensions>true</extensions>
- <configuration>
- <protocArtifact>com.google.protobuf:protoc:3.4.0:exe:${os.detected.classifier}</protocArtifact>
- <clearOutputDirectory>false</clearOutputDirectory>
- <outputDirectory>src/main/java</outputDirectory><!-- 指定编译文件生成的目录位置 -->
- <pluginId>grpc-java</pluginId>
- <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.8.0:exe:${os.detected.classifier}</pluginArtifact>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>compile</goal>
- <goal>compile-custom</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
执行maven的install命令或单独执行maven中的Plugins中的插件,生成编译后的java文件。
编译后会生成两个文件,位置位于上面proto文件中option java_package指定的目录下。
新建服务实现类,继承上面生成的与proto同名的类的的内部抽象类(以ImplBase结尾),并给服务实现类添加@GRpcService注解,指定grpcServiceOuterClass属性为上面生成的与proto同名的类。
- @GRpcService(grpcServiceOuterClass = ChainDismantleBillServiceGrpc.class)
- public class ChainDismantleBillGrpcService extends ChainDismantleBillServiceGrpc.ChainDismantleBillServiceImplBase {
- @Autowired
- private ChainDismantleBillService chainDismantleBillService;
-
- //重写proto文件中同名的方法
- @Override
- public void queryDismantleBillDetail(ChainDismantleBillServiceOuterClass.billRequest request, StreamObserver<ChainDismantleBillServiceOuterClass.queryBillDetailRes> responseObserver) {
- ChainDismantleBillServiceOuterClass.queryDismantleBillDetailRes.Builder response = ChainDismantleBillServiceOuterClass.queryDismantleBillDetailRes.newBuilder();
- try {
- Long groupID = request.getGroupID();
- String billIDs = request.getBillIDs();
- ChainDismantleBillServiceOuterClass.queryDismantleBillDetailRes.Data.Builder data = chainDismantleBillService.queryDismantleBillDetail(groupID,billIDs);
- response.setData(data);
- response.setCode(BasicErrorCode.SUCCESS.getCode()).setSuccess(BasicErrorCode.SUCCESS.isSuccess()).setMsg(BasicErrorCode.SUCCESS.getMessage());
- } catch (Exception e) {
- GlobalGrpcException.handlerException(e, request, request.getCommRequest(), responseObserver);
- return;
- }
- //注意gRpc返回值的赋值方式 把结果赋值给responseObserver
- responseObserver.onNext(response.build());
- responseObserver.onCompleted();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。