首页 文章

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

提问于
浏览
3

根据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 . 将其与其他Lagom API(如CompletionStage#doWithService)结合起来很痛苦 .

3 回答

  • 1

    从lagom消费第三方REST服务的一种方法是使用Lagom Descriptor 编写第三方的REST规范 .

    想象一下,您的代码想要与Slack的API进行交互,您可以在应用程序中创建一个 slack-api 项目并在那里创建Slack descriptor(当然,您不需要创建 slack-impl ) .

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

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

  • 0

    在Lagom中,任何第三方应用程序/服务都可以作为非托管服务进行访问 . 您需要在application.conf中添加它

    #external-services lagom.services { 3rd-party-service-identifier = "http://3rd-party-service-url" ... } 并且还需要在pom.xml中添加第三方服务URL作为unmanagedService,就像这样

    <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知道第三方服务的方法,但要使用该服务,请访问以下链接 . https://www.lagomframework.com/documentation/1.4.x/java/IntegratingNonLagom.html

  • 0

    由于之前的答案都不包含有关如何在Java中实现它的完整信息,因此我的方法如下:

    • 创建Lagom API模块,例如external-service-api并将请求/响应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);
        }
    }
    
    • 在impl模块中向您要使用它的external-service-api添加依赖项 .

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

    • 现在是棘手的部分,我找到了一些教程/存储库,其中在实现模块的pom.xml中定义了非托管服务 . 它对我不起作用,我不知道为什么(Lagom v.1.4.5,Scala二进制2.12) . 我必须做的是将unmanagedServices定义放在项目root 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>
    
    • 注入您的服务:
    @Inject
    public SomeConstructor(ExternalService externalService){
        ...
    }
    
    • 要使用它,请致电: externalService.somePostMethod().invoke(new ExternalServiceRequest(...))

相关问题