參考官方github代碼例子:https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/helloworld
一祭隔、idea安裝protobuf插件
二樟蠕、添加protobuf和grpc的pom依賴包和插件
依賴包
<properties>
<protobuf.version>3.6.1</protobuf.version>
<grpc.version>1.19.0</grpc.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<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>
</dependencies>
pom添加protobuf插件
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<!--protobuf插件-->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<!--
The version of protoc must match protobuf-java. If you don't depend on
protobuf-java directly, you will be transitively depending on the
protobuf-java version that grpc depends on.
-->
<protocArtifact>
com.google.protobuf:protoc:${protobuf.version}: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>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
三俏讹、編寫Proto文件
注:編寫proto文件彩掐,文件的格式可以參考Protobuf語言指南——.proto文件語法詳解里面講的很詳細 https://blog.csdn.net/u014308482/article/details/52958148
.proto文件內(nèi)容如下
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
四柔昼、生成gRpc java代碼
第一種方式:idea中執(zhí)行如下
compile:用于生成protobuf類
compile-custom:生成rpc調(diào)用類垂券,其中會用到compile過程中生成的protobuf類
第二種方式:
linux下執(zhí)行命令:
mvn protobuf:compile
mvn protobuf:compile-custom //生成rpc調(diào)用類
在64位window環(huán)境下使用的命令:
mvn protobuf:compile -Dos.detected.name=windows -Dos.detected.arch=x86_64 -Dos.detected.classifier=windows-x86_64
mvn protobuf:compile-custom -Dos.detected.name=windows -Dos.detected.arch=x86_64 -Dos.detected.classifier=windows-x86_64
其中需要傳入的三個參數(shù):
os.detected.name
os.detected.arch
os.detected.classifier
最終生成如下文件,其中GreeterGrpc是一個很重要的類执虹,直接關(guān)系到我們的服務拓挥,我們自己提供的服務需要繼承它的一個內(nèi)部類,在客戶端中則是可以從中得到一個stub用于調(diào)用(例如:GreeterGrpc.GreeterBlockingStub)袋励。
五侥啤、編寫服務端-客戶端代碼
服務端代碼主要兩件事:
第一件,實現(xiàn)提供的業(yè)務服務茬故,這是本職工作盖灸。
要實現(xiàn)提供的服務,只需要寫一個類繼承GreeterGrpc.GreeterImplBase這個類即可磺芭,然后因為我們定義的方法是SayHello赁炎,那么我們也實現(xiàn)這個方法,值得一提的是钾腺,需要注意一下這個方法的參數(shù)是固定的徙垫,不能隨心所欲地寫讥裤。
第二件,啟動一個grpc服務器姻报,用于接收客戶端的請求己英。
其中主要是start方法用于啟動服務器并接收客戶端的請求。在server中添加名稱解析服務服務實在構(gòu)造方法中進行的逗抑。另外剧辐,blockUntilShutdown方法則會讓server阻塞到程序退出為止。
代碼如下:
private Server server;
private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
server = ServerBuilder.forPort(port)
.addService(new GreeterImpl())
.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");
try {
HelloWorldServer.this.stop();
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
}
});
}
private void stop() throws InterruptedException {
if (server != null) {
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
}
}
/**
* 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 HelloWorldServer server = new HelloWorldServer();
server.start();
server.blockUntilShutdown();
}
客戶端代碼參考樣例邮府。