簡體   English   中英

嘗試使用Java驅動程序連接到Cassandra時出現AuthenticationException

[英]AuthenticationException when trying to connect to Cassandra with Java driver

我正在嘗試編寫一個Java代碼段,該代碼段在單獨的線程中啟動Cassandra,然后使用驅動程序進行連接並創建一些用戶。 我們有一個腳本為此目的使用基於Python的CQLSH客戶端,但是當我們將其中一台服務器升級到Ubuntu 16.04時遇到了問題。 顯然在Ubuntu上存在Python版本的不兼容性(在RHEL 7上有效,但在Ubuntu 16.04上無效)。 我什至嘗試在Ubuntu上降級Python版本,但仍然無法使用CQLSH客戶端,因此我求助於Java。 這些是Maven依賴項。 請注意,我嘗試使用默認的Guava版本(16.0),但是遇到了其他問題,因此我用17.0覆蓋了它。

[INFO] +- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- org.apache.cassandra:cassandra-all:jar:2.2.4:compile
[INFO] |  +- org.xerial.snappy:snappy-java:jar:1.1.1.7:compile
[INFO] |  +- net.jpountz.lz4:lz4:jar:1.3.0:compile
[INFO] |  +- com.ning:compress-lzf:jar:0.8.4:compile
[INFO] |  +- commons-cli:commons-cli:jar:1.1:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.9:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.3.2:compile
[INFO] |  +- org.apache.commons:commons-math3:jar:3.2:compile
[INFO] |  +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.4:compile
[INFO] |  +- org.antlr:antlr:jar:3.5.2:compile
[INFO] |  |  \- org.antlr:ST4:jar:4.0.8:compile
[INFO] |  +- org.antlr:antlr-runtime:jar:3.5.2:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] |  +- org.slf4j:log4j-over-slf4j:jar:1.7.7:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.5:compile
[INFO] |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.2:compile
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] |  +- com.googlecode.json-simple:json-simple:jar:1.1:compile
[INFO] |  +- com.boundary:high-scale-lib:jar:1.0.6:compile
[INFO] |  +- org.yaml:snakeyaml:jar:1.11:compile
[INFO] |  +- org.mindrot:jbcrypt:jar:0.3m:compile
[INFO] |  +- io.dropwizard.metrics:metrics-core:jar:3.1.0:compile
[INFO] |  +- com.addthis.metrics:reporter-config3:jar:3.0.0:compile
[INFO] |  |  +- com.addthis.metrics:reporter-config-base:jar:3.0.0:compile
[INFO] |  |  \- org.hibernate:hibernate-validator:jar:4.3.0.Final:compile
[INFO] |  |     +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] |  |     \- org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile
[INFO] |  +- com.thinkaurelius.thrift:thrift-server:jar:0.3.7:compile
[INFO] |  |  \- com.lmax:disruptor:jar:3.0.1:compile
[INFO] |  +- com.clearspring.analytics:stream:jar:2.5.2:compile
[INFO] |  |  \- it.unimi.dsi:fastutil:jar:6.5.7:compile
[INFO] |  +- net.sf.supercsv:super-csv:jar:2.1.0:compile
[INFO] |  +- ch.qos.logback:logback-core:jar:1.0.6:compile
[INFO] |  +- ch.qos.logback:logback-classic:jar:1.0.6:compile
[INFO] |  +- org.apache.thrift:libthrift:jar:0.9.2:compile
[INFO] |  |  +- org.apache.httpcomponents:httpclient:jar:4.2.5:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.2.4:compile
[INFO] |  +- org.apache.cassandra:cassandra-thrift:jar:2.2.4:compile
[INFO] |  +- net.java.dev.jna:jna:jar:4.0.0:compile
[INFO] |  +- com.github.jbellis:jamm:jar:0.3.0:compile
[INFO] |  +- com.github.tjake:crc32ex:jar:0.1.1:compile
[INFO] |  +- io.netty:netty-all:jar:4.0.23.Final:compile
[INFO] |  +- joda-time:joda-time:jar:2.4:compile
[INFO] |  \- org.fusesource:sigar:jar:1.6.4:compile
[INFO] +- com.google.guava:guava:jar:17.0:test
[INFO] +- com.datastax.cassandra:cassandra-driver-core:jar:3.1.2:compile
[INFO] |  +- io.netty:netty-handler:jar:4.0.37.Final:compile
[INFO] |  |  +- io.netty:netty-buffer:jar:4.0.37.Final:compile
[INFO] |  |  |  \- io.netty:netty-common:jar:4.0.37.Final:compile
[INFO] |  |  +- io.netty:netty-transport:jar:4.0.37.Final:compile
[INFO] |  |  \- io.netty:netty-codec:jar:4.0.37.Final:compile
[INFO] |  +- com.github.jnr:jnr-ffi:jar:2.0.7:compile
[INFO] |  |  +- com.github.jnr:jffi:jar:1.2.10:compile
[INFO] |  |  +- com.github.jnr:jffi:jar:native:1.2.10:runtime
[INFO] |  |  +- org.ow2.asm:asm:jar:5.0.3:compile
[INFO] |  |  +- org.ow2.asm:asm-commons:jar:5.0.3:compile
[INFO] |  |  +- org.ow2.asm:asm-util:jar:5.0.3:compile
[INFO] |  |  \- com.github.jnr:jnr-x86asm:jar:1.0.2:compile
[INFO] |  \- com.github.jnr:jnr-posix:jar:3.0.27:compile
[INFO] |     \- com.github.jnr:jnr-constants:jar:0.9.0:compile
[INFO] +- com.cisco.dascode:dascode-cassandra:zip:2.2.4:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

