Java实现gRPC服务

GRPC

gRPC由GOOGLE开发,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。

PB协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
syntax = "proto3";

package com.example.grpc;

option java_multiple_files = true;

enum Sentiment {
HAPPY = 0;
SLEEPY = 1;
ANGRY = 2;
}

message HelloRequest {
string name = 1;
int32 age = 2;
repeated string hobbies = 3;
map<string, string> bagOfTricks = 4;
Sentiment sentiment = 5;
}

message HelloResponse {
string greeting = 1;
}

// 4. service, unary request/response
service GreetingService {
rpc greeting(HelloRequest) returns (HelloResponse);
}

Maven

pb和gRPC包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.10.0</version>
</dependency>

<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.31.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.31.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.31.0</version>
</dependency>
</dependencies>

编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<build>
<defaultGoal>package</defaultGoal>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>

<plugins>
<!-- protobuf 编译组件 -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<extensions>true</extensions>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.20.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MyGrpcServer {
static public void main(String [] args) throws IOException, InterruptedException {
Server server = ServerBuilder.forPort(8080)
.addService(new GreetingServiceImpl()).build();

System.out.println("Starting server...");
server.start();
System.out.println("Server started!");
server.awaitTermination();
}

public static class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
@Override
public void greeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
System.out.println(request);

String greeting = "Hello there, " + request.getName();

HelloResponse response = HelloResponse.newBuilder().setGreeting(greeting).build();

responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
}

Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MyGrpcClient {
public static void main(String[] args) throws InterruptedException {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
.usePlaintext(true)
.build();

GreetingServiceGrpc.GreetingServiceBlockingStub stub =
GreetingServiceGrpc.newBlockingStub(channel);

HelloResponse helloResponse = stub.greeting(
HelloRequest.newBuilder()
.setName("Ray")
.setAge(18)
.setSentiment(Sentiment.HAPPY)
.build());

System.out.println(helloResponse);

channel.shutdown();
}
}
------ 本文结束------

本文标题:Java实现gRPC服务

文章作者:Perkins

发布时间:2020年08月17日

原始链接:https://perkins4j2.github.io/posts/24127/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。