在 cassandra 中使用地址转换映射时如何进行端口转换?
How to do port translation when using an address translation map with cassandra?
我正在尝试使用地址转换映射连接到 scylladb 集群。
如果在实例化集群实例时对端口进行硬编码,我只能让代码工作:
from cassandra.cluster import Cluster
from cassandra.policies import AddressTranslator
from cassandra.auth import PlainTextAuthProvider
################################################################################
# our variables
address_map = {
"10.0.24.69:9042": "sl-eu-lon-2-portal.3.dblayer.com:15227",
"10.0.24.71:9042": "sl-eu-lon-2-portal.2.dblayer.com:15229",
"10.0.24.70:9042": "sl-eu-lon-2-portal.1.dblayer.com:15228"
}
username = 'scylla'
password = 'changeme'
port = 15227
################################################################################
下一个 class 用于翻译地址:
class ComposeAddressTranslator(AddressTranslator):
def set_map(self, address_map):
# strip ports from both source and destination as the cassandra python
# client doesn't appear to support ports translation
self.address_map = {key.split(':')[0]: value.split(':')[0] for (key, value) in address_map.items()}
def contact_points(self):
return [value.split(':')[0] for (key, value) in address_map.items()]
def translate(self, addr):
# print some debug output
print('in translate(self, addr) method', type(addr), addr)
trans_addr = self.address_map[addr]
return trans_addr
现在开始连接:
compose_translator = ComposeAddressTranslator()
compose_translator.set_map(address_map)
auth_provider = PlainTextAuthProvider(
username=username,
password=password
)
# if the port parameter value is removed from below, we are unable
# to establish a connection
cluster = Cluster(
contact_points = compose_translator.contact_points(),
address_translator = compose_translator,
auth_provider = auth_provider,
cql_version = '3.2.1',
protocol_version = 2,
port = port
)
session = cluster.connect()
session.execute("USE my_keyspace;")
session.shutdown()
cassandra python 库似乎不支持 translate method 的端口转换?您可以在下面的调试输出中看到,传递给 translate 方法的 addr 是一个不带端口的字符串 ip 地址值:
in translate(self, addr) method <class 'str'> 10.0.24.69
in translate(self, addr) method <class 'str'> 10.0.24.71
我的环境:
$ pip freeze | grep cassandra
cassandra-driver==3.10
$ pip freeze | grep cassandra
cassandra-driver==3.10
其他 Cassandra 驱动程序如节点驱动程序支持端口转换。 nodejs translator documentation:
MyAddressTranslator.prototype.translate = function (address, port, callback) {
// Your custom translation logic.
};
上面可以看到翻译器同时收到了ip地址和端口
不过,我不相信目前的Cassandra python驱动支持端口地址translation:
translate(addr)
Accepts the node ip address, and returns a translated address to be used connecting to this node.
这里可以看到翻译器只接收到ip地址
我正在尝试使用地址转换映射连接到 scylladb 集群。
如果在实例化集群实例时对端口进行硬编码,我只能让代码工作:
from cassandra.cluster import Cluster
from cassandra.policies import AddressTranslator
from cassandra.auth import PlainTextAuthProvider
################################################################################
# our variables
address_map = {
"10.0.24.69:9042": "sl-eu-lon-2-portal.3.dblayer.com:15227",
"10.0.24.71:9042": "sl-eu-lon-2-portal.2.dblayer.com:15229",
"10.0.24.70:9042": "sl-eu-lon-2-portal.1.dblayer.com:15228"
}
username = 'scylla'
password = 'changeme'
port = 15227
################################################################################
下一个 class 用于翻译地址:
class ComposeAddressTranslator(AddressTranslator):
def set_map(self, address_map):
# strip ports from both source and destination as the cassandra python
# client doesn't appear to support ports translation
self.address_map = {key.split(':')[0]: value.split(':')[0] for (key, value) in address_map.items()}
def contact_points(self):
return [value.split(':')[0] for (key, value) in address_map.items()]
def translate(self, addr):
# print some debug output
print('in translate(self, addr) method', type(addr), addr)
trans_addr = self.address_map[addr]
return trans_addr
现在开始连接:
compose_translator = ComposeAddressTranslator()
compose_translator.set_map(address_map)
auth_provider = PlainTextAuthProvider(
username=username,
password=password
)
# if the port parameter value is removed from below, we are unable
# to establish a connection
cluster = Cluster(
contact_points = compose_translator.contact_points(),
address_translator = compose_translator,
auth_provider = auth_provider,
cql_version = '3.2.1',
protocol_version = 2,
port = port
)
session = cluster.connect()
session.execute("USE my_keyspace;")
session.shutdown()
cassandra python 库似乎不支持 translate method 的端口转换?您可以在下面的调试输出中看到,传递给 translate 方法的 addr 是一个不带端口的字符串 ip 地址值:
in translate(self, addr) method <class 'str'> 10.0.24.69
in translate(self, addr) method <class 'str'> 10.0.24.71
我的环境:
$ pip freeze | grep cassandra
cassandra-driver==3.10
$ pip freeze | grep cassandra
cassandra-driver==3.10
其他 Cassandra 驱动程序如节点驱动程序支持端口转换。 nodejs translator documentation:
MyAddressTranslator.prototype.translate = function (address, port, callback) {
// Your custom translation logic.
};
上面可以看到翻译器同时收到了ip地址和端口
不过,我不相信目前的Cassandra python驱动支持端口地址translation:
translate(addr)
Accepts the node ip address, and returns a translated address to be used connecting to this node.
这里可以看到翻译器只接收到ip地址