如何在 Spring 引导中使用属性文件添加 Cassandra MaxRequestsPerConnection
How to add Cassandra MaxRequestsPerConnection using properties file in Spring boot
我有一个 Spring 引导项目,我在其中使用 Cassandra 作为数据库。
目前,我正在通过自动连接获取 Cassandra 实例 CassandraOperations
。
我的问题是:
我们如何使用 属性 文件设置 MaxRequestsPerConnection
?
# spring.data.cassandra.keyspace-name=event
# spring.data.cassandra.contact-points=localhost
# spring.data.cassandra.port=9042
目前,我的 属性 文件中有这些属性,但我没有找到任何 属性 用于设置 MaxRequestsPerConnection
Spring Boot 不提供所有属性的配置。您可以定义一个 ClusterBuilderCustomizer
bean 来自定义 Cluster
个实例。
尝试使用以下代码来声明一个自定义程序 bean,该 bean 获取可以通过属性文件提供的注入属性(更一般地说,Spring Boot 可用的任何 属性 源):
@Configuration
public class MyConfiguration {
@Bean
ClusterBuilderCustomizer clusterBuilderCustomizer(
@Value("${spring.data.cassandra.pool.max-requests-local:10}") int local,
@Value("${spring.data.cassandra.pool.max-requests-remote:5}") int remote) {
PoolingOptions options = new PoolingOptions();
options.setMaxRequestsPerConnection(HostDistance.LOCAL, local);
options.setMaxRequestsPerConnection(HostDistance.REMOTE, remote);
return builder -> builder.withPoolingOptions(options);
}
}
@Value
的替代方法是使用配置 class(用 @ConfigurationProperties
注释,它为您提供 IDE 支持(例如 属性-name auto -完成)。
步骤编号:1
在 application.properties 文件中,我们必须声明本地和远程池大小(所需大小值)
# spring.data.cassandra.keyspace-name=event
# spring.data.cassandra.contact-points=localhost
# spring.data.cassandra.port=9042
# spring.data.cassandra.pool.max-requests-local:20
# spring.data.cassandra.pool.max-requests-remote:10
步骤No:2
在 Bean 配置中:
@豆子
ClusterBuilderCustomizer 请使用以下代码(使用@value 注释)获取值:
@Value("${spring.data.cassandra.pool.max-requests-local}")
private int localPool;
@Value("${spring.data.cassandra.pool.max-requests-remote}")
private int remotePool;
通过使用此 PoolingOptions class 为本地和远程设置 setMaxRequestsPerConnections
HostDistance.LOCAL -- localPool
HostDistance.REMOTE -- remotePool
根据 Spring Boot 2.3.0 release notes, ClusterBuilderCustomizer has been replaced with DriverConfigLoaderBuilderCustomizer and CqlSessionBuilderCustomizer. As said in ,您只需声明两个具有这些类型的 bean:
@Bean
public CqlSessionBuilderCustomizer cqlSessionBuilderCustomizer() {
return cqlSessionBuilder -> cqlSessionBuilder
.withNodeStateListener(new MyNodeStateListener())
.withSchemaChangeListener(new MySchemChangeListener());
}
@Bean
public DriverConfigLoaderBuilderCustomizer driverConfigLoaderBuilderCustomizer() {
return loaderBuilder -> loaderBuilder
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(10));
}
}
我有一个 Spring 引导项目,我在其中使用 Cassandra 作为数据库。
目前,我正在通过自动连接获取 Cassandra 实例 CassandraOperations
。
我的问题是:
我们如何使用 属性 文件设置 MaxRequestsPerConnection
?
# spring.data.cassandra.keyspace-name=event
# spring.data.cassandra.contact-points=localhost
# spring.data.cassandra.port=9042
目前,我的 属性 文件中有这些属性,但我没有找到任何 属性 用于设置 MaxRequestsPerConnection
Spring Boot 不提供所有属性的配置。您可以定义一个 ClusterBuilderCustomizer
bean 来自定义 Cluster
个实例。
尝试使用以下代码来声明一个自定义程序 bean,该 bean 获取可以通过属性文件提供的注入属性(更一般地说,Spring Boot 可用的任何 属性 源):
@Configuration
public class MyConfiguration {
@Bean
ClusterBuilderCustomizer clusterBuilderCustomizer(
@Value("${spring.data.cassandra.pool.max-requests-local:10}") int local,
@Value("${spring.data.cassandra.pool.max-requests-remote:5}") int remote) {
PoolingOptions options = new PoolingOptions();
options.setMaxRequestsPerConnection(HostDistance.LOCAL, local);
options.setMaxRequestsPerConnection(HostDistance.REMOTE, remote);
return builder -> builder.withPoolingOptions(options);
}
}
@Value
的替代方法是使用配置 class(用 @ConfigurationProperties
注释,它为您提供 IDE 支持(例如 属性-name auto -完成)。
步骤编号:1 在 application.properties 文件中,我们必须声明本地和远程池大小(所需大小值)
# spring.data.cassandra.keyspace-name=event
# spring.data.cassandra.contact-points=localhost
# spring.data.cassandra.port=9042
# spring.data.cassandra.pool.max-requests-local:20
# spring.data.cassandra.pool.max-requests-remote:10
步骤No:2
在 Bean 配置中:
@豆子 ClusterBuilderCustomizer 请使用以下代码(使用@value 注释)获取值:
@Value("${spring.data.cassandra.pool.max-requests-local}")
private int localPool;
@Value("${spring.data.cassandra.pool.max-requests-remote}")
private int remotePool;
通过使用此 PoolingOptions class 为本地和远程设置 setMaxRequestsPerConnections
HostDistance.LOCAL -- localPool
HostDistance.REMOTE -- remotePool
根据 Spring Boot 2.3.0 release notes, ClusterBuilderCustomizer has been replaced with DriverConfigLoaderBuilderCustomizer and CqlSessionBuilderCustomizer. As said in
@Bean
public CqlSessionBuilderCustomizer cqlSessionBuilderCustomizer() {
return cqlSessionBuilder -> cqlSessionBuilder
.withNodeStateListener(new MyNodeStateListener())
.withSchemaChangeListener(new MySchemChangeListener());
}
@Bean
public DriverConfigLoaderBuilderCustomizer driverConfigLoaderBuilderCustomizer() {
return loaderBuilder -> loaderBuilder
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(10));
}
}