rtifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<!-- mvn protobuf:compile -->
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:3.22.3:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:1.56.0:exe:${os.detected.classifier}
</pluginArtifact>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDirectory>false</clearOutputDirectory>
<skip>false</skip>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
编译生成代码
mvn clean compile
编写业务实现类
public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {
@Override
public void getRealNameByUsername(StudentUsernameRequest request,
StreamObserver<StudentResponse> responseObserver) {
String username = request.getUsername();
System.out.printf("username=%s\n", username);
StudentResponse response = StudentResponse.newBuilder().setRealName("徐国峰").build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
@Override
public void getRealNameByUsernameLike(StudentUsernameRequest request,
StreamObserver<StudentResponse> responseObserver) {
String username = request.getUsername();
System.out.printf("username=%s\n", username);
responseObserver.onNext(StudentResponse.newBuilder().setRealName("徐国峰1").build());
responseObserver.onNext(StudentResponse.newBuilder().setRealName("徐国峰2").build());
responseObserver.onNext(StudentResponse.newBuilder().setRealName("徐国峰3").build());
responseObserver.onNext(StudentResponse.newBuilder().setRealName("徐国峰4").build());
responseObserver.onCompleted();
}
@Override
public StreamObserver<StudentUsernameRequest> getRealNameByUsernames(
StreamObserver<StudentResponseList> responseObserver) {
return new StreamObserver<StudentUsernameRequest>() {
@Override
public void onNext(StudentUsernameRequest request) {
System.out.printf("username=%s\n", request.getUsername());
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
@Override
public void onCompleted() {
StudentResponse response1 = StudentResponse.newBuilder().setRealName("徐国峰5").build();
StudentResponse response2 = StudentResponse.newBuilder().setRealName("徐国峰6").build();
StudentResponse response3 = StudentResponse.newBuilder().setRealName("徐国峰7").b