社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
gRPC 一开始由 google 开发,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。在 gRPC 里客户端应用可以像调用本地对象一样直接调用另一台不同的机器上服务端应用的方法,能够更容易地创建分布式应用和服务。与许多 RPC 系统类似,gRPC 也是基于以下理念:定义一个服务,指定其能够被远程调用的方法(包含参数和返回类型)。在服务端实现这个接口,并运行一个 gRPC 服务器来处理客户端调用。在客户端拥有一个存根能够像服务端一样的方法。
首先新建空的maven工程,没有添加m2e插件的看这里
在pom.xml中进行如下配置:
<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>test-grpc</groupId>
<artifactId>test-grpc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Grpc-test</name>
<url>http://maven.apache.org</url>
<properties>
<grpc.version>1.4.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<version>${grpc.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.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.3.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后在main文件夹下新建proto文件夹——>新建test.proto文件,
文件内容如下:
syntax = "proto3";
option java_multiple_files = false;
option java_package = "io.grpc.examples";
option java_outer_classname = "TestProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
service Calculate{
rpc Calculate(CalculateRequest) returns (CalculateReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
message CalculateRequest {
string name = 1;
int64 number = 2;
}
// The response message containing the greetings
message CalculateReply {
string message = 1;
int64 result = 2;
}
java_package生成java代码的package
java_outer_classname 创建的javaBean的文件名
java_multiple_files是否需要多文件,创建java文件的时候如果这个选项是true那么我们的TestInput以及TestOutPut会有两个独立的文件,在一般的工作中一个proto往往会有很多个不同的接口定义,所以在这里我们就只讨论false也就是文件合一的情况。
上述proto定义了两个rpc,一个是GreeterRpc,一个是CalculateRpc,两个rpc分别有自己的request和reply。以Calculate为例,通过Calculate(String name,long number)接收请求,进行处理之后返回Reply,Reply包含一个string和一个long。
写好proto之后,mvn install一下,即可在target中生成Grpc和Proto类。将这两个类复制到我们的包下即可使用。
先贴上服务端全部代码
package io.grpc.examples;
import java.io.IOException;
import java.util.logging.Logger;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.examples.TestProto.CalculateReply;
import io.grpc.examples.TestProto.CalculateRequest;
import io.grpc.stub.StreamObserver;
public class CalculateServer {
private static final Logger logger = Logger.getLogger(CalculateServer.class.getName());
private Server server;
private void start() throws IOException {
/* The port on which the server should run */
int port = 50052;
server = ServerBuilder.forPort(port)
.addService(new CalculateImpl())
.build()
.start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
CalculateServer.this.stop();
System.err.println("*** server shut down");
}
});
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
/**
* Await termination on the main thread since the grpc library uses daemon threads.
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
/**
* Main launches the server from the command line.
*/
public static void main(String[] args) throws IOException, InterruptedException {
final CalculateServer server = new CalculateServer();
server.start();
server.blockUntilShutdown();
}
static class CalculateImpl extends CalculateGrpc.CalculateImplBase {
@Override
public void calculate(CalculateRequest req, StreamObserver<CalculateReply> responseObserver) {
String replyname = "Hello "+req.getName()+"!!";
long number = req.getNumber()+250;
CalculateReply reply = CalculateReply.newBuilder().setMessage(replyname).setResult(number).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
}
CalculateImpl 继承 CalculateGrpc.CalculateImplBase (Grpc生成的类),CalculateImpl 重写calculate方法,在服务端对客户端传来的数据进行自己想要的处理,然后将结果放入到CalculateReply 中,返回给客户端。Server的start()方法用来启动服务端监听,并将CalculateImpl 绑定。
下面是客户端代码:
package io.grpc.examples;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.TestProto.CalculateReply;
import io.grpc.examples.TestProto.CalculateRequest;
public class CalculateClient {
private static final Logger logger = Logger.getLogger(CalculateClient.class.getName());
private final ManagedChannel channel;
private final CalculateGrpc.CalculateBlockingStub blockingStub;
public CalculateClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext(true)
.build());
}
CalculateClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = CalculateGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public void calculate(String name,long number) {
logger.info("Will try to calculate " + name + " ...number is "+number);
CalculateRequest request = CalculateRequest.newBuilder().setName(name).setNumber(number).build();
CalculateReply response;
try {
response = blockingStub.calculate(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage()+"Result is "+response.getResult());
}
public static void main(String[] args) throws Exception {
CalculateClient client = new CalculateClient("localhost", 50052);
try {
/* Access a service running on the local machine on port 50051 */
String user = "zhangsan";
long number = 40;
if (args.length > 0) {
user = args[0]; /* Use the arg as the name to greet if provided */
}
client.calculate(user,number);
} finally {
client.shutdown();
}
}
}
Client方法比较简单,初始化连接,使用客户端的grpc存根,请求传参,获取返回reply。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!