最初,我使用cassandra-unit來啟動服務器,但是遇到了很多其他問題,因此我最終只是從我的代碼中在單獨的線程中啟動服務器,如下所示:

System.setProperty("cassandra.config", "file:" + file.getAbsolutePath()); // Path to cassandra.yaml
System.setProperty("cassandra-foreground", "true");
System.setProperty("cassandra.native.epoll.enabled", "false");
...
cassandraDaemon = new CassandraDaemon();
cassandraDaemon.activate();

客戶端代碼如下所示:

Cluster cluster = new       Cluster.Builder().addContactPoints("localhost").withPort(9042).withCredentials("cassandra", "cassandra").build();
Session session = cluster.connect("system_auth");

我已經嘗試了使用keyspace和不使用keyspace的connect()調用,並且行為沒有差異。 當客戶端嘗試連接時,出現以下異常:

com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host localhost/127.0.0.1:9042: Username and/or password are incorrect
at com.datastax.driver.core.Connection$8.apply(Connection.java:393)
at com.datastax.driver.core.Connection$8.apply(Connection.java:362)

有趣的是,如果僅在Mac上本地啟動服務器部分,則可以使用CQLSH客戶端,將用戶名/密碼設置為“ cassandra” /“ cassandra”,並且身份驗證沒有任何問題。

我的cassandra.yaml文件如下所示:

cluster_name: 'Test Cluster'
num_tokens: 256
hinted_handoff_enabled: true
hinted_handoff_throttle_in_kb: 1024
max_hints_delivery_threads: 2
batchlog_replay_throttle_in_kb: 1024
authenticator: PasswordAuthenticator
authorizer: CassandraAuthorizer
role_manager: CassandraRoleManager
roles_validity_in_ms: 2000
permissions_validity_in_ms: 2000
partitioner: org.apache.cassandra.dht.Murmur3Partitioner
disk_failure_policy: stop
commit_failure_policy: stop
key_cache_size_in_mb:
key_cache_save_period: 14400
row_cache_size_in_mb: 0
row_cache_save_period: 0
counter_cache_size_in_mb:
counter_cache_save_period: 7200
commitlog_sync: periodic
commitlog_sync_period_in_ms: 10000
commitlog_segment_size_in_mb: 32
seed_provider:
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
    parameters:
        - seeds: "127.0.0.1"
concurrent_reads: 32
concurrent_writes: 32
concurrent_counter_writes: 32
memtable_allocation_type: heap_buffers
index_summary_capacity_in_mb:
index_summary_resize_interval_in_minutes: 60
trickle_fsync: false
trickle_fsync_interval_in_kb: 10240
storage_port: 7000
ssl_storage_port: 7001
listen_address: localhost
start_native_transport: true
native_transport_port: 9042
start_rpc: false
rpc_address: localhost
rpc_port: 9160
rpc_keepalive: true
rpc_server_type: sync
thrift_framed_transport_size_in_mb: 15
incremental_backups: false
snapshot_before_compaction: false
auto_snapshot: true
tombstone_warn_threshold: 1000
tombstone_failure_threshold: 100000
column_index_size_in_kb: 64
batch_size_warn_threshold_in_kb: 5
batch_size_fail_threshold_in_kb: 50
compaction_throughput_mb_per_sec: 16
compaction_large_partition_warning_threshold_mb: 100
sstable_preemptive_open_interval_in_mb: 50

任何有關我可能在做錯事的提示都將不勝感激。

我不確定這是否有幫助,但是如果在創建CassandraDaemon之前添加以下內容,可能不會立即出現cassandra用戶:

System.setProperty("cassandra.superuser_setup_delay_ms", "0");

這可能會解決您的問題。 該值默認為10000(10秒)( source ),這說明了為什么睡眠20秒可能會解決您的問題。

我想這是比賽條件。 客戶端嘗試連接時,服務器尚未完全初始化。 我放了一個Thread.sleep(20000); 在嘗試連接之前的代碼中,該方法有效。 有沒有更好的方法來驗證服務器是否已完全初始化?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM