Java中GRPC的NewBlockingStub中的异常线程

发布于 2025-02-06 19:52:10 字数 709 浏览 1 评论 0原文

我是GRPC的新手。使用原始文件创建Java存根后,我在发送new Blocking生成的Java存根文件的loginapi从客户端发送存根。服务器正在启动,但客户端获得了例外。

客户端代码:

userBlockingStub userStub = userGrpc.newBlockingStub(channel);

生成的Java存根文件:

public static userBlockingStub newBlockingStub(io.grpc.Channel channel) {
    return new userBlockingStub(channel);
}

获取此例外:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at com.grpc.userGrpc.newBlockingStub(userGrpc.java:106)
at grpcClient.grpcClient.main(grpcClient.java:19)

有任何解决方案吗?

I am new to gRPC. After creating java stubs using a proto file, I am getting an exception in the newBlocking stub of the generated java stub file when sending a loginAPI request from client. The server is starting, but the client getting an exception.

Client code:

userBlockingStub userStub = userGrpc.newBlockingStub(channel);

generated Java stub file:

public static userBlockingStub newBlockingStub(io.grpc.Channel channel) {
    return new userBlockingStub(channel);
}

Getting this exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

at com.grpc.userGrpc.newBlockingStub(userGrpc.java:106)
at grpcClient.grpcClient.main(grpcClient.java:19)

Is there any solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2025-02-13 19:52:10

首先,当您尝试实现grpc通信时,因此您可能拥有多个服务(项目),您希望将它们传达给它们。

我本人实现了一个单独的项目grpc-interface并将其添加到git存储库中,然后我可以使用git-submodule subpodule代码>我需要传达的服务(项目)的功能。

我实现了我的grpc-interface如下:

这是我的build.gradle文件,您可以找到带有小R& d的maven版本也很容易:

buildscript {
    ext {
        protobufVersion = '3.19.1'
        protobufPluginVersion = '0.8.18'
        grpcVersion = '1.44.0'
        protobufJava = '3.19.4'
    }
}

plugins {
    id 'java'
    id 'com.google.protobuf' version "${protobufPluginVersion}"
}

group = 'com.ada.bourse.grpc-interface'
version = '0.0.1-SNAPSHOT'

dependencies {
    implementation "io.grpc:grpc-protobuf:${grpcVersion}"
    implementation "io.grpc:grpc-stub:${grpcVersion}"
    implementation "com.google.protobuf:protobuf-java:${protobufJava}"
    compileOnly 'jakarta.annotation:jakarta.annotation-api:1.3.5' // Java 9+ compatibility - Do NOT update to 2.0.0
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${protobufVersion}"
    }
    generatedFilesBaseDir = "$projectDir/src/generated"
    clean {
        delete generatedFilesBaseDir
    }
    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

repositories {
    mavenCentral()
}

然后我创建了一个.proto文件来介绍我的服务及其请求和响应模型:

syntax = "proto3";

package com.ada.bourse.grpc.accounting;

option java_multiple_files = true;
option java_package = "com.ada.bourse.grpc.accounting";

service AccountingService {
  rpc userBalance(UserBalanceRequest) returns (UserBalanceResponse) {
  }
}

message UserBalanceRequest {
  string username = 1;
}

message UserBalanceResponse {
  double balance = 1;
}

在此步骤之后,您需要编译该项目,并且将导致使用生成的在您的main软件包旁边的名称。

然后,正如我说的那样,如果您将该项目放入诸如git之类的存储库中,则需要使用git-submodule功能将其添加到您的项目中,当然,还有其他方法可以将其添加到您的项目中项目和git-submodule不是必须的!

只需考虑一下,将此模块添加到项目之后,在运行和编译主项目之前,您需要编译grpc-interface,以便您的项目可以使用生成的文件喜欢:

”在此处输入图像描述

现在我有两个服务,名为withs-manager会计>会计我想传达,正如您在我的.proto文件中看到的那样,我希望我的财富服务来请求并从我的Accounting服务中检查用户余额。

在我的财富服务中,我在服务层中所做的服务:

@Service
public class RestrictionServiceImpl implements IRestrictionService {

    @GrpcClient("accounting-service")
    private AccountingServiceGrpc.AccountingServiceBlockingStub accountingServiceStub;

private void checkUserBalanceForBuyOrder(RestrictionFilterCriteria filterCriteria) {
        Double userBalance = getUserBalanceFromAccountingServiceGrpc(filterCriteria.getUsername());
        if (userBalance < filterCriteria.getAmount() * filterCriteria.getPrice())
            throw new RestrictionException(new BaseException.ErrorDetail()
                    .setLoc("balance")
                    .setType(Constant.INSUFFICIENT_BALANCE));
    }

    private Double getUserBalanceFromAccountingServiceGrpc(String username) throws GrpcConnectionException {
        UserBalanceRequest request = UserBalanceRequest.newBuilder()
                .setUsername(username)
                .build();
        return accountingServiceStub.userBalance(request).getBalance();
    }

}

