- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
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(); } } }
模拟查询此id的用户名:1
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
关于 gRPC Health Checking ,如果 gRPC 服务托管在与其他也需要健康检查的 HTTP 服务相同的端口上,则对 grpc.health.v1.Health.Check 的响应应该
我的项目是读取服务器中的图像,进行一些处理,然后将整个图像传递给客户端。客户端占用图像并进行更多处理,并将一些输出值返回给服务器。服务器和客户端之间使用的图像大小为 [640x480x3]。 以下是我
gRPC 基于 HTTP/2,它(假设)被浏览器广泛支持。因此,我觉得从浏览器使用 gRPC 应该没有问题。 但是,很明显存在问题。协议(protocol),grpc web , 是不同的,因为“由于
关于服务器端代码的幂等性的问题,或者说它的必要性。对于一般的 gRPC,或者专门针对 java 实现。 当我们从客户端发送消息一次时,是否有可能由我们的服务实现处理两次?也许这与服务似乎不可用时的重试
假设我想使用 Grpc Server 流式传输或双向流式传输。 考虑到它在底层使用 http/2,流可以持续多长时间是否有任何限制? 如果是的话,它可以用来代替消息总线,这样流就可以打开并存活多久?
使用 gRPC 向客户端发送有关错误的更多详细信息的模式是什么? 例如,假设我有一个用于注册用户的表单,用于发送消息 message RegisterUser { string email = 1
我是 GRPC 的新手。我想知道当 GRPC 客户端启动一个请求时,服务器是否启动一个新线程来处理。 最佳答案 最多可能有一个 Runnable加入 Server's executor用于申请处理。每
我想传输一个 int64 数组。 我查了一下如何使用它。在我的原型(prototype)文件中,我需要一个流: service myService { rpc GetValues(myRequ
通常,客户端可以通过以下方式取消 gRPC 调用: (requestObserver as ClientCallStreamObserver) .cancel("Cancelled", nul
在 100Gb 网络上,我创建了一个服务器来监听 4 个端口,grpc 客户端可以达到 3GB+/s 的吞吐量。然而,当服务器监听一个端口时,grpc 客户端达到了 1GB/s 的吞吐量,即使我设置了
我想验证调用并可能在服务器拦截器中回答错误。有没有办法做到这一点?如果是,我该如何实现? 最佳答案 简单地从拦截器响应 RPC,可能通过调用 close() ,不要调用next .您仍然需要返回一个监
我有一个 GRPC API,经过重构,一些包被重命名。这包括我们定义 API 的原型(prototype)文件之一中的 package 声明。像这样的: package foo; service Ba
我想在 Ubuntu 上使用源代码中的所有子模块编译 grpc,并将其安装到/usr/local 以外的指定位置 为提供的 Makefile 指定此位置的方法是什么(类似于配置脚本的 --prefix
我不断在控制台中收到此警告: DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackag
上下文 我正在尝试使用 Google 的 Cloud Natural Language API。我有我的服务帐户 key JSON 文件,并且正在尝试编写一个简单的 .NET Core 应用程序(更具
用户在测试中遇到了此崩溃。我的第一个猜测是这与内存有关,但除此之外我没有什么可做的。更深入地研究代码,我认为这可能是主线程问题,但看起来监听器在后台线程上被删除,所以我怀疑这就是原因。 我认为在应用程
我正在编写一个 grpc 服务并在 Kubernetes (https://github.com/grpc-ecosystem/grpc-health-probe) 上使用 gRPC 健康检查。在我的
如何使用 gRPC-java 实现检测网络错误并尝试从中恢复的策略?我知道 UNAVAILABLE 状态可能意味着网络错误,但这并没有告诉我它是哪种网络错误 - 并且 UNAVAILABLE 也可以从
假设我们有一个 Search-Service service Search { rpc Search (SearchRequest) returns (SearchReply) {} } mess
如果浏览器支持http/2,为什么 grpc-web 需要 envoy 代理? 不支持 http/2 的旧浏览器是否只需要它? 最佳答案 回答于 https://github.com/grpc/grp
Tôi là một lập trình viên xuất sắc, rất giỏi!