microservices - Lagom 服务如何使用其他服务?

标签 microservices service-discovery lagom

我无法思考三种情况。

  1. Lagom 服务使用同一集群中的另一个 Lagom 服务
  2. Lagom 服务使用不同集群中的另一个 Lagom 服务
  3. Lagom 服务使用外部非 Lagom 服务
  4. 外部非 Lagom 服务使用 Lagom 服务

<强>1。 Lagom服务消耗同一集群中的另一个Lagom服务

对于这种情况,方法是 ServiceAImpl 依赖于 ServiceB API,该 API 绑定(bind)到将注入(inject)到 ServiceAImpl 的具体实现。

ServiceB binding:

import com.google.inject.AbstractModule;
import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport;
import docs.services.HelloService;

public class Module extends AbstractModule implements ServiceGuiceSupport {

    protected void configure() {
        bindClient(HelloService.class);
    }
}

ServiceA implementation:

public class MyServiceImpl implements MyService {
  private final HelloService helloService;

  @Inject
  public MyServiceImpl(HelloService helloService) {
    this.helloService = helloService;
  }

  @Override
  public ServiceCall<NotUsed, NotUsed, String> sayHelloLagom() {
    return (id, msg) -> {
      CompletionStage<String> response = helloService.sayHello().invoke("Lagom");
      return response.thenApply(answer ->
          "Hello service said: " + answer
      );
    };
  }
}

如果我理解正确的话,为了以这种方式使用服务 API,两个客户端必须位于同一个集群中。 然而Lagom says那个

A cluster should only span nodes that are running the same service.

在本例中,我们有两种不同类型的服务。

  • “同一服务”是指其 API 暴露给外部服务的顶级服务吗?
  • 在 Lagom 中,1 个微服务 = 1 个具有外部 API 的服务 + n 个内部服务?

<强>2。 Lagom 服务使用不同集群中的另一个 Lagom 服务

文档 says :

Note that if the service you want to communicate with is actually a Lagom service, you may want to read the documentation for integrating with an external Lagom projects.

为什么只配置了对服务API的依赖,而不配置外部Lagom服务的IP和端口?

<强>3。 Lagom 服务使用外部非 Lagom 服务

The first thing you will have to do is to register each external service in the Service Locator. Assume we want to register an external service named weather that is running on http://localhost:3333, here is what we would add to the build:

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

与该 IP 签订的契约(Contract)是什么?后面应该是什么?

<强>4。外部非 Lagom 服务消费 Lagom 服务

我必须使用Third-Party Registration Pattern直到 Lagom 支持self registration pattern

最佳答案

当 Lagom 谈论“集群”时,它指的是 Akka 集群。每个服务都可以部署为一个Akka集群,即一个服务可能是一个节点集群。因此,集群中没有多个服务,只有一个集群服务。

Lagom 服务调用以相当直接的方式映射到惯用的 REST。因此,当与外部服务通信时,该 IP 上的东西应该是 REST 服务。同样,当外部服务与 Lagom 通信时,它应该使用 REST。

关于microservices - Lagom 服务如何使用其他服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582126/

相关文章:

c# - 客户端应用程序在 C# 中的本地网络上查找服务器的最佳方式是什么?

web-services - 使用 Consul 追踪所有 REST 端点

java - Lagom 框架中的单元测试范例是什么?

java - 如何保持两个 Web 应用程序的数据库同步?

docker - 找不到docker-compose https证书中的spring-cloud-config

json - 从微服务返回大数据的最快机制

service - 当面向消息的中间件完成这项工作时,为什么还要为服务发现而烦恼?

java - 我们如何将 lagom 的读取端处理器与 Dgraph 结合使用?

scala - Lagom 的多部分表单错误

java - Spring Cloud Service 向 Discovery Server 注册,但随后取消注册并停止