以及在我的Accounting服务中,我为grpc服务创建了类:

@GrpcService
public class AccountingServiceGRPC extends AccountingServiceGrpc.AccountingServiceImplBase {

    private final IUserAccountService userAccountService;

    public AccountingServiceGRPC(IUserAccountService userAccountService) {
        this.userAccountService = userAccountService;
    }

    @Override
    public void userBalance(UserBalanceRequest request, StreamObserver<UserBalanceResponse> responseObserver) {
        UserBalanceResponse balanceResponse = UserBalanceResponse.newBuilder()
                .setBalance(userAccountService.getUserBalance(request.getUsername()))
                .build();
        responseObserver.onNext(balanceResponse);
        responseObserver.onCompleted();
    }
}

我本人曾与grpc争论很多,直到我可以解决它,希望我的经验也可以帮助您解决自己的能力。

First of all, when you are trying to implement a gRPC communication, so you have probably more than one service(project) that you want them to be communicated.

I myself implemented a separate project grpc-interface and added it to the Git repository, then I could add this project as a submodule using the Git-submodule feature to my services(projects) that I needed to be communicated.

I implemented my grpc-interface as below:

here is my build.gradle file, you can find the Maven version with a little R&D easily too:

buildscript {
    ext {
        protobufVersion = '3.19.1'
        protobufPluginVersion = '0.8.18'
        grpcVersion = '1.44.0'
        protobufJava = '3.19.4'
    }
}

plugins {
    id 'java'
    id 'com.google.protobuf' version "${protobufPluginVersion}"
}

group = 'com.ada.bourse.grpc-interface'
version = '0.0.1-SNAPSHOT'

dependencies {
    implementation "io.grpc:grpc-protobuf:${grpcVersion}"
    implementation "io.grpc:grpc-stub:${grpcVersion}"
    implementation "com.google.protobuf:protobuf-java:${protobufJava}"
    compileOnly 'jakarta.annotation:jakarta.annotation-api:1.3.5' // Java 9+ compatibility - Do NOT update to 2.0.0
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:${protobufVersion}"
    }
    generatedFilesBaseDir = "$projectDir/src/generated"
    clean {
        delete generatedFilesBaseDir
    }
    plugins {
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

repositories {
    mavenCentral()
}

Then I created a .proto file to introduce my service and its request and response model:

syntax = "proto3";

package com.ada.bourse.grpc.accounting;

option java_multiple_files = true;
option java_package = "com.ada.bourse.grpc.accounting";

service AccountingService {
  rpc userBalance(UserBalanceRequest) returns (UserBalanceResponse) {
  }
}

message UserBalanceRequest {
  string username = 1;
}

message UserBalanceResponse {
  double balance = 1;
}

After this step, you need to compile the project and it will result in creating a package with a generated name beside your main package.

Then, as I said if you put this project in a repository like Git, you need to add it to your projects using the Git-submodule feature, of course, there are other ways to add it to your projects and Git-submodule is not a MUST!

just consider that, after adding this module to your projects, before running and compiling the main project you need to compile your grpc-interface so that your project can use generated files that is like:

enter image description here

Now I have two services, named wealth-manager and accounting that I want to be communicated, as you saw in my .proto file, I want my wealth service to request and check the user balance from my accounting service.

in my wealth service I did as below in my Service layer:

@Service
public class RestrictionServiceImpl implements IRestrictionService {

    @GrpcClient("accounting-service")
    private AccountingServiceGrpc.AccountingServiceBlockingStub accountingServiceStub;

private void checkUserBalanceForBuyOrder(RestrictionFilterCriteria filterCriteria) {
        Double userBalance = getUserBalanceFromAccountingServiceGrpc(filterCriteria.getUsername());
        if (userBalance < filterCriteria.getAmount() * filterCriteria.getPrice())
            throw new RestrictionException(new BaseException.ErrorDetail()
                    .setLoc("balance")
                    .setType(Constant.INSUFFICIENT_BALANCE));
    }

    private Double getUserBalanceFromAccountingServiceGrpc(String username) throws GrpcConnectionException {
        UserBalanceRequest request = UserBalanceRequest.newBuilder()
                .setUsername(username)
                .build();
        return accountingServiceStub.userBalance(request).getBalance();
    }

}

And in my accounting service I have created class for grpc services as below:

@GrpcService
public class AccountingServiceGRPC extends AccountingServiceGrpc.AccountingServiceImplBase {

    private final IUserAccountService userAccountService;

    public AccountingServiceGRPC(IUserAccountService userAccountService) {
        this.userAccountService = userAccountService;
    }

    @Override
    public void userBalance(UserBalanceRequest request, StreamObserver<UserBalanceResponse> responseObserver) {
        UserBalanceResponse balanceResponse = UserBalanceResponse.newBuilder()
                .setBalance(userAccountService.getUserBalance(request.getUsername()))
                .build();
        responseObserver.onNext(balanceResponse);
        responseObserver.onCompleted();
    }
}

I myself have argued with gRPC a lot until I could resolve it, hope my experience with it helps you to resolve yours too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文