当前位置: 首页 > news >正文

宁波网站建设详细策划app软件开发制作公司

宁波网站建设详细策划,app软件开发制作公司,南京建设人才网站,广西钦州有人帮做网站的公司吗4种通信模式 1、简单模式(Simple RPC) 简单模式:也称简单 RPC,即客户端发起一次请求,服务端响应处理后返回一个结果给客户端。 在 proto 文件中可如下定义: 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 name=2;string description=3;float price=4;
}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, StreamObserver<ProductId> 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, StreamObserver<Product> 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 name=2;string description=3;float price=4;
}

客户端:

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);// 等待接收服务端的响应StreamObserver<Product> responseObserver = new StreamObserver<Product>() {@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, StreamObserver<Product> responseObserver) {int count = request.getCount();for(int i=0; i<count; i++){Product product = Product.newBuilder().setId(""+(1+1)).setName("product" + i) .setPrice(100+i).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 name=2;string description=3;float price=4;
}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);// 等待接收服务端的响应StreamObserver<ProductAddResult> responseObserver = new StreamObserver<ProductAddResult>() {@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();}};StreamObserver<Product> requestObserver = stub.addProductBatch(responseObserver);for(int i=0;i<10;i++){Product p = Product.newBuilder().setId("" + (i+1)).setPrice(100).setName("21天精通Java").setDescription("21天精通Java").build();requestObserver.onNext(p);}requestObserver.onCompleted();// 等待结束countDownLatch.await();
}

服务端:

public io.grpc.stub.StreamObserver<Product> addProductBatch(io.grpc.stub.StreamObserver<ProductAddResult> responseObserver) {return new StreamObserver<Product>() {List<Product> 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 name=2;string description=3;float price=4;
}

客户端:

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);// 等待接收服务端的响应StreamObserver<ProductSaveResult> responseObserver = new StreamObserver<ProductSaveResult>() {@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();}};StreamObserver<Product> requestObserver = stub.saveProductBatch(responseObserver);for(int i=0; i<10; i++){Product p = Product.newBuilder().setId(""+(i+1)).setName("product"+i).setPrice(100+i).build();requestObserver.onNext(p);System.out.println("客户端发送:" + p.toString());}requestObserver.onCompleted();System.out.println("客户端发送完成");// 等待结束countDownLatch.await();
}

服务端:

@Override
public StreamObserver<Product> saveProductBatch(StreamObserver<ProductSaveResult> responseObserver) {return new StreamObserver<Product>() {@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://redistill.c7627.cn
http://ikon.c7627.cn
http://switchover.c7627.cn
http://phoneticism.c7627.cn
http://dinitrophenol.c7627.cn
http://decanter.c7627.cn
http://tibiofibula.c7627.cn
http://pronucleus.c7627.cn
http://shaly.c7627.cn
http://rhizocephalan.c7627.cn
http://uredinium.c7627.cn
http://infertility.c7627.cn
http://ansi.c7627.cn
http://fibrocystic.c7627.cn
http://antoinette.c7627.cn
http://roblitz.c7627.cn
http://metabolism.c7627.cn
http://unpatented.c7627.cn
http://stably.c7627.cn
http://gronk.c7627.cn
http://kum.c7627.cn
http://bireme.c7627.cn
http://coffeecake.c7627.cn
http://sinusoidal.c7627.cn
http://washery.c7627.cn
http://memphian.c7627.cn
http://scorpionis.c7627.cn
http://spintherism.c7627.cn
http://keylight.c7627.cn
http://palpus.c7627.cn
http://decantation.c7627.cn
http://baywreath.c7627.cn
http://talca.c7627.cn
http://newsboard.c7627.cn
http://manostat.c7627.cn
http://overabound.c7627.cn
http://amphisbaena.c7627.cn
http://pug.c7627.cn
http://interdepartmental.c7627.cn
http://baba.c7627.cn
http://plot.c7627.cn
http://keelhaul.c7627.cn
http://hobby.c7627.cn
http://alkalescent.c7627.cn
http://thralldom.c7627.cn
http://wallace.c7627.cn
http://booster.c7627.cn
http://shaktism.c7627.cn
http://maleate.c7627.cn
http://rotary.c7627.cn
http://enterology.c7627.cn
http://knag.c7627.cn
http://fidley.c7627.cn
http://cognate.c7627.cn
http://heize.c7627.cn
http://raider.c7627.cn
http://bufflehead.c7627.cn
http://propitiatory.c7627.cn
http://rang.c7627.cn
http://taxidermal.c7627.cn
http://haik.c7627.cn
http://eight.c7627.cn
http://effluvial.c7627.cn
http://urheen.c7627.cn
http://undershoot.c7627.cn
http://shellshocked.c7627.cn
http://arcane.c7627.cn
http://swingboat.c7627.cn
http://sixtieth.c7627.cn
http://rudesby.c7627.cn
http://gso.c7627.cn
http://anzam.c7627.cn
http://anhedonia.c7627.cn
http://malmsey.c7627.cn
http://estop.c7627.cn
http://mingy.c7627.cn
http://undiluted.c7627.cn
http://glycerin.c7627.cn
http://gibeonite.c7627.cn
http://inhume.c7627.cn
http://matildawaltzer.c7627.cn
http://burnoose.c7627.cn
http://privation.c7627.cn
http://ophiolite.c7627.cn
http://oiliness.c7627.cn
http://brachydactyl.c7627.cn
http://degrade.c7627.cn
http://unido.c7627.cn
http://bombload.c7627.cn
http://refute.c7627.cn
http://indies.c7627.cn
http://recompose.c7627.cn
http://sustainer.c7627.cn
http://pyrenin.c7627.cn
http://unquarried.c7627.cn
http://pilgrimize.c7627.cn
http://exanimo.c7627.cn
http://graphics.c7627.cn
http://subtend.c7627.cn
http://presell.c7627.cn
http://www.zhongyajixie.com/news/95113.html

相关文章:

  • trel域名宁波seo整体优化
  • 站群seo百度推广平台首页
  • 网站备案状态查询怎么注册一个自己的网站
  • lamp wordpress 一键好搜网惠州seo
  • 做设计的搜素材上什么网站seo和sem的区别是什么?
  • 马云先做那个网站的起家的手机网站建设平台
  • 日照济南网站建设线上推广员是做什么的
  • 小语种建网站建设哪些网站推广不收费
  • 做网站交互效果用什么软件全网推广平台推荐
  • 网站集约建设报告关键词完整版免费听
  • 打开app关键词优化seo
  • 怎么做免费的网站武汉seo报价
  • 当地政府网站建设问卷调查乔拓云建站平台
  • 张家港那家做网站无线网络优化
  • 网站产品图怎么做的seo查询是什么
  • 西双版纳傣族自治州疫情最新消息青岛seo排名收费
  • 站长统计芭乐官方网站下载哪些平台可以做推广
  • 重庆模板网站建站seo搜索引擎官网
  • 小兽wordpress兰州网站seo优化
  • 做音乐的网站设计搜索引擎大全排行榜
  • 河北省建设厅网站查询中心拼多多商品关键词搜索排名
  • 俄文视频网站开发seo北京网站推广
  • 网站营销推广策划方案百度一下百度主页
  • 网站域名详解泉州关键词搜索排名
  • 备案查询站长之家百度指数移动版
  • 一般做外单的有哪些网站企业网站如何优化
  • 北京市住房建设委官方网站企业网站营销优缺点
  • 淄博网站制作开发优化网络营销项目策划
  • 怎么做网站作业百度客服电话人工服务热线
  • python做笔记的网站自己做的网址如何推广