Elffuss BraidHub

kikocis/java-driver public

a3674bcf4e77 · 2.4 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 [AddressTranslator][at] 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```java
19public class MyAddressTranslator implements AddressTranslator {
20 public InetSocketAddress translate(InetSocketAddress address) {
21 ... // your custom translation logic
22 }
23}
24
25Cluster cluster = Cluster.builder()
26 .addContactPoint("1.2.3.4")
27 .withAddressTranslator(new MyAddressTranslator())
28 .build();
29```
30
31Note: the contact points provided while creating the `Cluster` are not translated, only
32addresses retrieved from or sent by Cassandra nodes are.
33
34[at]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/AddressTranslator.html
35
36### EC2 multi-region
37
38[EC2MultiRegionAddressTranslator][ec2] is provided out of the box. It
39helps optimize network costs when your infrastructure (both Cassandra
40nodes *and* clients) is distributed across multiple Amazon EC2 regions:
41
42* a client communicating with a Cassandra node *in the same EC2 region* should use the node's
43 private IP (which is less expensive);
44* a client communicating with a node in a different region should use the public IP.
45
46To use this implementation, provide an instance when initializing the `Cluster`:
47
48```java
49Cluster cluster = Cluster.builder()
50 .addContactPoint("1.2.3.4")
51 .withAddressTranslator(new EC2MultiRegionAddressTranslator())
52 .build();
53```
54
55This class performs a reverse DNS lookup of the origin address, to find the domain name of the
56target instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the
57private/public switch automatically based on location.
58
59[ec2]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/EC2MultiRegionAddressTranslator.html