我已经阅读并尝试了许多关于这个问题的解决方案,但仍然不适用于我。我得到以下例外: Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /1.2.3.4:9042 (com.datastax.driver.core.exceptions.TransportException:[/1.2.3.4:9042] Channel has been closed))
我已将驱动程序更新到最新版本。cassandra.yaml配置如下: start_native_transport: true native_transport_port: 9042 rpc_address: 0.0.0.0
我可以从datastax devcenter连接到节点,但不能从java连接到节点。我不确定问题出在哪里。
这是我在java中使用的代码。
public class ClientToNodeConnection {
public static Logger logger =
LoggerFactory.getLogger(ClientToNodeConnection.class);
public static void main(String[] args) {
ClientToNodeConnection l = new ClientToNodeConnection();
l.execute();
}
private void execute() {
Cluster cluster;
Session session;
logger.info("Login into {}");
cluster = getCluster("/client-keystore.jks", "keypass", "1.2.3.4");
// Connect to the cluster and keyspace "ssl"
session = cluster.newSession();
session.execute(" CREATE KEYSPACE IF NOT EXISTS ssl WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};");
session.execute("use ssl;");
session.execute("CREATE TABLE IF NOT EXISTS songs (title text, lyrics text, PRIMARY KEY (title))");
// Insert one record into the users table
session.execute("INSERT INTO songs (title, lyrics) VALUES ('Don''t believe the hype', 'Caught you lookin for the same thing It''s a new thing check out this I bring')");
// Use select to get the user we just entered
ResultSet results = session.execute("SELECT * FROM songs Limit 200;");
for (Row row : results) {
logger.info("***data extracted***");
logger.info("{} - {}", row.getString("title"), row.getString("lyrics"));
}
session.close();
// Clean up the connection by closing it
cluster.close();
}
// This method is an example of loading a truststore from a resource, decoding it with its password.
private Cluster getCluster(String trustStoreLocation, String trustStorePassword, String host) {
Cluster cluster;
SSLContext sslcontext = null;
try {
InputStream is = ClientToNode.class.getResourceAsStream(trustStoreLocation);
KeyStore keystore = KeyStore.getInstance("jks");
char[] pwd = trustStorePassword.toCharArray();
keystore.load(is, pwd);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keystore);
TrustManager[] tm = tmf.getTrustManagers();
sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, tm, null);
} catch (KeyStoreException kse) {
logger.error(kse.getMessage(), kse);
} catch (CertificateException e) {
logger.error(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
logger.error(e.getMessage(), e);
} catch (KeyManagementException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
JdkSSLOptions sslOptions = JdkSSLOptions.builder()
.withSSLContext(sslcontext)
.build();
cluster = Cluster.builder()
.withClusterName("cassandra")
.withCredentials("casss", "cass")
.addContactPoint(host)
.withPort(9042)
.withProtocolVersion(ProtocolVersion.V3)
.withSSL(sslOptions)
.build();
return cluster;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!