手机怎样建立自己网站,社交营销,制作视频的网站软件,开江住房和城乡建设部网站4种通信模式
1、简单模式#xff08;Simple RPC#xff09;
简单模式#xff1a;也称简单 RPC#xff0c;即客户端发起一次请求#xff0c;服务端响应处理后返回一个结果给客户端。
在 proto 文件中可如下定义#xff1a;
rpc SayHello(HelloRequest) returns (Hello…4种通信模式
1、简单模式Simple RPC
简单模式也称简单 RPC即客户端发起一次请求服务端响应处理后返回一个结果给客户端。
在 proto 文件中可如下定义
rpc SayHello(HelloRequest) returns (HelloResponse);2、服务端数据流模式Server-side streaming RPC
服务端数据流模式也称服务端流式 RPC即客户端发起一次请求服务端可以连续返回数据流。 比如客户端向服务端发送了一个查询数据库的请求服务端持续返回多次结果。即客户端发送一次请求服务端查询到数据库有一万条数据服务端分批返回10次每次返回1000条数据给客户端。
在 proto 文件中可如下定义
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);注意在返回值前面加了stream
3、客户端数据流模式Client-side streaming RPC
客户端数据流模式也称客户端流式 RPC与服务端数据流模式相反客户端持续向服务端发送数据流在发送结束后由服务端返回一个响应。 比如客户端有一万条数据 分批多次请求服务端服务端接收后把这些数据都存到数据库然后返回一次结果给客户端。
在 proto 文件中可如下定义
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);注意在参数前面加了stream
4、双向数据流模式Bidirectional streaming RPC
双向数据流模式也称双向流式 RPC即客户端和服务端都可以向对方多次收发数据。
比如客户端有一万条数据 分批多次请求服务端服务端每次接收后存到数据库后都发送一次结果给客户端。
在 proto 文件中可如下定义
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);注意参数和返回前面都加了stream
代码示例
1、简单模式
product.proto
syntax proto3;option java_multiple_files true;
option java_package com.github.xjs.grpcapi;
option java_outer_classname ProductProto;package product;service ProductInfo {rpc addProduct (Product) returns (ProductId);rpc getProduct(ProductId) returns(Product);
}message Product {string id 1;string name2;string description3;float price4;
}message ProductId {string value 1;
}客户端
public static void main(String[] args) {ManagedChannel channel ManagedChannelBuilder.forAddress(localhost, 50050).usePlaintext().build();ProductInfoGrpc.ProductInfoBlockingStub stub ProductInfoGrpc.newBlockingStub(channel);Product p Product.newBuilder().setId(1).setPrice(100).setName(21天精通Java).setDescription(21天精通Java).build();ProductId productId stub.addProduct(p);System.out.println(productId.getValue() productId.getValue());Product product stub.getProduct(ProductId.newBuilder().setValue(99999).build());System.out.println(product.getName() product.getName());channel.shutdown();
}服务端
public class ProductInfoImpl extends ProductInfoGrpc.ProductInfoImplBase {Overridepublic void addProduct(Product request, StreamObserverProductId responseObserver) {// System.out.println(request.toString() request.toString());System.out.println(TextFormat.printer().escapingNonAscii(false).printToString(request));ProductId productId ProductId.newBuilder().setValue(request.getId()).build();responseObserver.onNext(productId);responseObserver.onCompleted();}Overridepublic void getProduct(ProductId request, StreamObserverProduct responseObserver) {System.out.println(TextFormat.printer().escapingNonAscii(false).printToString(request));Product product Product.newBuilder().setId(request.getValue()).setName(三国演义).build();responseObserver.onNext(product);responseObserver.onCompleted();}
}2、服务端数据流模式Server-side streaming RPC
product.proto
syntax proto3;option java_multiple_files true;
option java_package com.github.xjs.grpcapi.clientstream;
option java_outer_classname ProductProto;package product.clientstream;service ProductInfo {rpc getProductBatch (ProductGetBatchRequest) returns (stream Product);
}message ProductGetBatchRequest {int32 count 10;}message Product {string id 1;string name2;string description3;float price4;
}客户端
public static void main(String[] args) throws Exception {CountDownLatch countDownLatch new CountDownLatch(1);ManagedChannel channel ManagedChannelBuilder.forAddress(localhost, 50050).usePlaintext().build();ProductInfoGrpc.ProductInfoStub stub ProductInfoGrpc.newStub(channel);// 等待接收服务端的响应StreamObserverProduct responseObserver new StreamObserverProduct() {Overridepublic void onNext(Product result) {System.out.println(服务端返回 result.toString());}Overridepublic void onError(Throwable throwable) {throwable.printStackTrace();}Overridepublic void onCompleted() {System.out.println(服务端响应完成);// 关闭channelchannel.shutdown();// 结束程序countDownLatch.countDown();}};ProductGetBatchRequest request ProductGetBatchRequest.newBuilder().setCount(10).build();stub.getProductBatch(request, responseObserver);// 等待结束countDownLatch.await();}服务端
Override
public void getProductBatch(ProductGetBatchRequest request, StreamObserverProduct responseObserver) {int count request.getCount();for(int i0; icount; i){Product product Product.newBuilder().setId((11)).setName(product i) .setPrice(100i).build();responseObserver.onNext(product);System.out.println(发送数据 product);}responseObserver.onCompleted();System.out.println(发送完成);
}3、客户端数据流模式Client-side streaming RPC
product.proto
syntax proto3;option java_multiple_files true;
option java_package com.github.xjs.grpcapi.clientstream;
option java_outer_classname ProductProto;package product.clientstream;service ProductInfo {rpc addProductBatch (stream Product) returns (ProductAddResult);
}message Product {string id 1;string name2;string description3;float price4;
}message ProductAddResult {int32 count 1;
}客户端
public static void main(String[] args) throws Exception {CountDownLatch countDownLatch new CountDownLatch(1);ManagedChannel channel ManagedChannelBuilder.forAddress(localhost, 50050).usePlaintext().build();ProductInfoGrpc.ProductInfoStub stub ProductInfoGrpc.newStub(channel);// 等待接收服务端的响应StreamObserverProductAddResult responseObserver new StreamObserverProductAddResult() {Overridepublic void onNext(ProductAddResult result) {System.out.println(服务端返回 result.toString());}Overridepublic void onError(Throwable throwable) {throwable.printStackTrace();}Overridepublic void onCompleted() {System.out.println(服务端响应完成);// 关闭channelchannel.shutdown();// 结束程序countDownLatch.countDown();}};StreamObserverProduct requestObserver stub.addProductBatch(responseObserver);for(int i0;i10;i){Product p Product.newBuilder().setId( (i1)).setPrice(100).setName(21天精通Java).setDescription(21天精通Java).build();requestObserver.onNext(p);}requestObserver.onCompleted();// 等待结束countDownLatch.await();
}服务端
public io.grpc.stub.StreamObserverProduct addProductBatch(io.grpc.stub.StreamObserverProductAddResult responseObserver) {return new StreamObserverProduct() {ListProduct products new ArrayList();Overridepublic void onNext(Product product) {// 接收客户端请求System.out.println(TextFormat.printer().escapingNonAscii(false).printToString(product));products.add(product);}Overridepublic void onError(Throwable throwable) {// 错误处理throwable.printStackTrace();}Overridepublic void onCompleted() {// 客户端请求结束发送响应ProductAddResult result ProductAddResult.newBuilder().setCount(products.size()).build();responseObserver.onNext(result);responseObserver.onCompleted();System.out.println(服务端响应结束);}};
}4、双向数据流模式Bidirectional streaming RPC
product.proto
syntax proto3;option java_multiple_files true;
option java_package com.github.xjs.grpcapi.bidirectionalstream;
option java_outer_classname ProductProto;package product.clientstream;service ProductInfo {rpc saveProductBatch (stream Product) returns (stream ProductSaveResult);
}message ProductSaveResult {bool success 1;
}message Product {string id 1;string name2;string description3;float price4;
}客户端
public static void main(String[] args) throws Exception {CountDownLatch countDownLatch new CountDownLatch(1);ManagedChannel channel ManagedChannelBuilder.forAddress(localhost, 50050).usePlaintext().build();ProductInfoGrpc.ProductInfoStub stub ProductInfoGrpc.newStub(channel);// 等待接收服务端的响应StreamObserverProductSaveResult responseObserver new StreamObserverProductSaveResult() {Overridepublic void onNext(ProductSaveResult result) {System.out.println(服务端返回 result.toString());}Overridepublic void onError(Throwable throwable) {throwable.printStackTrace();}Overridepublic void onCompleted() {System.out.println(服务端响应完成);// 关闭channelchannel.shutdown();// 结束程序countDownLatch.countDown();}};StreamObserverProduct requestObserver stub.saveProductBatch(responseObserver);for(int i0; i10; i){Product p Product.newBuilder().setId((i1)).setName(producti).setPrice(100i).build();requestObserver.onNext(p);System.out.println(客户端发送 p.toString());}requestObserver.onCompleted();System.out.println(客户端发送完成);// 等待结束countDownLatch.await();
}服务端
Override
public StreamObserverProduct saveProductBatch(StreamObserverProductSaveResult responseObserver) {return new StreamObserverProduct() {Overridepublic void onNext(Product product) {System.out.println(收到客户端请求 product);ProductSaveResult result ProductSaveResult.newBuilder().setSuccess(true).build();System.out.println(发送响应);responseObserver.onNext(result);}Overridepublic void onError(Throwable throwable) {throwable.printStackTrace();}Overridepublic void onCompleted() {System.out.println(客户端请求完成);responseObserver.onCompleted();System.out.println(服务端响应完成);}};
}完成的源码下载https://github.com/xjs1919/learning-demo/tree/master/grpc-demo 文章转载自: http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.yqlrq.cn.gov.cn.yqlrq.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.tgdys.cn.gov.cn.tgdys.cn http://www.morning.txnqh.cn.gov.cn.txnqh.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.djpzg.cn.gov.cn.djpzg.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.lcbnb.cn.gov.cn.lcbnb.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.chjnb.cn.gov.cn.chjnb.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.nfmlt.cn.gov.cn.nfmlt.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn