依赖
<!-- grpc相关依赖 -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-okhttp</artifactId>
<version>1.23.0</version>
</dependency>
插件
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<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>
设计proto
在
src/main
目录(java
同级目录)下新建proto
目录.
在proto
目录下新建文件hello.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.java.grpc";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "GRPC";
package common;
service HelloService {
rpc SayHello (Hello) returns (Response) {
}
}
message Hello {
string name = 1;
}
message Response {
string content = 1;
}
生成Java文件
mvn protobuf:compile
mvn protobuf:compile-custom
编写服务端
server:
grpc:
port: 9090
@Service
public class GrpcServer {
// 配置文件中定义端口号
@Value("${server.grpc.port}")
Integer port;
private Server server;
@PostConstruct
protected void start() throws IOException {
server = ServerBuilder.forPort(port)
.addService(new HelloServiceImpl())
.build()
.start();
log.info("Grpc Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.err.println("shutting down gRPC java server ...");
GrpcServer.this.stop();
}));
}
private void stop() {
if (server != null) {
server.shutdown();
}
}
protected void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
private class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
@Override
public void sayHello(Hello req, StreamObserver<Response> responseObserver) {
Response reply = Response.newBuilder().setContent(("Response Message from Java gRPC-Server: Hello " + req.getName())).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
}
编写客户端
@Slf4j
public class GrpcClient {
private ManagedChannel channel;
private HelloServiceGrpc.HelloServiceBlockingStub blockingStub;
public GrpcClient(String host, int port) {
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext(true)
.build();
blockingStub = HelloServiceGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public String sayHello(String name) {
Hello request = Hello.newBuilder().setName(name).build();
Response response;
try {
response = blockingStub.sayHello(request);
return response.getContent();
} catch (StatusRuntimeException e) {
log.warn("RPC failed: {}", e.getStatus());
return "RPC failed";
}
}
}