在 Lagom 中使用外部 REST 服务最简单的方法是什么?

What is the simplest way to consume an external REST service in Lagom?

根据 Lagom 文档,我们可以定义外部服务 URI(如下所示)并可以从 ServiceLocator 中获取它。

lagomUnmanagedServices in ThisBuild := Map("weather" -> "http://localhost:3333")

http://www.lagomframework.com/documentation/1.0.x/ServiceLocator.html#Communicating-with-external-services

在Lagom中调用外部REST最简单的方法是什么API?

我考虑过在Lagom中使用WsClient,但我没有选择它。 Lagom 仅包含 Scala 的 WsClient,因此它提供的结果值不是 java.util.concurrent.CompletionStage,而是 scala.concurrent.Future。 与 CompletionStage#doWithService.

等其他 Lagom APIs 结合起来很痛苦

使用 Lagom 的第 3 方 REST 服务的一种方法是使用 Lagom Descriptor.

编写第 3 方的 REST 规范

假设您的代码想要与 Slack 的 API 交互,您将在您的应用程序中创建一个 slack-api 项目并在那里创建 Slack descriptor(您不需要创建一个slack-impl当然)。

然后,在您的 fancy-impl 代码中,您将依赖于 slack-api,而在您的 FancyServiceImpl 实现中,您将在构造函数中注入 SlackService

PS:要点是 scala 代码,但同样的想法适用于 Lagom 的 Java DSL。

在 Lagom 中,任何第三方 application/service 都可以作为非托管服务进行访问。您需要像这样

在 application.conf 中添加它

#external-services lagom.services { 3rd-party-service-identifier = "http://3rd-party-service-url" ... } 并且还需要将第 3 方服务 url 添加为 pom.xml 中的非托管服务,就像这样

<plugin>
    <groupId>com.lightbend.lagom</groupId>
    <artifactId>lagom-maven-plugin</artifactId>
    <version>${lagom.version}</version>
    <configuration>
      <kafkaEnabled>false</kafkaEnabled>
      <cassandraEnabled>false</cassandraEnabled>
      <unmanagedServices>
        <3rd-party-service-identifier>${3rd-party-service-URL}</3rd-party-service-identifier>
      </unmanagedServices>
    </configuration>
  </plugin>

这就是您让 Lagom 了解第 3 方服务的方式,但要让 Lagom 与该服务一起使用,请转到下面的 link。 https://www.lagomframework.com/documentation/1.4.x/java/IntegratingNonLagom.html

由于 none 之前的答案包含有关如何在 Java 中实施它的完整信息,以下是我的做法:

  1. 创建 Lagom API 模块,例如external-service-api 并在其中放入 request/response DTO 和 Lagom 服务,例如:

    public interface ExternalService extends Service {
    
        ServiceCall<ExternalServiceRequest, ExternalServicePostResponse> somePostMethod();
    
        ServiceCall<NotUsed, ExternalServiceGetResponse> someGetMethod();
    
        @Override
        default Descriptor descriptor(){
            return Service.named("external-service").withCalls(
                Service.pathCall("/post-endpoint", this::somePostMethod),
                Service.pathCall("/get-endpoint", this::someGetMethod)
            ).withAutoAcl(true);
        }
    }
    
  2. 在要使用它的 impl 模块中添加对外部服务的依赖-api。

  3. 在 *Module.java 中注册您的服务。使用:bindClient(ExternalService.class);

  4. 现在是棘手的部分,我发现一些tutorials/repositories在实现模块的pom.xml中定义了非托管服务。它对我不起作用,我也不知道为什么(Lagom v. 1.4.5,Scala 二进制文件 2.12)。我必须做的是将 unmanagedServices 定义放在项目根 pom.xml 中。请注意,unmanagedService 子元素的名称应与您在描述符中定义的名称相同。

        <plugin>
            <groupId>com.lightbend.lagom</groupId>
            <artifactId>lagom-maven-plugin</artifactId>
            <version>${lagom.version}</version>
            <configuration>
                <unmanagedServices>
                    <external-service>http://api.url.com/</external-service>
                </unmanagedServices>
            </configuration>
        </plugin>
    
  5. 注入您的服务:

    @Inject
    public SomeConstructor(ExternalService externalService){
        ...
    }
    
  6. 要使用它调用例如:externalService.somePostMethod().invoke(new ExternalServiceRequest(...))