1

我有一個Spring引導項目,其中我使用Cassandra作爲數據庫。如何在Spring引導中使用屬性文件添加Cassandra MaxRequestsPerConnection

目前,我通過自動佈線CassandraOperations獲得了Cassandra實例。

我的問題是

我們如何使用屬性文件中設置MaxRequestsPerConnection

# spring.data.cassandra.keyspace-name=event 
# spring.data.cassandra.contact-points=localhost 
# spring.data.cassandra.port=9042 

目前,我有我的屬性文件這些屬性,但我沒有發現任何屬性設置MaxRequestsPerConnection

回答

2

春天開機不提供所有屬性的配置。您可以定義一個ClusterBuilderCustomizer bean來自定義Cluster實例。

嘗試使用以下代碼來聲明一個定製豆它獲取屬性注射可經由屬性來提供文件(更一般地講,彈簧引導可用任何屬性源):

@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支持(如屬性名稱自動完成)

0

步驟號: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 

步驟否:在bean配置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類爲本地和遠程設置setMaxRequestsPerConnections

HostDistance.LOCAL -- localPool 
HostDistance.REMOTE -- remotePool 
相關問題