我有一个 Spring boot 项目,其中使用 Cassandra 作为数据库。
目前,我通过 Autowiring CassandraOperations
获取 Cassandra 实例。
我的问题是:
我们如何使用属性文件设置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,它获取可通过属性文件提供的注入(inject)属性(更一般地说,是 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
的替代方法是使用配置类(用 @ConfigurationProperties
注释,它为您提供 IDE 支持(例如属性名称自动完成)。
关于spring - 如何在 Spring boot 中使用属性文件添加 Cassandra MaxRequestsPerConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45171346/