1.添加maven依赖
io.grpc
grpc-netty
1.6.1
io.grpc
grpc-protobuf
1.6.1
io.grpc
grpc-stub
1.6.1
kr.motd.maven
os-maven-plugin
1.5.0.Final
org.xolstice.maven.plugins
protobuf-maven-plugin
0.5.0
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
grpc-java
io.grpc:protoc-gen-grpc-java:1.6.1:exe:${os.detected.classifier}
compile
compile-custom
2.编写proto文件(HelloWorld.proto),并定义一个接口
syntax = "proto3";
option java_package = "com";
package helloworld;
// The greeter 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;
}
3.编译打包
a:执行mvn package 打包
打包如图所示:
b:将 grpc-my-1.0-SNAPSHOT.jar,添加到maven本地厂库
如:
mvn install:install-file -DgroupId=com.my.grpc -DartifactId=hwb -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=target/grpc-my-1.0-SNAPSHOT.jar
c:并在项目中添加此jar包:
com.my.grpc
hwb
1.0-SNAPSHOT
4.继承 GreeterGrpc.GreeterImplBase并实现
创建java类,类名为 HelloServiceGrpc;
import com.GreeterGrpc;
import com.HelloWorld;
import io.grpc.stub.StreamObserver;
public class HelloServiceGrpc extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloWorld.HelloRequest request, StreamObserverresponseObserver) {
String name = request.getName();
System.out.println("您的名字:" + name);
HelloWorld.HelloReply.Builder builder = HelloWorld.HelloReply.newBuilder();
builder.setMessage("小伙子不错的、");
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}
}
5.server服务端(HelloServer)
import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;
public class HelloServer {
private Server server;
public static void main(String[] args) throws IOException, InterruptedException {
final HelloServer server = new HelloServer();
server.start();
server.blockUntilShutdown();
}
private void start() throws IOException { /* The port on which the server should run */
int port = 50051;
//这个部分启动server
server = ServerBuilder.forPort(port)
.addService(new HelloServiceGrpc())
.build().start();
System.out.println("Server started, listening on "+ port);
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
System.err.println("*** shutting down gRPC server since JVM is shutting down");
HelloServer.this.stop();
System.err.println("*** server shut down");
}
});
}
private void stop(){
if (server != null){
server.shutdown();
}
}
// block 一直到退出程序
private void blockUntilShutdown() throws InterruptedException {
if (server != null){
server.awaitTermination();
}
}
}
6.client客户端
import com.GreeterGrpc;
import com.HelloWorld;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
/**
* Created by Hua wb on 2018/10/11.
*/
public class HelloClient {
public static void main(String[] args) {
ManagedChannel localhost = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext(true).build();
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(localhost);
HelloWorld.HelloRequest hwb = HelloWorld.HelloRequest.newBuilder().setName("化伟博").build();
HelloWorld.HelloReply helloReply = stub.sayHello(hwb);
System.out.println(helloReply.getMessage());
}
}