微服务从grpc到客户端再到服务端

proto文件如下:

message ProdHoldingInfoRequest {
    oneof check_paging {
        int32 paging = 1001; //是否分页
    }
    oneof check_pageno {
        int32 pageno = 1002; //页码
    }
    oneof check_pagelength {
        int32 pagelength = 1003; //页长
    }
    oneof check_total {
        int32 total = 1004; //记录数
    }
    string sort = 1005; //排序条件
    oneof check_czr {
        int32 czr = 1; //操作人
    }
    oneof check_cpid {
        int32 cpid = 2; //ID
    }
}
message ProdHoldingInfoResponse {
    int32 code = 1; //返回值
    string note = 2; //返回消息
    int32 hasrecordset = 1006; //是否返回记录集
    int32 total = 1007; //总记录数
    repeated ProdHoldingInfoRecord records = 3; //返回结果集
}
message ProdHoldingInfoRecord {   //返回的结果集中的参数
    string cpdm = 1;    
    string khmc = 2;    
    string khh = 3;     
    string khid = 4;    
    string cpfe = 5;    
    string djfe = 6;    
    string zxsz = 7;   
    string zxjz = 8;    
}

service ProdViewService {
rpc ProdHoldingInfo (ProdHoldingInfoRequest) returns (ProdHoldingInfoResponse) {};
}
proto总结:

1.proto将会生成ProdViewServiceGrpc文件,里面是生成的代码
2.oneof check_xxx{…}的作用就是服务里传进去参数不能为空,
如oneof check_cpid {
int32 cpid = 2; //服务端传进去的cpid不能为空不能没有值
}
3.message ProdHoldingInfoResponse里的repeated ProdHoldingInfoRecord records=3 是返回的一个结果集,可以理解为一个数组,
可以看到message ProdHoldingInfoRecord里定义的具体返回结果集的参数。
有时候Response里需要返回数组,可以这样写repeated string name=4,这就返回了一个叫name的string类型数组。

客户端如下:

 public ProdHoldingInfoResponse prodHoldingInfo(ProdHoldingInfoModel prodHoldingInfoModel, int czr) { //ProdHoldingInfoModel里是前端传进来的参数
        ProdHoldingInfoRequest.Builder prodHoldingInfoBuild = ProdHoldingInfoRequest.newBuilder();     //创建Builder
        if (null != prodHoldingInfoModel.getPaging()) {                                           //如果paging不为空
            prodHoldingInfoBuild.setPaging(prodHoldingInfoModel.getPaging());   //在Builder里设置paging
        }
        if (null != prodHoldingInfoModel.getPageNo()) {
            prodHoldingInfoBuild.setPageno(prodHoldingInfoModel.getPageNo());
        }
        if (null != prodHoldingInfoModel.getPageLength()) {
            prodHoldingInfoBuild.setPagelength(prodHoldingInfoModel.getPageLength());
        }
        if (null != prodHoldingInfoModel.getTotalRows()) {
            prodHoldingInfoBuild.setTotal(prodHoldingInfoModel.getTotalRows());
        }
        if (null != prodHoldingInfoModel.getSort()) {
            prodHoldingInfoBuild.setSort(prodHoldingInfoModel.getSort());
        }
        prodHoldingInfoBuild.setCzr(czr);
        if (null != prodHoldingInfoModel.getCpid()) {
            prodHoldingInfoBuild.setCpid(prodHoldingInfoModel.getCpid());
        }
        ProdHoldingInfoRequest req = prodHoldingInfoBuild.build();  //创建Request,把Builder加到Request里
        ProdHoldingInfoResponse resp = stub.prodHoldingInfo(req);//创建Response,调用服务端函数,Request作为参数。当然上面一个语句和这个语句可合为一条,调用服务端函数,直接把Builder作为参数如:ProdHoldingInfoResponse resp = stub.prodHoldingInfo(prodHoldingInfoBuild.build());
        return resp; 
    }
客户端总结

一般接口接受前端传进来的参数放到modle里,接口调用客户端函数并将modle传过来,客户端函数接受modle里的参数做初步处理判断处理,
在Builder里设置proto中的Request 的一些参数,然后Builder加到Request,Request加到Response并调用服务端函数。

服务端如下:

public void prodHoldingInfo(ProdHoldingInfoRequest request, StreamObserver<ProdHoldingInfoResponse> responseObserver) {  //被客户端调用的服务端函数
        Map<String, Object> ins = new HashMap<>(2);
        if (ProdHoldingInfoRequest.CheckPagingCase.PAGING == request.getCheckPagingCase()) {       //判断传进来的paging是否有值
            ins.put("I_PAGING", request.getPaging());                                                                                  //有值则put到ins里
        }
        if (ProdHoldingInfoRequest.CheckPagenoCase.PAGENO == request.getCheckPagenoCase()) {
            ins.put("I_PAGENO", request.getPageno());
        }
        if (ProdHoldingInfoRequest.CheckPagelengthCase.PAGELENGTH == request.getCheckPagelengthCase()) {
            ins.put("I_PAGELENGTH", request.getPagelength());
        }
        if (ProdHoldingInfoRequest.CheckTotalCase.TOTAL == request.getCheckTotalCase()) {
            ins.put("I_TOTALROWS", request.getTotal());
        }
        ins.put("I_SORT", request.getSort());
        if (ProdHoldingInfoRequest.CheckCzrCase.CZR == request.getCheckCzrCase()) {
            ins.put("I_CZR", request.getCzr());
        }
        if (ProdHoldingInfoRequest.CheckCpidCase.CPID == request.getCheckCpidCase()) {
            ins.put("I_CPID", request.getCpid());
        }
        List<QueryProdHoldingInfoModel> resultList = prodViewDao.queryProdHoldingInfoBase(ins);      //带有所有请求参数的ins调用Dao层的queryProdHoldingInfoBase,返回的结果集存为resultList

        ProdHoldingInfoResponse.Builder response = ProdHoldingInfoResponse.newBuilder();//创建返回类型Builder
        try {
            response.setCode((Integer) ins.get("O_CODE"));                                                         //设置返回的状态码等
            response.setNote((String) ins.get("O_NOTE"));
            response.setHasrecordset((Integer) ins.get("O_HASRECORDSET"));
            response.setTotal((Integer) ins.get("I_TOTALROWS"));

            for (QueryProdHoldingInfoModel queryProdHoldingInfo : resultList) {               //遍历resultList读取结果集
                ProdHoldingInfoRecord record = ProdHoldingInfoRecord.newBuilder()        //获取结果集中的一系列参数
                        .setCpdm(getString(queryProdHoldingInfo.getCpdm()))
                        .setKhmc(getString(queryProdHoldingInfo.getKhmc()))
                        .setKhh(getString(queryProdHoldingInfo.getKhh()))
                        .setKhid(getString(queryProdHoldingInfo.getKhid()))
                        .setCpfe(getString(queryProdHoldingInfo.getCpfe()))
                        .setDjfe(getString(queryProdHoldingInfo.getDjfe()))
                        .setZxsz(getString(queryProdHoldingInfo.getZxsz()))
                        .setZxjz(getString(queryProdHoldingInfo.getZxjz()))
                        .build();
                response.addRecords(record);                      //将结果集加到Builder里
            }
        } catch (Exception e) {

            log.error(e.getMessage());
            responseObserver.onError(new StatusException(Status.INTERNAL));
            return;
        }
        ProdHoldingInfoResponse resp = response.build();   //将Builder加入到Response里
        responseObserver.onNext(resp);                              //返回Response
        responseObserver.onCompleted();
    }  
服务端总结

客户端Request将参数传到服务端函数处理,函数调用Dao层接口去数据库取数据,取到的数据放到Record中,Record加Builder里,Builder又加Response 里,最后返回到客户端

你可能感兴趣的:(微服务)