| 1 | ## Address resolution |
| 2 | |
| 3 | The driver auto-detects new Cassandra nodes added to the cluster through server |
| 4 | side push notifications and through checking the system tables. |
| 5 | |
| 6 | For 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. |
| 8 | However, sometimes the addresses received through this mechanism will either not be |
| 9 | reachable 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 |
| 11 | go through a router). |
| 12 | |
| 13 | ### The [AddressTranslator][at] interface |
| 14 | |
| 15 | This interface allows you to deal with such cases, by transforming the address sent by a |
| 16 | Cassandra node to another address to be used by the driver for connection. |
| 17 | |
| 18 | ```java |
| 19 | public class MyAddressTranslator implements AddressTranslator { |
| 20 | public InetSocketAddress translate(InetSocketAddress address) { |
| 21 | ... // your custom translation logic |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | Cluster cluster = Cluster.builder() |
| 26 | .addContactPoint("1.2.3.4") |
| 27 | .withAddressTranslator(new MyAddressTranslator()) |
| 28 | .build(); |
| 29 | ``` |
| 30 | |
| 31 | Note: the contact points provided while creating the `Cluster` are not translated, only |
| 32 | addresses 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 |
| 39 | helps optimize network costs when your infrastructure (both Cassandra |
| 40 | nodes *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 | |
| 46 | To use this implementation, provide an instance when initializing the `Cluster`: |
| 47 | |
| 48 | ```java |
| 49 | Cluster cluster = Cluster.builder() |
| 50 | .addContactPoint("1.2.3.4") |
| 51 | .withAddressTranslator(new EC2MultiRegionAddressTranslator()) |
| 52 | .build(); |
| 53 | ``` |
| 54 | |
| 55 | This class performs a reverse DNS lookup of the origin address, to find the domain name of the |
| 56 | target instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the |
| 57 | private/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 |