1.在pom中引入java
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-protobuf -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-stub -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.18.0</version>
</dependency>
2.maven配置golang
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId><!--引入操做系統os設置的屬性插件,不然${os.detected.classifier} 操做系統版本會找不到 -->
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<!--添加編譯proto文件的編譯程序和對應的編譯插件-->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.編寫IDL文件, 由於要誇語言調用, 因此和golang的使用同一個文件,除了option以外, 全部的東西不要改!!!maven
syntax = "proto3";
option java_multiple_files = true;
option java_package = "cn.com.xu.grpc";
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;
}
4.生成文件
ide
5.客戶端代碼ui
public class GrpcClient {
private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
public GrpcClient(String host,int port){
channel = ManagedChannelBuilder.forAddress(host,port)
.usePlaintext(true)
.build();
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public void greet(String name){
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response = blockingStub.sayHello(request);
System.out.println("this is java client, response..." + response.getMessage());
}
public static void main(String[] args) throws InterruptedException {
GrpcClient client = new GrpcClient("localhost",50001);
client.greet("this is java client");
}
}
6.服務端代碼this
public class GrpcServer { private int port = 50001; private Server server; private void start() throws IOException { server = ServerBuilder.forPort(port) .addService(new GreeterImpl()) .build() .start(); System.out.println("service start..."); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.err.println("*** shutting down gRPC server since JVM is shutting down"); GrpcServer.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(); } } public static void main(String[] args) throws IOException, InterruptedException { final GrpcServer server = new GrpcServer(); server.start(); server.blockUntilShutdown(); } // 實現 定義一個實現服務接口的類 private class GreeterImpl extends GreeterGrpc.GreeterImplBase { public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { System.out.println("this is java service, request..."+req.getName()); HelloReply reply = HelloReply.newBuilder().setMessage(("this is java service")).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } }}