Springboot 2.3.1.RELEASE 和 Cassandra 连接问题
Springboot 2.3.1.RELEASE and Cassandra connectivity issue
我在使用最新的 springboot 版本连接到 CASSANDRA 时遇到身份验证异常。
它正在使用订单版本。在较新版本的 data-cassandra 中引入了很多变化。请找到我正在使用的以下配置。
Springboot 版本:2.3.1.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
卡桑德拉配置:
@Configuration
@EnableCassandraRepositories
public class CassandraConfig 扩展了 AbstractCassandraConfiguration {
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Value("${spring.data.cassandra.port}")
private int port;
@Value("${spring.data.cassandra.keyspace-name}")
private String keySpace;
@Value("${spring.data.cassandra.username}")
private String username;
@Value("${spring.data.cassandra.password}")
private String password;
@Override
protected String getContactPoints() {
return contactPoints;
}
@Override
protected int getPort() {
return port;
}
@Override
protected CqlSession getRequiredSession() {
// TODO Auto-generated method stub
List<InetSocketAddress> hostList = new ArrayList<>();
Stream.of(contactPoints.split(",")).collect(Collectors.toList()).forEach(host->
hostList.add(new InetSocketAddress(host, port))
);
return CqlSession.builder()
.addContactPoints(hostList)
.withAuthCredentials(username, password)
.withKeyspace(keySpace)
.withLocalDatacenter("local")
.build();
}
@Override
protected String getKeyspaceName() {
return keySpace;
}
}
启动时出现以下错误
requires authenti cation (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.lambda$buildAuthenticator(ProtocolInitHandler.java:354) at java.util.Optional.orElseThrow(Optional.java:290) at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.buildAuthenticator(ProtocolInitHandler.java:350) at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.onResponse(ProtocolInitHandler.java:204) at com.datastax.oss.driver.internal.core.channel.ChannelHandlerRequest.onResponse(ChannelHandlerRequest.java:94) at com.datastax.oss.driver.internal.core.channel.InFlightHandler.channelRead(InFlightHandler.java:255) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748)
它与 Springboot 版本 2.2 完美配合。4.RELEASE 版本
您可能正在点击 this issue,尽管该问题已在 2.3.1 中标记为已解决。尝试使用 2.4.0,或者尝试以下解决方法:
@Bean
CqlSessionBuilderCustomizer authCustomizer(CassandraProperties properties) {
return (builder) -> builder.withAuthCredentials(
properties.getUsername(), properties.getPassword());
}
最后我删除了所有自定义配置 class 并仅在 application.yml 中保留了以下配置并且它起作用了。
spring:
profiles: dev
data:
cassandra:
keyspace-name:
contact-points:
port:
username:
password:
localDatacenter:
我的仓库如下所示:
@Repository
public interface MyRepo extends CassandraRepository<MyEntity,String>
我在使用最新的 springboot 版本连接到 CASSANDRA 时遇到身份验证异常。 它正在使用订单版本。在较新版本的 data-cassandra 中引入了很多变化。请找到我正在使用的以下配置。
Springboot 版本:2.3.1.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
卡桑德拉配置:
@Configuration
@EnableCassandraRepositories public class CassandraConfig 扩展了 AbstractCassandraConfiguration {
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Value("${spring.data.cassandra.port}")
private int port;
@Value("${spring.data.cassandra.keyspace-name}")
private String keySpace;
@Value("${spring.data.cassandra.username}")
private String username;
@Value("${spring.data.cassandra.password}")
private String password;
@Override
protected String getContactPoints() {
return contactPoints;
}
@Override
protected int getPort() {
return port;
}
@Override
protected CqlSession getRequiredSession() {
// TODO Auto-generated method stub
List<InetSocketAddress> hostList = new ArrayList<>();
Stream.of(contactPoints.split(",")).collect(Collectors.toList()).forEach(host->
hostList.add(new InetSocketAddress(host, port))
);
return CqlSession.builder()
.addContactPoints(hostList)
.withAuthCredentials(username, password)
.withKeyspace(keySpace)
.withLocalDatacenter("local")
.build();
}
@Override
protected String getKeyspaceName() {
return keySpace;
}
}
启动时出现以下错误
requires authenti cation (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.lambda$buildAuthenticator(ProtocolInitHandler.java:354) at java.util.Optional.orElseThrow(Optional.java:290) at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.buildAuthenticator(ProtocolInitHandler.java:350) at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.onResponse(ProtocolInitHandler.java:204) at com.datastax.oss.driver.internal.core.channel.ChannelHandlerRequest.onResponse(ChannelHandlerRequest.java:94) at com.datastax.oss.driver.internal.core.channel.InFlightHandler.channelRead(InFlightHandler.java:255) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748)
它与 Springboot 版本 2.2 完美配合。4.RELEASE 版本
您可能正在点击 this issue,尽管该问题已在 2.3.1 中标记为已解决。尝试使用 2.4.0,或者尝试以下解决方法:
@Bean
CqlSessionBuilderCustomizer authCustomizer(CassandraProperties properties) {
return (builder) -> builder.withAuthCredentials(
properties.getUsername(), properties.getPassword());
}
最后我删除了所有自定义配置 class 并仅在 application.yml 中保留了以下配置并且它起作用了。
spring:
profiles: dev
data:
cassandra:
keyspace-name:
contact-points:
port:
username:
password:
localDatacenter:
我的仓库如下所示:
@Repository
public interface MyRepo extends CassandraRepository<MyEntity,String>