环境
JDK8 + Maven3.6.3 我的 Grpc-java demo https://github.com/999bug/grpc-java 记得star😊😊
搭建步骤
1、利用代码编译器创建maven 项目
2、添加依赖坐标
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.44.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.44.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.44.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
</dependencies>
3、添加构建grpc proto插件
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.19.2:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.44.0:exe:${os.detected.classifier}</pluginArtifact>
<protoSourceRoot>src/main/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意 <protoSourceRoot>src/main/proto</protoSourceRoot> protoSourceRoot 设置proto文件的位置,不然插件不能正常编译
4、在protoSourceRoot 设置的文件夹下面创建helloWord.proto 文件,后缀必须为.proto,文件名无所谓
syntax = "proto3";
option java_multiple_files = true;
//指定该proto文件编译成的java源文件的包名
option java_package = "com.ncst.grpc.protobuf";
// 表示下面的message编译成的java类文件的名字
option java_outer_classname = "GrpcHelloProto";
package Hello;
// 使用的时候此类是所有使用类的前缀,定义rpc方法
如HelloGrpc、HelloBlockingStub、HelloResponse、HelloRequest
service Hello {
rpc SayHello (HelloRequest) returns (HelloResponse) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloResponse {
string message = 1;
}
5、利用 Maven 插件,构建grpc 源代码
- 首先运行compile
- 然后接着运行compile-custom
6、构建完成会发现在 target 包中生成如下代码
7、将protobuf 包移入项目中
编写server 端代码
1、远程调用方法实现类 HelloServiceImpl
package client.service;
import client.grpc.HelloGrpc;
import client.grpc.protobuf.HelloRequest;
import client.grpc.protobuf.HelloResponse;
import io.grpc.stub.StreamObserver;
public class HelloServiceImpl extends HelloGrpc.HelloImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
HelloResponse helloResponse = HelloResponse.newBuilder()
.setMessage("我是业主晓丹,我不喜欢保安,我希望每天都说 宝 早安,而不是早 保安!")
.build();
responseObserver.onNext(helloResponse);
responseObserver.onCompleted();
System.out.println("message from Grpc-client:" + request.getName());
}
}
2、ServerService
package client.service;
import io.grpc.ServerBuilder;
import java.io.IOException;
public class ServerService {
private static final int port = 10086;
private static io.grpc.Server server;
public static void start() throws IOException, InterruptedException {
server = ServerBuilder.forPort(port)
.addService(new HelloServiceImpl())
.maxInboundMessageSize(Integer.MAX_VALUE)
.build()
.start();
System.out.println("server start port: " + port);
server.awaitTermination();
}
}
3、Server 端启动类
package client.stater;
import client.service.ServerService;
import java.io.IOException;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
ServerService.start();
}
}
Client 端
1、BaseService
package client.service;
import client.grpc.HelloGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;
public abstract class BaseService {
protected final ManagedChannel channel;
protected final HelloGrpc.HelloBlockingStub blockingStub;
public BaseService(String host, int port) {
channel = ManagedChannelBuilder
.forAddress(host, port)
.usePlaintext()
.maxInboundMessageSize(Integer.MAX_VALUE)
.build();
blockingStub = HelloGrpc.newBlockingStub(channel);
}
public void shutdown() {
try {
channel.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public abstract void sayHello(String str);
}
2、BaseServiceImpl
package client.service;
import client.grpc.protobuf.HelloRequest;
import client.grpc.protobuf.HelloResponse;
public class BaseServiceImpl extends BaseService {
public BaseServiceImpl(String host, int port) {
super(host, port);
}
@Override
public void sayHello(String str) {
HelloRequest request = HelloRequest.newBuilder()
.setName(str)
.build();
HelloResponse response;
response = blockingStub.sayHello(request);
System.out.println("message from Grpc-server: " + response.getMessage());
System.out.println();
}
}
3、Client 启动类
package client.stater;
import client.service.BaseService;
import client.service.BaseServiceImpl;
public class Client {
public static void main(String[] args) {
BaseService client = new BaseServiceImpl("127.0.0.1", 10086);
client.sayHello("我是一名保安,保卫一方平安,爱吃小熊饼干,喜欢业主晓丹");
client.shutdown();
}
}
调用结果
Server 端
Client 端
|