sách gpt4 ai đã đi

gRPC 实战之——客户端为 Request,服务端为 Response

In lại 作者:知者 更新时间:2024-03-12 23:31:52 25 4
mua khóa gpt4 Nike

一 编写 proto 文件

syntax = "proto3"; package grpc.proto; option java_package = "grpc.proto"; option java_outer_classname = "StudentData"; option java_multiple_files = true ; // 定义接口 service StudentService { // 请求一个 Requset 对象,响应一个 Response 对象 rpc queryStudentNameById(MyRequestId) returns(MyResponseName) {} // 请求一个 Requset 对象,响应一个 Stream 对象 rpc queryStudentsByCourseName(MyRequestCourseName) returns(stream MyResponseStudentsStream) {} // 请求一个 Stream 对象,响应一个 Response 对象 rpc queryStudentsByCourseName2(stream MyRequestCourseName) returns(MyResponseStudents) {} // 请求一个 Stream,响应一个 Stream 对象 rpc queryStudentNameById2(stream MyRequestId) returns(stream MyResponseName) {} } message MyRequestId { int32 id = 1 ; } message MyResponseName { string name = 1 ; } message MyStudent { int32 id = 1 ; string name = 2; string courseName = 3 ; } message MyResponseStudents { // 服务端的响应结果是集合类型,因此需要加上 repeated repeated MyStudent students = 1 ; } // 数据结构,定义请求的 Request 对象 message MyRequestCourseName { string courseName = 1 ; } // 数据结构,定义响应的 Stream message MyResponseStudentsStream { int32 id = 1 ; string name = 2; string courseName = 3 ; }

二 编写接口实现类

package grpc; import grpc.proto.*; import io.grpc.stub.StreamObserver; public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase { @Override public void queryStudentNameById(MyRequestId request, StreamObserver responseObserver) { System.out.println("模拟查询此id的用户名:" + request.getId()); // 假设此 id 的 name 是“zs” responseObserver.onNext(MyResponseName.newBuilder().setName("zs").build()); responseObserver.onCompleted(); } ]

三 编写服务端代码

package grpc; import io.grpc.Server; import io.grpc.ServerBuilder; import java.io.IOException; public class MyGRPCServer { private Server server; // 启动服务 private void start() throws IOException { int port = 8888; server = ServerBuilder.forPort(port) .addService(new StudentServiceImpl()) .build() .start(); Runtime.getRuntime().addShutdownHook(new Thread(() ->{ System.err.println(Thread.currentThread().getName() + ",关闭JVM"); // 当 JVM 关闭前,先关闭 MyGRPCServer服务 MyGRPCServer.this.stop(); } )); } // 关闭服务 private void stop() { if (server != null) { server.shutdown(); } } private void blockUntilShutdown() throws InterruptedException { if (server != null) { // 等待服务结束 server.awaitTermination(); } } public static void main(String[] args) throws IOException, InterruptedException { final MyGRPCServer server = new MyGRPCServer(); server.start(); server.blockUntilShutdown(); } }

四 编写客户端代码

package grpc; import grpc.proto.MyRequestId; import grpc.proto.MyResponseName; import grpc.proto.StudentServiceGrpc; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; public class MyGRPCClient { public static void main(String[] args) throws Exception { // 创建一个客户端 ManagedChannel client = ManagedChannelBuilder.forAddress("127.0.0.1", 8888) .usePlaintext().build(); try { // 创建客户端的代理对象,用于代表客户端去访问服务端提供的方法 StudentServiceGrpc.StudentServiceBlockingStub stub = StudentServiceGrpc .newBlockingStub(client); // 请求Request,响应Response // 调用服务端提供的方法,查询id为1的姓名 MyResponseName responseName = stub.queryStudentNameById(MyRequestId.newBuilder() .setId(1).build()); System.out.println(responseName.getName()); } finally { client.shutdown(); } } }

五 测试

1 启动服务端和客户端

2 服务端打印如下

模拟查询此id的用户名:1

3 客户端打印如下

zs

六 冲突解决

GRPC-Server报错服务端启动com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava异常解决方案_时间是一种解药的博客-CSDN博客

https://blog.csdn.net/cucgyfjklx/article/details/122681339

guava 版本冲突,保留最高版本的 guava 可解决问题。

解决冲突后的 pom 文件如下

  4.0.0 org.example demo2022 1.0-SNAPSHOT  org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE   UTF-8 1.15.0 3.5.1 3.5.1-1 2.0.7.Final     org.springframework.boot spring-boot-devtools true    cn.afterturn easypoi-base 4.1.0   guava com.google.guava     cn.afterturn easypoi-web 4.1.0   cn.afterturn easypoi-annotation 4.1.0   io.netty netty-all 4.1.29.Final compile   com.google.protobuf protobuf-java 3.5.1   org.apache.httpcomponents httpclient 4.3   httpcore org.apache.httpcomponents     org.apache.httpcomponents httpcore 4.4.4   com.alibaba fastjson 1.1.23   org.apache.thrift libthrift 0.11.0   httpcore org.apache.httpcomponents   httpclient org.apache.httpcomponents     io.grpc grpc-netty-shaded ${grpc.version}   io.grpc grpc-protobuf ${grpc.version}   guava com.google.guava     io.grpc grpc-stub ${grpc.version}   io.grpc grpc-alts ${grpc.version}   httpclient org.apache.httpcomponents   grpc-grpclb io.grpc     io.grpc grpc-testing ${grpc.version} test   mockito-core org.mockito      io.grpc grpc-netty ${grpc.version}   io.netty netty-tcnative-boringssl-static ${netty.tcnative.version}   com.google.api.grpc proto-google-common-protos 1.0.0   com.google.protobuf protobuf-java-util ${protobuf.version}   guava com.google.guava     junit junit 4.12 test   org.mockito mockito-core 1.9.5 test      kr.motd.maven os-maven-plugin 1.5.0.Final     org.springframework.boot spring-boot-maven-plugin  true    org.xolstice.maven.plugins protobuf-maven-plugin 0.5.1  com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}  grpc-java io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}      compile compile-custom       
25 4 0
Bài viết được đề xuất: 多张图解,一扫你对多线程问题本质的所有误区
Bài viết được đề xuất: 深度学习中的优化算法之Nadam
Bài viết được đề xuất: gRPC 实战之——客户端为 Stream,服务端为 Stream
Bài viết được đề xuất: Giới thiệu về Apache Thrift
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com