[英]Is it possible to run a gRPC server in a Java application using WildFly?
I'm trying to run a gRPC server in our WildFly application to connect to from a client Java microservice using Quarkus.我正在尝试在我们的 WildFly 应用程序中运行 gRPC 服务器,以使用 Quarkus 从客户端 Java 微服务连接。 I was able to set up a running server on port 9002 for gRPC where the client microservice can connect to.
我能够在端口 9002 上为 gRPC 设置一个正在运行的服务器,客户端微服务可以连接到该服务器。
When doing a call to the running gRPC service, I can see the rpc function is executed inside the WildFly application.在调用正在运行的 gRPC 服务时,我可以看到 rpc 函数在 WildFly 应用程序内部执行。 I'm running into 2 issues however.
但是,我遇到了两个问题。 For the Server and Service setup, I used this code as an example.
对于服务器和服务设置,我使用此代码作为示例。
Issues问题
null
value. null
值。 Does anyone have experience with these issues or knows if it is even possible to run a gRPC server within a WildFly application?有没有人有这些问题的经验,或者知道是否可以在 WildFly 应用程序中运行 gRPC 服务器?
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 .我试图找到与 WildFly 的现有 gRPC 集成,但目前正在开发中,因为我可以从这个 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.从这个邮件列表看来,似乎有人进行了概念验证并对 WildFly 代码进行了更改以使其工作,但这不是我们正在寻找的解决方案,因为我们不想更改 WildFly 代码。
Relevant code相关代码
The client code hangs on the blockingStub.updateBalance(request)
call because no reply is ever received:客户端代码挂在
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);
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());
}
}
I don't have experience with WildFly but it seems the classic issue related to the application context.我没有使用 WildFly 的经验,但似乎是与应用程序上下文相关的经典问题。 It happens when you get an object with NEW instead of through the IOC.
当您使用 NEW 而不是通过 IOC 获取对象时,就会发生这种情况。 When you do that, the injections don't work.
当你这样做时,注射不起作用。
Do know Spring?知道春天吗? Spring has the same concept.
Spring 也有同样的概念。 Maybe you can transpose this Spring code to WildFly
也许你可以把这个 Spring 代码转成 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.这是 gRPC 和 Spring 的示例,可以帮助您。 https://github.com/apssouza22/modern-api-management/blob/master/services/shelf/src/main/java/com/apssouza/shelf/App.java
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对于这个问题,你只需要更好地了解你的框架是如何工作的
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.然而,
null
注入仍然是一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.