本文参照官网文档https://grpc.io/docs/tutorials/basic/java/,主要介绍gRPC的demo搭建。
我从https://github.com/grpc/grpc-java.git拷贝了grpc-java项目到本地,参考了examples文件夹下的代码,由于这个项目是gradle构建,不太熟悉gradle的我新建了一个项目并将examples下的部分代码复制到了新的项目中。
首先将examples文件夹下的pom.xml的dependencies和build拷贝到新项目中:
<properties>
<grpc.version>1.25.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>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.2.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>
然后再新项目src/main目录下新建一个proto目录,拷贝examples/src/main/proto文件夹下的helloworld.proto到新建的proto目录下,在项目中建一个helloword包,将helloworld.proto中的java_package改成自己项目中helloworld包的路径,然后执行mvn install命令。执行完成后,target目录下就会生成gRPC代码。
这个时候将examples/src/main/java/io/grpc/examples/helloworld中的类拷贝到新项目的helloworld包下。重新导入一下类,然后依次启动HelloworldServer和HelloworldClient,HelloworldClient运行 如下:
这个例子就是HelloworldClient发送一个name('world')给HelloworldServer,HelloworldServer返回给HelloworldClient消息'Hello' + name。这样,官网的Helloworld demo就运行成功了。
下面尝试仿照这个Helloworld自己写一个demo。
首先在proto目录下新建一个student.proto文件:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "zhangc.grpc.student";
option java_outer_classname = "StudentProto";
option objc_class_prefix = "STU";
service StudentOperation{
rpc Get(StudentRequest) returns (stream Student) {}
}
//定义StudentRequest类型,里面包含一个String类型的msg字段
//后面的数字是标识号 不必连续,1-15占1字节 16-2047占2字节 通常将1-15保留给常用字段
message StudentRequest{
string msg = 1;
}
message Student{
int32 id = 1;
string name = 2;
}
然后执行mvn install生成gRPC代买,编写StudentServer服务端:
package zhangc.grpc.student;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;
/**
* @author zhangc
* @version 1.0
* @date 2019/11/28
*/
public class StudentServer {
private final int port;
private final Server server;
public StudentServer(int port) throws IOException {
this.port = port;
this.server = ServerBuilder.forPort(port)
.addService(new StudentService())
.build()
.start();
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");
StudentServer.this.stop();
System.err.println("*** server shut down");
}
});
}
public 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();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
StudentServer studentServer = new StudentServer(1214);
studentServer.blockUntilShutdown();
}
}
StudentClient客户端:
package zhangc.grpc.student;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* @author zhangc
* @version 1.0
* @date 2019/11/28
*/
public class StudentClient {
private static final Logger logger = Logger.getLogger(StudentClient.class.getName());
private final ManagedChannel channel;
private final StudentOperationGrpc.StudentOperationBlockingStub blockingStub;
public StudentClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
}
public StudentClient(ManagedChannelBuilder<?> builder) {
channel = builder.build();
blockingStub = StudentOperationGrpc.newBlockingStub(channel);
}
public void get(String msg){
StudentRequest request = StudentRequest.newBuilder().setMsg(msg).build();
Student student = blockingStub.get(request);
logger.info("id:" + student.getId());
logger.info("name:" + student.getName());
}
public static void main(String[] args) throws Exception {
StudentClient client = new StudentClient("localhost", 1214);
try {
/* Access a service running on the local machine on port 50051 */
String msg = "helloworld";
if (args.length > 0) {
msg = args[0]; /* Use the arg as the name to greet if provided */
}
client.get(msg);
} finally {
client.shutdown();
}
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
}
客户端发送一个msg给服务端,然后服务端将msg打印并响应一个Student对象给客户端,然后客户端将这个对象打印,运行结果如下:
server:
client: