pom.xml
protobuf-maven-plugin里protobuf.version和grpc.version可从maven获取最新版本
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>grpc_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<os.detected.classifier>windows-x86_64</os.detected.classifier>
<grpc.version>1.60.1</grpc.version>
<protobuf.version>3.25.1</protobuf.version>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</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>
<build>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<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>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
步骤
目录结构- src/main/proto目录编写user.proto文件
syntax = "proto3";
message UserRequest {
string name = 1;
string age = 2;
}
message UserResponse {
string birthday = 1;
string city = 2;
}
service UserService {
rpc searchBirthdayAndCity(UserRequest) returns (UserResponse);
}
-
点击maven工具protobuf:compile和protobuf:compile-custom, 编译生成java文件
-
经过步骤2之后, 会在target目录下生成两个java文件, 将这两个文件拷贝到工程目录中
- 编写server和client的代码, 分别启动server和client的main()方法
- server
public static void main(String[] args) throws Exception {
io.grpc.Server server = ServerBuilder.forPort(8088)
.addService(new UserServiceGrpc.UserServiceImplBase() {
@Override
public void searchBirthdayAndCity(User.UserRequest request, StreamObserver<User.UserResponse> responseObserver) {
System.out.printf("receive search request === name:%s,age:%s\n", request.getName(), request.getAge());
User.UserResponse response = User.UserResponse.newBuilder()
.setBirthday(LocalDate.now().toString())
.setCity("beijing").build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}).build();
server.start();
server.awaitTermination();
}
- client
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8088)
.usePlaintext()
.build();
UserServiceGrpc.UserServiceBlockingStub stub = UserServiceGrpc.newBlockingStub(channel);
User.UserRequest request = User.UserRequest.newBuilder()
.setName("jay chou")
.setAge("30")
.build();
User.UserResponse response = stub.searchBirthdayAndCity(request);
System.out.println(response);
channel.shutdown();
}
网友评论