简体   繁体   English

是否可以使用 WildFly 在 Java 应用程序中运行 gRPC 服务器?

[英]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问题

  1. 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. bean 是在 WildFly 中创建的,但是无论我 @Inject 到 gRPC 服务中的对象是什么,它始终具有null值。
  2. The server executes the code and replies with an appropriate gRPC message but this message never arrives at client side.服务器执行代码并使用适当的 gRPC 消息进行回复,但此消息永远不会到达客户端。 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?有没有人有这些问题的经验,或者知道是否可以在 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.

相关问题 是否可以使用 Glassfish 应用程序服务器运行 gRPC? - Is it possible to run gRPC with Glassfish application server? 如何在不同 java 版本的 Wildfly 服务器上运行两个 web 应用程序? - How to run two web application on wildfly server with different java version? 如何使用Eclipse停止Wildfly 8.2.1最终服务器上的应用程序-Java - How to stop application on Wildfly 8.2.1 final server using Eclipse - Java 在JBoss Wildfly服务器上运行Spring应用程序 - Run a Spring Application on a JBoss Wildfly Server 是否可以仅使用 Nginx 服务器运行 Java web 应用程序而不使用 Z1B359D87540678B535BEFA04 - Is it possible to run Java web application using Nginx server only without tomcat server? 使用Java在OpenCL上运行C应用程序-可能吗? - Run C application on OpenCL using Java - possible? 是否可以在WebSphere Application Server上自动运行Java应用程序? - Is it possible to run automatically a Java application on WebSphere Application Server? 将WildFly应用程序服务器与NetBeans IDE一起使用 - Using the WildFly application server with NetBeans IDE 如何将Java Web应用程序部署到远程Wildfly服务器? - How to deploy the java web-application to a remote wildfly server? 无法通过Java Web应用程序在Wildfly服务器中创建目录 - Can't create directory in the Wildfly server from the java web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM