英文:
Is it possible to run a gRPC server in a Java application using WildFly?
问题
我正在尝试在我们的WildFly应用程序中运行一个gRPC服务器,以便从使用Quarkus的客户端Java微服务进行连接。
我已经能够在端口9002上设置一个运行中的gRPC服务器,客户端微服务可以连接到该服务器。
当对正在运行的gRPC服务进行调用时,我可以看到rpc函数在WildFly应用程序内部被执行。然而,我遇到了两个问题。
对于服务器和服务设置,我使用了这个代码作为示例。
问题
- 依赖注入在这个服务中不起作用。在WildFly中创建了该bean,但是无论我在gRPC服务中注入什么对象,它的值始终为
null
。 - 服务器执行代码并回复了适当的gRPC消息,但是这个消息永远不会到达客户端。没有显示任何错误信息。
是否有人对这些问题有经验,或者知道在WildFly应用程序中运行gRPC服务器甚至是否可能?
我尝试过的:
我试图找到一个现有的与WildFly集成的gRPC解决方案,但目前正在进行中,我从这个开放的拉取请求中得出了这一点。
从这个邮件列表中可以看出,有人制作了一个概念验证,并对WildFly代码进行了更改使其工作,然而这不是我们寻找的解决方案,因为我们不希望改变WildFly代码。
相关代码
客户端代码在blockingStub.updateBalance(request)
调用处挂起,因为从未收到回复:
BalanceUpdateGrpc.BalanceUpdateBlockingStub blockingStub = BalanceUpdateGrpc.newBlockingStub(channel);
var request = BalanceUpdateRequest.newBuilder()
.setContractNumber("111")
.setBalance((float) 100.2)
.setDate(BalanceUpdateRequest.Date
.newBuilder()
.setDay(3)
.setMonth(11)
.setYear(2020)
.build())
.build();
var reply = blockingStub.updateBalance(request);
服务端代码:
@Singleton
public class BalanceUpdateService extends BalanceUpdateGrpc.BalanceUpdateImplBase {
@Inject
private DossierDAO dossierDAO;
@Override
public void updateBalance(BalanceUpdateRequest balanceUpdateRequest, StreamObserver<BalanceUpdateReply> responseObserver) {
//只是回复true
//在这个代码块中使用dossierDAO时,它总是为null
responseObserver.onNext(BalanceUpdateReply.newBuilder().setSuccess(true).build());
}
}
英文:
I'm trying to run a gRPC server in our WildFly application to connect to from a client Java microservice using Quarkus.
I was able to set up a running server on port 9002 for gRPC where the client microservice can connect to.
When doing a call to the running gRPC service, I can see the rpc function is executed inside the WildFly application. I'm running into 2 issues however.
For the Server and Service setup, I used this code as an example.
Issues
- Dependency injection does not work in this service. The bean is created in WildFly but whatever object I @Inject into the gRPC Service, it always has a
null
value. - The server executes the code and replies with an appropriate gRPC message but this message never arrives at client side. No error is displayed whatsoever.
Does anyone have experience with these issues or knows if it is even possible to run a gRPC server within a WildFly application?
What I tried:
I tried to find an existing gRPC integration with WildFly but this is currently in the works as I could derive from this open pull request.
From this mailing list it seems that someone made a Proof Of Concept and made changes to the WildFly code to make it work, however this is not the solution that we are looking for since we'd prefer not to change the WildFly code.
Relevant code
The client code hangs on the blockingStub.updateBalance(request)
call because no reply is ever received:
BalanceUpdateGrpc.BalanceUpdateBlockingStub blockingStub = BalanceUpdateGrpc.newBlockingStub(channel);
var request = BalanceUpdateRequest.newBuilder()
.setContractNumber("111")
.setBalance((float) 100.2)
.setDate(BalanceUpdateRequest.Date
.newBuilder()
.setDay(3)
.setMonth(11)
.setYear(2020)
.build())
.build();
var reply = blockingStub.updateBalance(request);
The service code:
@Singleton
public class BalanceUpdateService extends BalanceUpdateGrpc.BalanceUpdateImplBase {
@Inject
private DossierDAO dossierDAO;
@Override
public void updateBalance(BalanceUpdateRequest balanceUpdateRequest, StreamObserver<BalanceUpdateReply> responseObserver) {
//Just reply with true
//dossierDAO is always null when used in this code block
responseObserver.onNext(BalanceUpdateReply.newBuilder().setSuccess(true).build());
}
}
答案1
得分: 2
以下是您要翻译的内容:
我没有使用 WildFly 的经验,但这似乎与应用程序上下文相关的经典问题。当您使用 NEW 获取对象而不是通过 IOC 获取时,就会发生这种情况。这样做时,注入不起作用。您了解 Spring 吗?Spring 有相同的概念。也许您可以将这个 Spring 代码转移到 WildFly。
以下是一个可以帮助您的 gRPC 和 Spring 示例。https://github.com/apssouza22/modern-api-management/blob/master/services/shelf/src/main/java/com/apssouza/shelf/App.java
对于这个问题,您只需要更好地了解您的框架如何工作。
英文:
I don't have experience with WildFly but it seems the classic issue related to the application context. It happens when you get an object with NEW instead of through the IOC. When you do that, the injections don't work.
Do know Spring? Spring has the same concept. Maybe you can transpose this Spring code to WildFly
public static void main(String[] args)throws IOException, InterruptedException {
SpringApplication springApplication = new SpringApplication(App.class, GrpcConfig.class);
//Getting application context with all objects created with annotations
ConfigurableApplicationContext context = springApplication.run(args);
// Getting object created by Spring "Wildfly" from outside the context
GrpcServer grpcServer = context.getBean(GrpcServer.class);
grpcServer.start();
}
Here is an example of gRPC and Spring which can help you. https://github.com/apssouza22/modern-api-management/blob/master/services/shelf/src/main/java/com/apssouza/shelf/App.java
For this issue, you just need to know better how your framework works
答案2
得分: 1
我需要添加:
responseObserver.onCompleted()
到服务中以便将响应发送回客户端。然而,“null”注入仍然是一个问题。
英文:
I needed to add:
responseObserver.onCompleted()
to the service to get the response sent back to the client.
The null
injection is still an issue however.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论