我无法思考三种情况。
- Lagom 服务使用同一集群中的另一个 Lagom 服务
- Lagom 服务使用不同集群中的另一个 Lagom 服务
- Lagom 服务使用外部非 Lagom 服务
- 外部非 Lagom 服务使用 Lagom 服务
<强>1。 Lagom服务消耗同一集群中的另一个Lagom服务
对于这种情况,方法是 ServiceAImpl 依赖于 ServiceB API,该 API 绑定(bind)到将注入(inject)到 ServiceAImpl 的具体实现。
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);
}
}
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/