我正在使用 GRPC 开发一个聊天应用程序,其中服务器从客户端接收信息并将其发送回所有连接到它的客户端。为此,我使用了Saturnism 的 聊天示例作为参考。我复制了代码,代码编译并运行,但服务器应该从未收到来自客户端的任何请求。
我的问题是:
文库服务器.java
public class WingokuServer {
public static void main(String[] args) throws IOException, InterruptedException {
Server server = ServerBuilder.forPort(8091)
.intercept(recordRequestHeadersInterceptor())
.addService(new WingokuServiceImpl())
.build();
System.out.println("Starting server...");
server.start();
System.out.println("Server started!");
server.awaitTermination();
}
Run Code Online (Sandbox Code Playgroud)
WinokuServerSideService实现:
public class WingokuServiceImpl extends WingokuServiceGrpc.WingokuServiceImplBase {
private static Set<StreamObserver<Response>> observers =
Collections.newSetFromMap(new ConcurrentHashMap<>());
public WingokuServiceImpl() {
System.out.println("WingokuServiceImp");
}
@Override
public StreamObserver<Request> messages(StreamObserver<Response> responseObserver) {
System.out.println("messages");
observers.add(responseObserver);
return new StreamObserver<Request>() {
@Override
public void onNext(Request request) { …Run Code Online (Sandbox Code Playgroud) 我添加了 maven 配置如下。
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.0.0</protocArtifact>
<inputDirectories>
<include>src/main/protobuf</include>
</inputDirectories>
<outputTargets>
<outputTarget>
<type>java</type>
<outputDirectory>src/main/java</outputDirectory>
</outputTarget>
<outputTarget>
<type>grpc-java</type>
<outputDirectory>src/main/java</outputDirectory>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1</pluginArtifact>
</outputTarget>
</outputTargets>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我通过“mvn protoc-jar:run”执行它,但是它跳过了grpc服务存根,只将“消息”转换为java代码。
输出是
[INFO] Protoc version: 3.5.1
protoc-jar: protoc version: 3.5.1, detected platform: osx-x86_64 (mac os x/x86_64)
protoc-jar: embedded: bin/3.5.1/protoc-3.5.1-osx-x86_64.exe
protoc-jar: executing: [/var/folders/9y/w8qrc__9513dv57323sjdlmw0000gn/T/protocjar320569499467403052/bin/protoc.exe, --version]
libprotoc 3.5.1
[INFO] Protoc command: /var/folders/9y/w8qrc__9513dv57323sjdlmw0000gn/T/protocjar320569499467403052/bin/protoc.exe
[INFO] Input directories:
[INFO] /Users/dev/learn/proto-java/src/main/protobuf
[INFO] Output targets:
[INFO] java: /Users/dev/learn/proto-java/target/generated-sources (add: main, clean: false, plugin: null, …Run Code Online (Sandbox Code Playgroud) 如果我按照这两个测试运行,我会收到错误消息。
第一次测试
@Rule
public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
@Test
public void findAll() throws Exception {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
// Create a server, add service, start, and register for automatic graceful shutdown.
grpcCleanup.register(InProcessServerBuilder
.forName(serverName)
.directExecutor()
.addService(new Data(mockMongoDatabase))
.build()
.start());
// Create a client channel and register for automatic graceful shutdown.
RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(
grpcCleanup.register(InProcessChannelBuilder
.forName(serverName)
.directExecutor()
.build()));
RoleOuter.Response response = stub.findAll(Empty.getDefaultInstance());
assertNotNull(response);
}
Run Code Online (Sandbox Code Playgroud)
第二次测试
@Test
public void testFindAll() {
ManagedChannel channel …Run Code Online (Sandbox Code Playgroud) 它对我来说是新话题。我能够以明文形式连接。
public ManagedChannel getChannel(String serviceName){
TSServiceClientManager scm = TSServiceManagementFactory.getInstance().getServiceClientManager();
TSServiceConnectionInfo connInfo = scm.getServiceConnectionInfo(serviceName);
if(channel == null){
channel = ManagedChannelBuilder.forAddress(connInfo.getHost(), connInfo.getPort())
.usePlaintext(true) //need help here for SSL code
.build();
}
return channel;
}
Run Code Online (Sandbox Code Playgroud)
我被告知要启用客户端 SSL。我知道如何生成密钥库、信任库、pem、CA 等。我需要以下方面的帮助:
如何启用 SSL 而不是 .usePlaintext(true) 如上面的代码所示?
(考虑到证书文件、密钥库、信任库和 .pem 文件存在,请重写代码)
和
我想知道与服务器有什么关系才能使 SSL 连接正常工作?
GRPC 服务器确实对请求进行排队,并根据服务器启动时传递的 maxWorker 配置来为它们提供服务。如何打印队列中项目的指标数量?本质上,我想跟踪处于等待状态的请求数量。
我是 Spring webflux 和 protobuf 的新手。我读过一些东西,发现它们之间有一些相似之处。喜欢
然而,我仍然找不到任何结合了 webflux(反应式编程)、gRPC(更快的数据编码和解码)和 Spring(依赖注入)功能的示例。那些不兼容吗?
我的应用程序在使用此版本的依赖项时运行得非常好,如下所述,但是当迁移到最新版本的 Firestore 依赖项时,我的应用程序无法在 Firestore 中添加或删除数据,我知道最新的依赖项不需要 firebase-core 依赖项,尽管遵循官方文档中提到的所有要求的 firebase,我的应用程序仍然无法使用最新的依赖项,但可以与下面提到的依赖项完美配合
我想使用 FirestoreUI 这就是为什么将我的项目迁移到最新版本的原因,我还尝试了本网站中提到的发行说明传递依赖项标准, https://github.com/firebase/FirebaseUI-Android/releases
但是仍然显示此错误,请帮助我我从我这边尝试所有可能的事情,我希望我们的 StackOverflow 家庭帮助新来的开发人员
感谢您的宝贵时间...快乐编码:)
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-firestore:17.1.2'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.firebaseui:firebase-ui-auth:6.2.0'
implementation 'com.google.android.material:material:1.2.0-alpha06'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Run Code Online (Sandbox Code Playgroud)
将数据添加到最新版本的 Firestore 时,反复显示此错误(下文提及),
implementation 'com.google.firebase:firebase-firestore:17.1.2'
I/Firestore: (21.4.1) [GrpcCallProvider]: Current gRPC connectivity state: CONNECTING
I/Firestore: (21.4.1) [GrpcCallProvider]: Setting the connectivityAttemptTimer
I/Firestore: (21.4.1) [GrpcCallProvider]: connectivityAttemptTimer elapsed. Resetting the channel.
I/Firestore: (21.4.1) [GrpcCallProvider]: Clearing the connectivityAttemptTimer
I/Firestore: (21.4.1) [GrpcCallProvider]: Current gRPC connectivity …Run Code Online (Sandbox Code Playgroud) dependencies firebase android-studio grpc-java google-cloud-firestore
我正在使用 gRPC 应用程序并构建一个简单的应用程序。在文件下面。
syntax = "proto3";
option java_multiple_files = true;
package com.grpc;
message HelloRequest {
string firstName = 1;
string lastname = 2;
}
message HelloResponse {
string greeting = 1;
}
service HelloService {
rpc hello(HelloRequest) returns (HelloResponse);
}
Run Code Online (Sandbox Code Playgroud)
它正常生成文件
User.java -> generated from protoc compiler
userGrpc.java -> generated from protoc compiler
Run Code Online (Sandbox Code Playgroud)
GRPC服务.java
package GRPCService;
import java.io.IOException;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import user.UserService;
public class GPRCService {
public static void main(String[] args) throws IOException, InterruptedException {
Server server = …Run Code Online (Sandbox Code Playgroud) 我正在尝试正确处理 gRPC 错误(Java、Spring-boot 应用程序)。
基本上,我也需要将错误详细信息从 gRPC 服务器传输到客户端,但我发现很难理解 的正确用法StreamObserver.onError();
方法文档说:
“从流中接收到终止错误。只能调用一次,如果调用,则必须是最后调用的方法。特别是,如果 onError 的实现引发异常,则不允许进一步调用任何方法。”
“不允许再打电话”是什么意思?在我维护的应用程序中,他们调用其他 gRPC 方法,并且java.lang.IllegalStateException: call already closed根据文档,他们得到了很好的结果。
我想知道 - 我(开发人员)应该在收到错误后终止当前的 java 方法(通常使用 gRPC 调用)吗?例如抛出异常来停止执行。或者预计 gRPC 将终止执行..(类似于从 gRPC 抛出异常)
基本上我如何正确使用onError()以及如果我调用它我应该期待和处理什么?我需要解释一下它的用法和效果。
我们目前正在使用 grpc-starter 包https://github.com/yidongnan/grpc-spring-boot-starter实现高吞吐量的 Spring Boot 应用程序,这是一个基于客户端服务器的应用程序。我们正在将旧的 REST 端点 CRUD 操作迁移到 GRPC。为了获得这项服务的最佳设计,我们需要帮助解决以下问题:
如果单个不可变 GRPC 阻塞存根客户端实例被多个线程访问,这将是阻塞调用,即在任何给定时间仅执行一个 RPC 调用。
根据 google GRPC IO 文档,建议不要使用阻塞存根来并行化 RPC 调用。这是否突出显示了同一客户端对象上的多个 RPC 调用的情况。https://grpc.io/docs/guides/performance/
阻塞存根是否会为每个调用使用新的 TCP 连接,或者重复使用相同的 TCP 连接。