从1.5.1版开始,Lagom通过封装Akka Discovery的ServiceLocator与Akka Discovery进行了内置集成。这个ServiceLocator实现称为AkkaDiscoveryServiceLocator。这是专门针对Kubernetes和DC/OS(Marathon)用户的推荐生产环境实现方式。
依赖
要使用此功能,请在项目的生产环境中添加以下内容:
libraryDependencies += lagomScaladslAkkaDiscovery
配置
一旦你的项目中有了它,你就可以把这个组件添加到你的LagomApplicationLoader中。
import com.lightbend.lagom.scaladsl.devmode.LagomDevModeComponentsimport com.lightbend.lagom.scaladsl.server._import com.lightbend.lagom.scaladsl.akka.discovery.AkkaDiscoveryComponentsclass HelloApplicationLoader extends LagomApplicationLoader {override def load(context: LagomApplicationContext) =new HelloApplication(context) with AkkaDiscoveryComponentsoverride def loadDevMode(context: LagomApplicationContext) =new HelloApplication(context) with LagomDevModeComponents}
默认情况下,Lagom使用多种聚合发现方法。第一个发现方法设置为配置,第二个设置为DNS。因此,服务端点的静态定义优先于DNS发现。
在application.conf中配置静态服务端点信息,参考有关聚合发现方法的文档。
DNS SRV vs. DNS A/AAAA Lookups
AkkaDiscoveryServiceLocator支持DNS SRV和DNS A查找。它默认为SRV查找,因为它是Kubernetes和DC/OS(Marathon)等环境中最常见的用法。
由于Lagom的ServiceLocatorAPI不支持SRV查找中使用的port-name 和protocol,因此AkkaDiscoveryServiceLocator将使用默认值作为备用值。默认的port-name是http(通过设置lagom.akka.discovery.defaults.port name定义),默认的protocol是tcp(通过设置lagom.akka.discovery.defaults.port protocol定义)。
仅当对不符合SRV格式的字符串进行查找时,才会使用这些值。例如,当使用Lagom服务客户端寻找另一个Lagom服务时。在这种情况下,查找是使用服务名称(由其ServiceDescriptor定义)以及port-name 和protocol的默认值来完成的。
如果两个lagom.akka.discovery.defaults.port-name 和lagom.akka.discovery.defaults.port-protocol 都设置为null或空白字符串,则相当于以简单DNS A查找方式进行查找。
配置服务映射
可以使用service-name-mappings在每个服务基础上覆盖这些值。
您可以将服务名称映射到SRV字符串,如下所示:
lagom.akka.discovery {service-name-mappings {my-service-name {# lookup is done using 'my-service-name'# but translated to SRV string _http._tcp.default.svc.cluster.locallookup = _http._tcp.default.svc.cluster.local}}}
您还可以覆盖端口名和协议以强制DNS A进行查找:
lagom.akka.discovery {service-name-mappings {# lookup is done using 'my-cassandra-server'# but translated to cassandra.default.svc.cluster.local# and without port name and protocol, ie: DNS A lookupmy-cassandra-server {lookup = cassandra.default.svc.cluster.localport-name = nullport-protocol = null}}}
每个服务覆盖将允许在cassandra服务器上进行DNS A查找,而任何其他查找仍将使用默认值。
默认设置在参考配置中定义为:
lagom.akka.discovery {# When the service lookup regex fails, the defaults are used for the port and protocol.defaults {# The default port name. Blank if no port name should be added by default.port-name = http# The default port protocol. Blank if no port protocol should be added by default.port-protocol = tcp# The default scheme to use in returned URIs if not defined in the port-name-scheme-mappings.scheme = http}# A mapping of service names to lookup information. Each mapping should define the following:## - lookup - An alternative name for the service. This can be configured with a simple name or a SRV lookup, for example:# * my-service (simple name)# * my-service.default.svc.cluster.local (simple fully-qualified name)# * _http._tcp.my-service (SRV)# * _http._tcp.my-service.default.svc.cluster.local (fully-qualified SRV)# - port-name - The port name to use. If undefined, it will use the default lagom.akka.discovery.defaults.port-name.# Setting to null or empty string will result to a lookup without port-name (eg: DNS A records)# - port-protocol - The protocol to use. If undefined, it will use the default lagom.akka.discovery.defaults.port-protocol.# Setting to null or empty string will result to a lookup without protocol (eg: DNS A records)# - scheme - The scheme to return in the URI. If undefined, it will use the default scheme lagom.akka.discover.defaults.scheme.## For example:# service-name-mappings {# my-service-name {# lookup = my-service.default.svc.cluster.local# port-name = http# port-protocol = tcp# scheme = http# }# }service-name-mappings {}# The timeout for a successful lookup.lookup-timeout = 5 seconds}
注:此组件以前作为独立库发布。如果你的类路径上有它,建议删除它,直接使用Lagom提供的。
