Grpc入门

释放双眼,带上耳机,听听看~!

一、简介

gRPC 是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支持.

gRPC 基于 HTTP/2 标准设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

二、Grpc是什么

在 gRPC 里客户端应用可以像调用本地对象一样直接调用另一台不同的机器上服务端应用的方法,使得您能够更容易地创建分布式应用和服务。与许多 RPC 系统类似,gRPC 也是基于以下理念:定义一个服务,指定其能够被远程调用的方法(包含参数和返回类型)。在服务端实现这个接口,并运行一个 gRPC 服务器来处理客户端调用。在客户端拥有一个存根能够像服务端一样的方法。
Grpc入门

使用protocol buffers

gRPC 默认使用 protocol buffers,这是 Google 开源的一套成熟的结构数据序列化机制(当然也可以使用其他数据格式如 JSON)。正如你将在下方例子里所看到的,你用 proto files 创建 gRPC 服务,用 protocol buffers 消息类型来定义方法参数和返回类型。

三、安装Grpc工具

使用grpc需要针对不同的语言安装相应的工具,进行编译。这里我使用的是Java语言,采用maven插件进行处理,这里就不再多说了。

四、Java使用Grpc入门开发

(一)官网例子

我们可以去grpc-java下载官网例子,里面有个examples,这里面存放了实例代码。

(二)创建maven的Java项目,引入依赖


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
1 <properties>
2    <grpc.version>1.13.1</grpc.version>
3    <protobuf.version>3.5.1</protobuf.version>
4    <protoc.version>3.5.1-1</protoc.version>
5  </properties>
6
7  <dependencies>
8    <dependency>
9      <groupId>io.grpc</groupId>
10      <artifactId>grpc-netty</artifactId>
11      <version>${grpc.version}</version>
12    </dependency>
13    <dependency>
14      <groupId>io.grpc</groupId>
15      <artifactId>grpc-protobuf</artifactId>
16      <version>${grpc.version}</version>
17    </dependency>
18    <dependency>
19      <groupId>io.grpc</groupId>
20      <artifactId>grpc-stub</artifactId>
21      <version>${grpc.version}</version>
22    </dependency>
23    <dependency>
24      <groupId>com.google.protobuf</groupId>
25      <artifactId>protobuf-java-util</artifactId>
26      <version>${protobuf.version}</version>
27    </dependency>
28  </dependencies>
29
30

(三)引入编译proto的插件


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
1<build>
2    <extensions>
3      <extension>
4        <groupId>kr.motd.maven</groupId>
5        <artifactId>os-maven-plugin</artifactId>
6        <version>1.5.0.Final</version>
7      </extension>
8    </extensions>
9    <plugins>
10      <plugin>
11        <groupId>org.xolstice.maven.plugins</groupId>
12        <artifactId>protobuf-maven-plugin</artifactId>
13        <version>0.5.1</version>
14        <configuration>
15          <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
16          <pluginId>grpc-java</pluginId>
17          <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
18        </configuration>
19        <executions>
20          <execution>
21            <goals>
22              <goal>compile</goal>
23              <goal>compile-custom</goal>
24            </goals>
25          </execution>
26        </executions>
27      </plugin>
28      <plugin>
29        <groupId>org.apache.maven.plugins</groupId>
30        <artifactId>maven-enforcer-plugin</artifactId>
31        <version>1.4.1</version>
32        <executions>
33          <execution>
34            <id>enforce</id>
35            <goals>
36              <goal>enforce</goal>
37            </goals>
38            <configuration>
39              <rules>
40                <requireUpperBoundDeps/>
41              </rules>
42            </configuration>
43          </execution>
44        </executions>
45      </plugin>
46    </plugins>
47  </build>
48
49

(四)定义服务

在src/main下面创建proto文件夹,在里面编写proto文件定义服务,文件编写方式参考例子中proto文件定义方式,这里定义的proto文件如下:
helloworld.proto(其实也是例子中的)


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
36
37
38
1// Copyright 2015 The gRPC Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14syntax = "proto3";
15
16option java_multiple_files = true;
17option java_package = "io.grpc.examples.helloworld";
18option java_outer_classname = "HelloWorldProto";
19option objc_class_prefix = "HLW";
20
21package helloworld;
22
23// The greeting service definition.
24service Greeter {
25  //简单的RPC调用
26  rpc simpleRpc (HelloRequest) returns (HelloReply) {};
27}
28
29// The request message containing the user's name.
30message HelloRequest {
31  string name = 1;
32}
33// The response message containing the greetings
34message HelloReply {
35  string message = 1;
36}
37
38

(五)通过插件生成proto文件

Grpc入门
执行之后会在target/generated-sources/protobuf下面生成对应的文件:
Grpc入门
其中java里面的主要是一些Java实体类和构造这些实体类的Builder,而grpc里面z只有一个类GreeterGrpc,这个类里面有很多的内部类,我们需要关注的是下面这些:

  • GreeterImplBase:服务接口的抽象实现类,需要服务端去实现
  • GreeterBlockingStub:同步的服务调用存根,用于客户端去调用服务使用
  • GreeterStub:异步的服务调用存根,用于客户端去调用服务使用

接下来,我们可以分别编写客户端和服务端的代码。在实际开发中,我们应该把上面的(二)、(三)、(四)、(五)这些步骤在服务端项目和客户端项目都执行。注意,这个proto文件直接编写一份然后拷贝即可。

(六)服务端编写接口的实现类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1package com.firewolf.grpc;
2
3import io.grpc.examples.helloworld.GreeterGrpc;
4import io.grpc.examples.helloworld.HelloReply;
5import io.grpc.examples.helloworld.HelloRequest;
6import io.grpc.stub.StreamObserver;
7
8/**
9 * 作者:刘兴 时间:2019/5/9
10 **/
11public class GreeterServiceImpl extends GreeterGrpc.GreeterImplBase {
12
13  @Override
14  public void simpleRpc(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
15    System.out.println(request.getName());
16    HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
17    responseObserver.onNext(reply); //回写响应
18    responseObserver.onCompleted(); //触发回写操作
19  }
20}
21
22

(七)编写服务启动类

通过编写服务启动类用来启动服务,同时把自己的服务实现添加进去


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
1/*
2 * Copyright 2015 The gRPC Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.firewolf.grpc;
18
19import io.grpc.Server;
20import io.grpc.ServerBuilder;
21import io.grpc.examples.helloworld.GreeterGrpc;
22import io.grpc.examples.helloworld.HelloReply;
23import io.grpc.examples.helloworld.HelloRequest;
24import io.grpc.stub.StreamObserver;
25import java.io.IOException;
26import java.util.jar.JarEntry;
27import java.util.logging.Logger;
28import javax.sound.midi.Soundbank;
29
30/**
31 * Server that manages startup/shutdown of a {@code Greeter} server.
32 */
33public class HelloWorldServer {
34
35  private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());
36  private Server server;
37
38  private void start() throws IOException {
39    int port = 50051;
40    server = ServerBuilder.forPort(port)
41        .addService(new GreeterServiceImpl()) //添加服务实现
42        .build()
43        .start();
44    logger.info("Server started, listening on " + port);
45    Runtime.getRuntime().addShutdownHook(new Thread() {
46      @Override
47      public void run() {
48        System.err.println("*** shutting down gRPC server since JVM is shutting down");
49        HelloWorldServer.this.stop();
50        System.err.println("*** server shut down");
51      }
52    });
53  }
54
55  private void stop() {
56    if (server != null) {
57      server.shutdown();
58    }
59  }
60
61  /**
62   * Await termination on the main thread since the grpc library uses daemon threads.
63   */
64  private void blockUntilShutdown() throws InterruptedException {
65    if (server != null) {
66      server.awaitTermination();
67    }
68  }
69
70  /**
71   * Main launches the server from the command line.
72   */
73  public static void main(String[] args) throws IOException, InterruptedException {
74    final HelloWorldServer server = new HelloWorldServer();
75    server.start();
76    server.blockUntilShutdown();
77  }
78}
79
80

(八)客户端连接服务并调用服务


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
1/*
2 * Copyright 2015 The gRPC Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.firewolf.grpc;
18
19import io.grpc.ManagedChannel;
20import io.grpc.ManagedChannelBuilder;
21import io.grpc.StatusRuntimeException;
22import io.grpc.examples.helloworld.GreeterGrpc;
23import io.grpc.examples.helloworld.GreeterGrpc.GreeterStub;
24import io.grpc.examples.helloworld.HelloReply;
25import io.grpc.examples.helloworld.HelloRequest;
26import io.grpc.stub.StreamObserver;
27import java.util.Collections;
28import java.util.Iterator;
29import java.util.List;
30import java.util.concurrent.TimeUnit;
31import java.util.logging.Level;
32import java.util.logging.Logger;
33import javax.sound.midi.Soundbank;
34
35/**
36 * A simple client that requests a greeting from the {@link HelloWorldServer}.
37 */
38public class HelloWorldClient {
39  private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());
40
41  private final ManagedChannel channel;
42  private final GreeterGrpc.GreeterBlockingStub blockingStub;
43
44  private  final GreeterGrpc.GreeterStub asyncStub;
45
46  public HelloWorldClient(String host, int port) {
47    this(ManagedChannelBuilder.forAddress(host, port)
48        .usePlaintext()
49        .build());
50  }
51
52  HelloWorldClient(ManagedChannel channel) {
53    this.channel = channel;
54    blockingStub = GreeterGrpc.newBlockingStub(channel);
55    asyncStub = GreeterGrpc.newStub(channel);
56  }
57
58  public void shutdown() throws InterruptedException {
59    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
60  }
61
62  //简单的RPC调用
63  public void greet(String name) {
64    logger.info("Will try to greet " + name + " ...");
65    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
66    HelloReply response;
67    try {
68      response = blockingStub.simpleRpc(request);
69    } catch (StatusRuntimeException e) {
70      logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
71      return;
72    }
73    logger.info("Greeting: " + response.getMessage());
74  }
75
76  public static void main(String[] args) throws Exception {
77    HelloWorldClient client = new HelloWorldClient("localhost", 50051);
78    try {
79      String user = "world";
80      if (args.length > 0) {
81        user = args[0];
82      }
83     client.greet(user);
84    } finally {
85      client.shutdown();
86    }
87  }
88}
89
90
91

(九)测试

分别启动服务端和客户端,分别打印如下效果:
Grpc入门
Grpc入门
可以看到,服务已经调用成功

五、Grpc交互方式

(一)4中交互方式概述

Grpc允许定义4中类型的service方法,这些都定义在proto文件的接口Greeter里面:

  • 简单的RPC。客户端使用存根发送请求到服务器并等待响应返回,就像平常的函数调用一样,如:


1
2
3
1 rpc simpleRpc (HelloRequest) returns (HelloReply) {};
2
3
  • 服务器端流式 RPC 。客户端发送请求到服务器,拿到一个流去读取返回的消息序列。 客户端读取返回的流,直到里面没有任何消息。从例子中可以看出,通过在响应类型前插入 stream 关键字,可以指定一个服务器端的流方法,如:


1
2
3
1rpc serverStream (HelloRequest) returns (stream HelloReply) {};
2
3
  • 客户端流式 RPC 。 客户端写入一个消息序列并将其发送到服务器,同样也是使用流。一旦 客户端完成写入消息,它等待服务器完成读取返回它的响应。通过在 请求 类型前指定 stream 关键字来指定一个客户端的流方法,如:


1
2
3
1rpc clientStream (stream HelloRequest) returns (HelloReply) {};
2
3
  • 双向流式 RPC 是双方使用读写流去发送一个消息序列。两个流独立操作,因此客户端和服务器 可以以任意喜欢的顺序读写:比如, 服务器可以在写入响应前等待接收所有的客户端消息,或者可以交替 的读取和写入消息,或者其他读写的组合。 每个流中的消息顺序被预留。你可以通过在请求和响应前加 stream 关键字去制定方法的类型,如:


1
2
3
1rpc bothStream (stream HelloRequest) returns (stream HelloReply){};
2
3

其中第一种方式在入门案例中已经使用了,后面主要讲述另外三种方式。

为了方便后面讲解,这里把统一的工作先做了:将上述修改后的proto文件重新进行编译,生成相关文件(不会的参看前面的步骤)。

(二)服务端流式RPC

1.服务端接口实现代码


1
2
3
4
5
6
7
8
9
10
11
12
1  @Override
2  public void serverStream(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
3
4    System.out.println(request.getName());
5    for (int i = 0; i < 10; i++) {
6      HelloReply reply = HelloReply.newBuilder().setMessage("Hello," + i).build();
7      responseObserver.onNext(reply);
8    }
9    responseObserver.onCompleted();
10  }
11
12

这个代码比较简单,这里有一个参数为服务端端响应观察者StreamObserver<HelloReply>。就是接受到客户端的请求之后,我们可以通过这个观察者使用onNext()方法流式写回一些数据,之后通过onCompleted()方法告诉grpc

2.客户端服务调用


1
2
3
4
5
6
7
8
9
10
11
12
1  public void getList(String name) {
2    //同步
3    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
4    Iterator&lt;HelloReply&gt; helloReplies = blockingStub.serverStream(request);
5    while (helloReplies.hasNext()) {
6      HelloReply reply = helloReplies.next();
7      System.out.println(reply.getMessage());
8    }
9
10  }
11
12

上面是使用的同步阻塞方式调用的,也可以使用异步方式调用,如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1    asyncStub.serverStream(request, new StreamObserver&lt;HelloReply&gt;() {
2      public void onNext(HelloReply helloReply) {
3        System.out.println(helloReply.getMessage());
4      }
5
6      public void onError(Throwable throwable) {
7        throwable.printStackTrace();
8      }
9
10      public void onCompleted() {
11
12      }
13    });
14
15

(三)客户端流式

这种模式下,客户端通过流式写数据到服务端,服务端处理完之后,再回写数据到客户端,客户端发送数据后,异步等待服务端返回的数据,进行处理

1. 服务端接口实现


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1@Override
2  public StreamObserver&lt;HelloRequest&gt; clientStream(final StreamObserver&lt;HelloReply&gt; responseObserver) {
3
4    return new StreamObserver&lt;HelloRequest&gt;() {
5      public void onNext(HelloRequest helloRequest) {
6        //读取客户端发送过来的信息
7        System.out.println(helloRequest.getName());
8      }
9
10      public void onError(Throwable throwable) {
11
12      }
13
14      public void onCompleted() {
15        //客户端写完之后(上面的onNext函数全部执行完毕之后)会触发这个函数,这个时候回写信息,客户端会接受到这个信息
16        responseObserver.onNext(HelloReply.newBuilder().setMessage(&quot;处理完毕&quot;).build());
17        responseObserver.onCompleted();
18      }
19    };
20
21

2. 客户端服务调用


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
36
1public void sendList() {
2
3    //使用SettableFuture控制程序,在获取完服务端返回的数据之前,阻塞客户端,否则应为是异步线程,看不到效果
4    // 实际开发中不需要
5    final SettableFuture&lt;Void&gt; finishFuture = SettableFuture.create();
6    StreamObserver&lt;HelloReply&gt; responseStreamObserver = new StreamObserver&lt;HelloReply&gt;() {
7      public void onNext(HelloReply helloReply) {
8        System.out.println(helloReply.getMessage());
9      }
10
11      public void onError(Throwable throwable) {
12        finishFuture.setException(throwable);
13      }
14
15      public void onCompleted() {
16        finishFuture.set(null);
17      }
18    };
19
20    StreamObserver&lt;HelloRequest&gt; requestStreamObserver = asyncStub.clientStream(responseStreamObserver);
21    for (int i = 0; i &lt; 10; i++) {
22      requestStreamObserver.onNext(HelloRequest.newBuilder().setName(&quot;脏三&quot; + i).build());
23    }
24    requestStreamObserver.onCompleted();
25    try {
26      finishFuture.get();
27      System.out.println(&quot;获取返回完成&quot;);
28    } catch (InterruptedException e) {
29      e.printStackTrace();
30    } catch (ExecutionException e) {
31      e.printStackTrace();
32    }
33
34  }
35
36

(四)双向流式RPC

和我们的客户端流的例子一样,我们拿到和返回一个 StreamObserver 应答观察者,除了这次我们在客户端仍然写入消息到 它们的 消息流时通过我们方法的应答观察者返回值。这里读写的语法和客户端流以及服务器流方法一样。虽然每一端都会按照它们写入的顺序拿到另一端的消息,客户端和服务器都可以任意顺序读写——流的操作是互不依赖的。

1. 服务端接口实现


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1@Override
2  public StreamObserver&lt;HelloRequest&gt; bothStream(final StreamObserver&lt;HelloReply&gt; responseObserver) {
3
4    return new StreamObserver&lt;HelloRequest&gt;() {
5
6      public void onNext(HelloRequest helloRequest) {
7        System.out.println(helloRequest.getName());
8
9        HelloReply helloReply = HelloReply.newBuilder().setMessage(&quot;消息回执了&quot;).build();
10
11        responseObserver.onNext(helloReply);
12
13      }
14
15      public void onError(Throwable throwable) {
16      }
17      public void onCompleted() {
18        responseObserver.onCompleted();
19      }
20    };
21  }
22
23

2. 客户端代码


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
36
37
38
39
40
1public void bothStreamTest() {
2
3    //使用SettableFuture控制程序,在获取完服务端返回的数据之前,阻塞客户端,否则应为是异步线程,看不到效果
4    // 实际开发中不需要
5    final SettableFuture&lt;Integer&gt; settableFuture = SettableFuture.create();
6
7    //读取服务端响应回来的数据
8    StreamObserver&lt;HelloReply&gt; responseStreamObserver = new StreamObserver&lt;HelloReply&gt;() {
9      public void onNext(HelloReply helloReply) {
10        System.out.println(helloReply.getMessage());
11      }
12
13      public void onError(Throwable throwable) {
14        settableFuture.setException(throwable);
15      }
16
17      public void onCompleted() {
18        settableFuture.set(1); //读取完成,设置读取完成标志
19      }
20    };
21
22    //用于客户端往服务端写数据
23    StreamObserver&lt;HelloRequest&gt; requestStreamObserver = asyncStub.bothStream(responseStreamObserver);
24    for (int i = 0; i &lt; 10; i++) {
25      HelloRequest helloRequest = HelloRequest.newBuilder().setName(&quot;zhangsan&quot; + i).build();
26      requestStreamObserver.onNext(helloRequest);
27    }
28    //写完数据后设置标志
29    requestStreamObserver.onCompleted();
30    try {
31      settableFuture.get();
32      System.out.println(&quot;整个服务请求结束&quot;);
33    } catch (InterruptedException e) {
34      e.printStackTrace();
35    } catch (ExecutionException e) {
36      e.printStackTrace();
37    }
38  }
39
40

到此,GRPC的使用基本讲解完毕

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Google AdSense 全面解析(申请+操作+作弊+忠告)

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索