日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GRPC java实现demo

發布時間:2025/4/16 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GRPC java实现demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、環境?

? ? ? ?jdk8+maven+IDEA

二、 我的理解+官方文檔

? ? ? ?1、官方文檔:https://codelabs.developers.google.com/codelabs/cloud-grpc-java/index.html#2

? ? ? ?2、我的理解:grpc是利用.proto文件用相應的語言編譯生成相應的代碼,這樣就能實現不同語言平臺之間的調用。最神奇的是仿佛客戶端和服務器端調用的是相同的函數,在相同函數里面實現通信,相當的容易理解。

三、我的github+代碼

? ? ?1、github鏈接:https://github.com/cysisu/grpc-java.git

? ? ?2、代碼如下:

? ? ? ? ? ?(1)pop.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>com.grpc.cube</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty</artifactId><version>1.7.0</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.7.0</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>1.7.0</version></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.5.0.Final</version></extension></extensions><plugins><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.5.0</version><configuration><protocArtifact>com.google.protobuf:protoc:3.4.0:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.7.0:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build></project>

? ? ? ? ? ? ? ? (2) 客戶端:

? ? ? ? ? ? ??

package com.example.grpc;import io.grpc.*;public class Client {public static void main( String[] args ) throws Exception{// Channel is the abstraction to connect to a service endpoint// Let's use plaintext communication because we don't have certsfinal ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8080").usePlaintext(true).build();// It is up to the client to determine whether to block the call// Here we create a blocking stub, but an async stub,// or an async stub with Future are always possible.GreetingServiceGrpc.GreetingServiceBlockingStub stub = GreetingServiceGrpc.newBlockingStub(channel);GreetingServiceOuterClass.HelloRequest request =GreetingServiceOuterClass.HelloRequest.newBuilder().setName("Ray").build();// Finally, make the call using the stubGreetingServiceOuterClass.HelloResponse response =stub.greeting(request);System.out.println(response);// A Channel should be shutdown before stopping the process.channel.shutdownNow();} }

?

? ? ? ?(3)服務器端調用的函數:

package com.example.grpc;import io.grpc.stub.StreamObserver;public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {public void greeting(GreetingServiceOuterClass.HelloRequest request,StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {// HelloRequest has toString auto-generated.System.out.println(request);// You must use a builder to construct a new Protobuffer objectGreetingServiceOuterClass.HelloResponse response = GreetingServiceOuterClass.HelloResponse.newBuilder().setGreeting("Hello there, " + request.getName()).build();// Use responseObserver to send a single response backresponseObserver.onNext(response);// When you are done, you must call onCompleted.responseObserver.onCompleted();} }

? ? ? ? (5)服務器端:

package com.example.grpc; import io.grpc.*;public class App {public static void main( String[] args ) throws Exception{// Create a new server to listen on port 8080Server server = ServerBuilder.forPort(8080).addService(new GreetingServiceImpl()).build();// Start the serverserver.start();// Server threads are running in the background.System.out.println("Server started");// Don't exit the main thread. Wait until server is terminated.server.awaitTermination();} }

? ? ? ?(6)proto文件:

? ??

syntax = "proto3"; package com.example.grpc;// Request payload message HelloRequest {// Each message attribute is strongly typed.// You also must assign a "tag" number.// Each tag number is unique within the message.string name = 1;// This defines a strongly typed list of Stringrepeated string hobbies = 2;// There are many more basics types, like Enum, Map// See https://developers.google.com/protocol-buffers/docs/proto3// for more information. }message HelloResponse {string greeting = 1; }// Defining a Service, a Service can have multiple RPC operations service GreetingService {// Define a RPC operationrpc greeting(HelloRequest) returns (HelloResponse); }

?

總結

以上是生活随笔為你收集整理的GRPC java实现demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 五月天久久久 | 久久久久亚洲av成人网人人软件 | 中文字幕在线色 | 性一交一乱一色一免费无遮挡 | 久久综合成人网 | 亚洲视频观看 | 污污视频网站在线免费观看 | 黄av在线 | a色网站| 亚洲天堂日韩在线 | av视屏| 九色porny视频 | 国产xxxx| 麻豆蜜桃视频 | 少妇一级淫片免费视频 | 亚洲第一视频网 | 日本一区二区视频在线观看 | 欧美另类老妇 | 国产成人午夜高潮毛片 | 久久久久久久久蜜桃 | 精品人伦一区二区 | 日韩黄色一级 | 国产调教视频 | 久久噜噜噜精品国产亚洲综合 | 亚洲精品一区在线观看 | www.毛片| 国产午夜视频在线 | 日本3级网站 | 亚洲国产第一页 | 大尺度在线观看 | 日韩精品一区二区三区在线视频 | 国产欧美日本在线 | 亚洲精品综合在线 | 国产精品福利一区二区三区 | 久久精品视频中文字幕 | 天堂网在线资源 | 极品熟妇大蝴蝶20p 国产偷自拍视频 | 一区二区麻豆 | 亚洲天堂av影院 | 吃瓜网今日吃瓜 热门大瓜 色婷在线 | 国产婷婷色 | 天天干夜夜怕 | 午夜久久久久久久久 | 91国内精品久久久久 | 中文字幕网址在线 | 91成人黄色 | 久久精品国产亚洲av麻豆 | 在线免费视频你懂的 | 中文字幕在线观看一区二区三区 | 国产成人在线一区二区 | 亚洲视频在线免费播放 | 影音先锋黑人 | 久久一级大片 | 亚色在线 | 欧美啪啪一区二区 | 青青草视频成人 | 男女作爱网站 | 欧美黑人一级爽快片淫片高清 | 91香蕉国产在线观看软件 | 亚洲一区二区三区在线视频观看 | 亚洲Av无码成人精品区伊人 | 日本伦理片在线播放 | 午夜伦情 | 向日葵视频在线 | 欧美交换国产一区内射 | 男人天堂资源网 | 久久综合久色欧美综合狠狠 | 亚洲一页 | 亚洲av永久无码精品国产精品 | 涩涩一区 | 日本乱子伦xxxx| 欧美性生活精品 | 韩国美女一区二区 | 深夜福利av| 亚洲女人av | 青青超碰 | 中出精品| 黄色一级淫片 | 最新中文字幕在线视频 | 污视频网站免费 | 人人澡人人插 | 日本成人在线视频网站 | 瑟瑟视频在线 | 冲田杏梨一区二区三区 | 亚洲 欧美 综合 | 在线国产一区 | 国产熟女一区二区三区四区 | 一级免费大片 | 日韩久草| 日本在线观看中文字幕 | 色屋在线| 亚洲精华国产精华精华液网站 | 亚洲精品日韩丝袜精品 | 91调教视频| 欧美三级午夜理伦三级老人 | 非洲黄色一级片 | 国产femdom调教7777 | 久久深夜视频 | 亚洲女优在线 |