一、pom.xml
<?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>cn.edu.tju</groupId>
<artifactId>grpcfullstream</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<grpc-version>1.54.0</grpc-version>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>${grpc-version}</version>
</dependency>
<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>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.22.2</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</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.21.7:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.0: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目录编写.proto文件 student.proto
syntax = "proto3";
option java_package = "cn.edu.tju.test";
option java_multiple_files=true;
package cn.edu.tju.test;
message StudentInfo {
string name =1;
int32 age =2;
}
message UppercaseStudentInfo {
string name =1;
int32 age =2;
}
service UppercaseService {
rpc getInfo(stream StudentInfo) returns (stream UppercaseStudentInfo){};
}
三、mvn clean install生成java代码,并拷贝到src相应的package下
四、编写服务实现
package cn.edu.tju.test;
import io.grpc.stub.StreamObserver;
import java.util.LinkedList;
import java.util.List;
public class UppercaseServiceImpl extends UppercaseServiceGrpc.UppercaseServiceImplBase {
public StreamObserver<StudentInfo> getInfo(
StreamObserver<UppercaseStudentInfo> responseObserver) {
return new StreamObserver<StudentInfo>() {
List<StudentInfo> studentInfoList = new LinkedList<>();
@Override
public void onNext(StudentInfo studentInfo) {
studentInfoList.add(studentInfo);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
studentInfoList.forEach(studentInfo -> {
String name = studentInfo.getName().toUpperCase();
int age =studentInfo.getAge();
UppercaseStudentInfo uppercaseStudentInfo = UppercaseStudentInfo.newBuilder().setName(name).setAge(age).build();
responseObserver.onNext(uppercaseStudentInfo);
});
responseObserver.onCompleted();
}
};
}
}
五、编写服务器
package cn.edu.tju.test;
import io.grpc.Server;
import io.grpc.ServerBuilder;
public class MyServer {
public static void main(String[] args) throws Exception {
Server s = ServerBuilder.forPort(50065)
.addService(new UppercaseServiceImpl())
.build();
s.start();
System.out.println("server started......");
s.awaitTermination();
}
}
六、编写客户端
package cn.edu.tju.test;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class MyClient {
final static CountDownLatch finishLatch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
ManagedChannel managedChannel = ManagedChannelBuilder.forTarget("localhost:50065").usePlaintext()
.build();
UppercaseServiceGrpc.UppercaseServiceStub stub = UppercaseServiceGrpc.newStub(managedChannel);
StreamObserver<UppercaseStudentInfo> streamObserver = new StreamObserver<UppercaseStudentInfo>() {
@Override
public void onNext(UppercaseStudentInfo uppercaseStudentInfo) {
System.out.println(uppercaseStudentInfo.getName()+":"+uppercaseStudentInfo.getAge());
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
System.out.println("finished......");
finishLatch.countDown();
}
};
StreamObserver<StudentInfo> requestObserver = stub.getInfo(streamObserver);
try {
for (int i = 0; i < 10; ++i) {
StudentInfo req = StudentInfo.newBuilder().setName("amadeus"+(20+i)).setAge(20+i).build();
requestObserver.onNext(req);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Mark the end of requests
requestObserver.onCompleted();
finishLatch.await(10, TimeUnit.MINUTES);
}
}
七、运行服务器和客户端