| 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 [AddressTranslater][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 MyAddressTranslater implements AddressTranslater { |
| 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 | .withAddressTranslater(new MyAddressTranslater()) |
| 28 | .build(); |
| 29 | ``` |
| 30 | |
| 31 | Notes: |
| 32 | |
| 33 | * the contact points provided while creating the `Cluster` are not translated, only addresses |
| 34 | retrieved from or sent by Cassandra nodes are; |
| 35 | * you might want to implement [CloseableAddressTranslater][cat] if your |
| 36 | implementation has state that should be cleaned up when the `Cluster` |
| 37 | shuts down. This is provided as a separate interface for historical |
| 38 | reasons, the `close()` method will be merged in `AddressTranslater` in a |
| 39 | future release. |
| 40 | |
| 41 | [at]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/policies/AddressTranslater.html |
| 42 | [cat]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/policies/CloseableAddressTranslater.html |
| 43 | |
| 44 | ### EC2 multi-region |
| 45 | |
| 46 | [EC2MultiRegionAddressTranslater][ec2] is provided out of the box. It |
| 47 | helps optimize network costs when your infrastructure (both Cassandra |
| 48 | nodes *and* clients) is distributed across multiple Amazon EC2 regions: |
| 49 | |
| 50 | * a client communicating with a Cassandra node *in the same EC2 region* should use the node's |
| 51 | private IP (which is less expensive); |
| 52 | * a client communicating with a node in a different region should use the public IP. |
| 53 | |
| 54 | To use this implementation, provide an instance when initializing the `Cluster`: |
| 55 | |
| 56 | ```java |
| 57 | Cluster cluster = Cluster.builder() |
| 58 | .addContactPoint("1.2.3.4") |
| 59 | .withAddressTranslater(new EC2MultiRegionAddressTranslater()) |
| 60 | .build(); |
| 61 | ``` |
| 62 | |
| 63 | This class performs a reverse DNS lookup of the origin address, to find the domain name of the |
| 64 | target instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the |
| 65 | private/public switch automatically based on location. |
| 66 | |
| 67 | [ec2]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/policies/EC2MultiRegionAddressTranslater.html |