日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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