|
20 | 20 | package org.apache.pekko.discovery.eureka |
21 | 21 |
|
22 | 22 | import org.apache.pekko.actor.ActorSystem |
23 | | -import org.apache.pekko.discovery.{ Lookup, ServiceDiscovery } |
| 23 | +import org.apache.pekko.discovery.ServiceDiscovery.{Resolved, ResolvedTarget} |
| 24 | +import org.apache.pekko.discovery.eureka.JsonFormat._ |
| 25 | +import org.apache.pekko.discovery.{Lookup, ServiceDiscovery} |
| 26 | +import org.apache.pekko.event.{LogSource, Logging} |
| 27 | +import org.apache.pekko.http.scaladsl.Http |
| 28 | +import org.apache.pekko.http.scaladsl.model.headers._ |
| 29 | +import org.apache.pekko.http.scaladsl.model.{HttpRequest, MediaRange, MediaTypes, Uri} |
| 30 | +import org.apache.pekko.http.scaladsl.unmarshalling.Unmarshal |
24 | 31 |
|
| 32 | +import java.net.InetAddress |
25 | 33 | import scala.concurrent.Future |
26 | 34 | import scala.concurrent.duration.FiniteDuration |
| 35 | +import scala.util.Try |
27 | 36 |
|
28 | | -class EurekaServiceDiscovery(system: ActorSystem) extends ServiceDiscovery { |
| 37 | +class EurekaServiceDiscovery(implicit system: ActorSystem) extends ServiceDiscovery { |
| 38 | + |
| 39 | + import system.dispatcher |
| 40 | + |
| 41 | + private val log = Logging(system, getClass)(LogSource.fromClass) |
| 42 | + private val settings = EurekaSettings(system) |
| 43 | + private val (schema, host, port, path, group) = (settings.schema, settings.host, settings.port, settings.path, settings.groupName) |
| 44 | + private val http = Http() |
| 45 | + |
| 46 | + override def lookup(lookup: Lookup, resolveTimeout: FiniteDuration): Future[ServiceDiscovery.Resolved] = { |
| 47 | + |
| 48 | + val uriPath = Uri.Path.Empty / path / "apps" / lookup.serviceName |
| 49 | + val uri = Uri.from(scheme = schema, host = host, port = port).withPath(uriPath) |
| 50 | + val request = HttpRequest(uri = uri, headers = Seq(`Accept-Encoding`(HttpEncodings.gzip), Accept(MediaRange(MediaTypes.`application/json`)))) |
| 51 | + |
| 52 | + log.info("Requesting seed nodes by: {}", request.uri) |
| 53 | + |
| 54 | + for { |
| 55 | + response <- http.singleRequest(request) |
| 56 | + entity <- response.entity.toStrict(resolveTimeout) |
| 57 | + response <- { |
| 58 | + log.debug("Eureka response: [{}]", entity.data.utf8String) |
| 59 | + val unmarshalled = Unmarshal(entity).to[EurekaResponse] |
| 60 | + unmarshalled.failed.foreach { _ => |
| 61 | + log.error( |
| 62 | + "Failed to unmarshal Eureka response status [{}], entity: [{}], uri: [{}]", |
| 63 | + response.status.value, |
| 64 | + entity.data.utf8String, |
| 65 | + uri) |
| 66 | + } |
| 67 | + unmarshalled |
| 68 | + } |
| 69 | + instances <- pick(response.application.instance) |
| 70 | + } yield Resolved(lookup.serviceName, targets(instances)) |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + private[eureka] def pick(instances: Seq[EurekaResponse.Instance]): Future[Seq[EurekaResponse.Instance]] = { |
| 75 | + Future.successful(instances.collect({ |
| 76 | + case instance if instance.status == "UP" && instance.appGroupName == group => instance |
| 77 | + })) |
| 78 | + } |
| 79 | + |
| 80 | + private[eureka] def targets(instances: Seq[EurekaResponse.Instance]): Seq[ResolvedTarget] = { |
| 81 | + instances.map { instance => |
| 82 | + ResolvedTarget( |
| 83 | + host = instance.ipAddr, |
| 84 | + port = Some(instance.port.port), |
| 85 | + address = Try(InetAddress.getByName(instance.ipAddr)).toOption) |
| 86 | + } |
| 87 | + } |
29 | 88 |
|
30 | | - override def lookup(lookup: Lookup, resolveTimeout: FiniteDuration): Future[ServiceDiscovery.Resolved] = ??? |
31 | 89 | } |
0 commit comments