Docs: Discovery via DNS (#29153)
This commit is contained in:
parent
84b1369238
commit
7e69b990b9
6 changed files with 113 additions and 19 deletions
|
|
@ -7,9 +7,7 @@ package akka.discovery.dns
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
|
|
||||||
import scala.concurrent.duration._
|
import scala.concurrent.duration._
|
||||||
|
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory
|
||||||
|
|
||||||
import akka.actor.ActorSystem
|
import akka.actor.ActorSystem
|
||||||
import akka.discovery.{ Discovery, Lookup }
|
import akka.discovery.{ Discovery, Lookup }
|
||||||
import akka.discovery.ServiceDiscovery
|
import akka.discovery.ServiceDiscovery
|
||||||
|
|
@ -20,13 +18,11 @@ import akka.testkit.{ AkkaSpec, SocketUtil, TestKit }
|
||||||
object DnsDiscoverySpec {
|
object DnsDiscoverySpec {
|
||||||
|
|
||||||
val config = ConfigFactory.parseString(s"""
|
val config = ConfigFactory.parseString(s"""
|
||||||
//#configure-dns
|
|
||||||
akka {
|
akka {
|
||||||
discovery {
|
discovery {
|
||||||
method = akka-dns
|
method = akka-dns
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#configure-dns
|
|
||||||
akka {
|
akka {
|
||||||
loglevel = DEBUG
|
loglevel = DEBUG
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,12 @@ Port can be used when a service opens multiple ports e.g. a HTTP port and an Akk
|
||||||
|
|
||||||
## Discovery Method: DNS
|
## Discovery Method: DNS
|
||||||
|
|
||||||
|
@@@ note { title="Async DNS" }
|
||||||
|
|
||||||
|
Akka Discovery with DNS does always use the @ref[Akka-native "async-dns" implementation](../io-dns.md) (it is independent of the `akka.io.dns.resolver` setting).
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
DNS discovery maps `Lookup` queries as follows:
|
DNS discovery maps `Lookup` queries as follows:
|
||||||
|
|
||||||
* `serviceName`, `portName` and `protocol` set: SRV query in the form: `_port._protocol.name` Where the `_`s are added.
|
* `serviceName`, `portName` and `protocol` set: SRV query in the form: `_port._protocol.name` Where the `_`s are added.
|
||||||
|
|
@ -86,27 +92,15 @@ The mapping between Akka service discovery terminology and SRV terminology:
|
||||||
|
|
||||||
Configure `akka-dns` to be used as the discovery implementation in your `application.conf`:
|
Configure `akka-dns` to be used as the discovery implementation in your `application.conf`:
|
||||||
|
|
||||||
@@snip[application.conf](/akka-discovery/src/test/scala/akka/discovery/dns/DnsDiscoverySpec.scala){ #configure-dns }
|
@@snip[application.conf](/akka-docs/src/test/scala/docs/discovery/DnsDiscoveryDocSpec.scala){ #configure-dns }
|
||||||
|
|
||||||
From there on, you can use the generic API that hides the fact which discovery method is being used by calling:
|
From there on, you can use the generic API that hides the fact which discovery method is being used by calling:
|
||||||
|
|
||||||
Scala
|
Scala
|
||||||
: ```scala
|
: @@snip[snip](/akka-docs/src/test/scala/docs/discovery/DnsDiscoveryDocSpec.scala){ #lookup-dns }
|
||||||
import akka.discovery.ServiceDiscovery
|
|
||||||
val system = ActorSystem("Example")
|
|
||||||
// ...
|
|
||||||
val discovery = ServiceDiscovery(system).discovery
|
|
||||||
val result: Future[Resolved] = discovery.lookup("service-name", resolveTimeout = 500 milliseconds)
|
|
||||||
```
|
|
||||||
|
|
||||||
Java
|
Java
|
||||||
: ```java
|
: @@snip[snip](/akka-docs/src/test/java/jdocs/discovery/DnsDiscoveryDocTest.java){ #lookup-dns }
|
||||||
import akka.discovery.ServiceDiscovery;
|
|
||||||
ActorSystem system = ActorSystem.create("Example");
|
|
||||||
// ...
|
|
||||||
SimpleServiceDiscovery discovery = ServiceDiscovery.get(system).discovery();
|
|
||||||
Future<SimpleServiceDiscovery.Resolved> result = discovery.lookup("service-name", Duration.create("500 millis"));
|
|
||||||
```
|
|
||||||
|
|
||||||
### DNS records used
|
### DNS records used
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,12 @@ Users should pick one of the built in extensions.
|
||||||
Akka DNS is a pluggable way to interact with DNS. Implementations much implement `akka.io.DnsProvider` and provide a configuration
|
Akka DNS is a pluggable way to interact with DNS. Implementations much implement `akka.io.DnsProvider` and provide a configuration
|
||||||
block that specifies the implementation via `provider-object`.
|
block that specifies the implementation via `provider-object`.
|
||||||
|
|
||||||
|
@@@ note { title="DNS via Akka Discovery" }
|
||||||
|
|
||||||
|
@ref[Akka Discovery](discovery/index.md) can be backed by the Akka DNS implementation and provides a more general API for service lookups which is not limited to domain name lookup.
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
To select which `DnsProvider` to use set `akka.io.dns.resolver ` to the location of the configuration.
|
To select which `DnsProvider` to use set `akka.io.dns.resolver ` to the location of the configuration.
|
||||||
|
|
||||||
There are currently two implementations:
|
There are currently two implementations:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package jdocs.discovery;
|
||||||
|
|
||||||
|
import akka.actor.ActorSystem;
|
||||||
|
// #lookup-dns
|
||||||
|
import akka.discovery.Discovery;
|
||||||
|
import akka.discovery.ServiceDiscovery;
|
||||||
|
// #lookup-dns
|
||||||
|
import akka.testkit.javadsl.TestKit;
|
||||||
|
import docs.discovery.DnsDiscoveryDocSpec;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.scalatestplus.junit.JUnitSuite;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class DnsDiscoveryDocTest extends JUnitSuite {
|
||||||
|
|
||||||
|
private static ActorSystem system;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setup() {
|
||||||
|
system = ActorSystem.create("LeaseDocTest", DnsDiscoveryDocSpec.config());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void teardown() {
|
||||||
|
TestKit.shutdownActorSystem(system);
|
||||||
|
system = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dnsDiscoveryShouldResolveAkkaIo() throws Exception {
|
||||||
|
// #lookup-dns
|
||||||
|
|
||||||
|
ServiceDiscovery discovery = Discovery.get(system).discovery();
|
||||||
|
// ...
|
||||||
|
CompletionStage<ServiceDiscovery.Resolved> result =
|
||||||
|
discovery.lookup("akka.io", Duration.ofMillis(500));
|
||||||
|
// #lookup-dns
|
||||||
|
ServiceDiscovery.Resolved resolved =
|
||||||
|
result.toCompletableFuture().get(500, TimeUnit.MILLISECONDS);
|
||||||
|
assertFalse(resolved.getAddresses().isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2020 Lightbend Inc. <https://www.lightbend.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package docs.discovery
|
||||||
|
|
||||||
|
import akka.testkit.AkkaSpec
|
||||||
|
import com.typesafe.config.ConfigFactory
|
||||||
|
|
||||||
|
import scala.concurrent.duration._
|
||||||
|
import scala.concurrent.Future
|
||||||
|
|
||||||
|
object DnsDiscoveryDocSpec {
|
||||||
|
val config = ConfigFactory.parseString("""
|
||||||
|
// #configure-dns
|
||||||
|
akka {
|
||||||
|
discovery {
|
||||||
|
method = akka-dns
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// #configure-dns
|
||||||
|
""")
|
||||||
|
}
|
||||||
|
|
||||||
|
class DnsDiscoveryDocSpec extends AkkaSpec(DnsDiscoveryDocSpec.config) {
|
||||||
|
|
||||||
|
"DNS Discovery" should {
|
||||||
|
"find akka.io" in {
|
||||||
|
// #lookup-dns
|
||||||
|
import akka.discovery.Discovery
|
||||||
|
import akka.discovery.ServiceDiscovery
|
||||||
|
|
||||||
|
val discovery: ServiceDiscovery = Discovery(system).discovery
|
||||||
|
// ...
|
||||||
|
val result: Future[ServiceDiscovery.Resolved] = discovery.lookup("akka.io", resolveTimeout = 500.millis)
|
||||||
|
// #lookup-dns
|
||||||
|
val resolved = result.futureValue
|
||||||
|
resolved.serviceName shouldBe "akka.io"
|
||||||
|
resolved.addresses shouldNot be(Symbol("empty"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -202,6 +202,7 @@ lazy val docs = akkaModule("akka-docs")
|
||||||
actorTyped,
|
actorTyped,
|
||||||
clusterTools % "compile->compile;test->test",
|
clusterTools % "compile->compile;test->test",
|
||||||
clusterSharding % "compile->compile;test->test",
|
clusterSharding % "compile->compile;test->test",
|
||||||
|
discovery % "compile->compile;test->test",
|
||||||
testkit % "compile->compile;test->test",
|
testkit % "compile->compile;test->test",
|
||||||
remote % "compile->compile;test->test",
|
remote % "compile->compile;test->test",
|
||||||
persistence % "compile->compile;test->test",
|
persistence % "compile->compile;test->test",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue