Elffuss BraidHub

kikocis/java-driver public

0a0514fbf2ff · 2.5 KiBRenderedRawPermalink
1## Address resolution
2
3The driver auto-detects new Cassandra nodes added to the cluster through server
4side push notifications and through checking the system tables.
5
6For each node, the address the driver will receive will correspond to the address set as
7`rpc_address` in the node's `cassandra.yaml` file. In most cases, this is the correct value.
8However, sometimes the addresses received through this mechanism will either not be
9reachable directly by the driver, or should not be the preferred address to use
10(for instance, it might be a private IP, but some clients may have to use a public IP, or
11go through a router).
12
13### The `AddressTranslater` interface
14
15This interface allows you to deal with such cases, by transforming the address sent by a
16Cassandra node to another address to be used by the driver for connection.
17
18 public class MyAddressTranslater implements AddressTranslater {
19 public InetSocketAddress translate(InetSocketAddress address) {
20 ... // your custom translation logic
21 }
22 }
23
24 Cluster cluster = Cluster.builder()
25 .addContactPoint("1.2.3.4")
26 .withAddressTranslater(new MyAddressTranslater())
27 .build();
28
29Notes:
30
31* the contact points provided while creating the `Cluster` are not translated, only addresses
32 retrieved from or sent by Cassandra nodes are;
33* you might want to implement `CloseableAddressTranslater` if your implementation has state that
34 should be cleaned up when the `Cluster` shuts down. This is provided as a separate interface for
35 historical reasons, the `close()` method will be merged in `AddressTranslater` in a future
36 release.
37
38
39### EC2 multi-region
40
41`EC2MultiRegionAddressTranslater` is provided out of the box. It helps optimize network costs when
42your infrastructure (both Cassandra nodes *and* clients) is distributed across multiple Amazon EC2
43regions:
44
45* a client communicating with a Cassandra node *in the same EC2 region* should use the node's
46 private IP (which is less expensive);
47* a client communicating with a node in a different region should use the public IP.
48
49To use this implementation, provide an instance when initializing the `Cluster`:
50
51 Cluster cluster = Cluster.builder()
52 .addContactPoint("1.2.3.4")
53 .withAddressTranslater(new EC2MultiRegionAddressTranslater())
54 .build();
55
56This class performs a reverse DNS lookup of the origin address, to find the domain name of the
57target instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the
58private/public switch automatically based on location.