gpt4 book ai didi

cassandra - Spring 数据 : Connect to Cassandra via SSL

转载 作者:行者123 更新时间:2023-12-02 04:24:15 25 4
gpt4 key购买 nike

我想通过 SSL 从使用 Spring Data 的 Java 应用程序连接到 Cassandra 集群。我们有以下成功连接到集群的脚本。基本上,它只启用 SSL 连接而不指定 SSL 证书。

mkdir .cassandra
echo "[ssl]" > .cassandra/cqlshrc
echo "validate = false" >> .cassandra/cqlshrc
cqlsh -u USER -p PASS -k KEYSPACE --debug --ssl HOSTNAME

我尝试通过 CassandraClusterFactoryBean 在 Spring @Configuration 中设置相同的连接选项。这是有问题的片段:

CassandraClusterFactoryBean factoryBean = new CassandraClusterFactoryBean();        
factoryBean.setContactPoints(contactPoint);
factoryBean.setPort(9042);
factoryBean.setQueryOptions(queryOptions);
factoryBean.setAuthProvider(cassandraAuthentication());
factoryBean.setSslEnabled(true);

cassandraAuthentication() 方法创建一个带有明文凭据的 AuthProvider。尝试连接到集群时出现以下异常:

Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: <hostname>:9042 (com.datastax.driver.core.exceptions.TransportException: [<hostname>:9042] Error writing))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:268) ~[cassandra-driver-core-3.6.0.jar!/:na]
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:107) ~[cassandra-driver-core-3.6.0.jar!/:na]
at com.datastax.driver.core.Cluster$Manager.negotiateProtocolVersionAndConnect(Cluster.java:1652) ~[cassandra-driver-core-3.6.0.jar!/:na]
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1571) ~[cassandra-driver-core-3.6.0.jar!/:na]
at com.datastax.driver.core.Cluster.init(Cluster.java:208) ~[cassandra-driver-core-3.6.0.jar!/:na]
at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:376) ~[cassandra-driver-core-3.6.0.jar!/:na]
at com.datastax.driver.core.Cluster.connect(Cluster.java:332) ~[cassandra-driver-core-3.6.0.jar!/:na]
at org.springframework.data.cassandra.config.CassandraCqlSessionFactoryBean.connect(CassandraCqlSessionFactoryBean.java:89) ~[spring-data-cassandra-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
at org.springframework.data.cassandra.config.CassandraCqlSessionFactoryBean.afterPropertiesSet(CassandraCqlSessionFactoryBean.java:82) ~[spring-data-cassandra-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
at org.springframework.data.cassandra.config.CassandraSessionFactoryBean.afterPropertiesSet(CassandraSessionFactoryBean.java:59) ~[spring-data-cassandra-2.1.3.RELEASE.jar!/:2.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
... 51 common frames omitted

我应该在集群配置中添加什么设置才能连接到数据库?谢谢

最佳答案

如果您使用的是 CassandraClusterFactoryBean,请使用以下代码,

@Bean
@Override
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
PlainTextAuthProvider sap = new PlainTextAuthProvider(env.getProperty("cassandra.username"), env.getProperty("cassandra.password"));
cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));
cluster.setAuthProvider(sap);
return cluster;
}

关于cassandra - Spring 数据 : Connect to Cassandra via SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56373261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com