spring数据cassandra配置动态变化

j8yoct9x  于 2021-06-15  发布在  Cassandra
关注(0)|答案(1)|浏览(402)

我正在尝试对我的申请做类似的事情。我正在使用以下版本的spring boot和cassandra:
spring data cassandra-2.0.8.release spring boot starter parent-2.0.4.release我需要动态更改cassandra的一些属性(主要是主机名),并希望它与应用程序建立新的连接。对于配置更改,我们有内部云配置更改管理,它在更改上运行良好,并监听它。这是我的课:

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@RefreshScope
@EnableCassandraRepositories(basePackages = {"com.*.*.*.dao.repo"})
public class AppConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppConfig.class);

    @Value("${application['cassandraPort']}")
    private String cassandraPort;

    @Value("${application['cassandraEndpoint']}")
    private String cassandraEndpoint;

    @Value("${application['keyspaceName']}")
    private String keyspaceName;

    @Value("${application['cassandraConsistency']}")
    private String cassandraConsistency;

    @Value("${application['cassandraUserName']}")
    private String cassandraUserName;

    @Autowired
    private AppConfig appConfig;

    public AppConfig() {

        System.out.println("AppConfig Constructor");

    }

    public String getCassandraPort() {
        return cassandraPort;
    }

    public void setCassandraPort(String cassandraPort) {
        this.cassandraPort = cassandraPort;
    }

    public String getCassandraEndpoint() {
        return cassandraEndpoint;
    }

    public void setCassandraEndpoint(String cassandraEndpoint) {
        this.cassandraEndpoint = cassandraEndpoint;
    }

    public String getKeyspaceName() {
        return keyspaceName;
    }

    public void setKeyspaceName(String keyspaceName) {
        this.keyspaceName = keyspaceName;
    }

    public String getCassandraConsistency() {
        return cassandraConsistency;
    }

    public void setCassandraConsistency(String cassandraConsistency) {
        this.cassandraConsistency = cassandraConsistency;
    }

    public String getCassandraUserName() {
        return cassandraUserName;
    }

    public void setCassandraUserName(String cassandraUserName) {
        this.cassandraUserName = cassandraUserName;
    }

    @Bean
    // @RefreshScope
    public CassandraConverter converter() {
        return new MappingCassandraConverter(this.mappingContext());
    }

    @Bean
    // @RefreshScope
    public CassandraMappingContext mappingContext() {
        return new CassandraMappingContext();
    }

    @Bean
    //@RefreshScope
    public CassandraSessionFactoryBean session() {

        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();

        session.setCluster(this.cluster().getObject());
        session.setKeyspaceName(appConfig.getKeyspaceName());
        session.setConverter(this.converter());
        session.setSchemaAction(SchemaAction.NONE);
        return session;
    }

    @Bean
    //@RefreshScope
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();

        cluster.setContactPoints(appConfig.getCassandraEndpoint());
        cluster.setPort(Integer.valueOf(appConfig.getCassandraPort()));
        cluster.setUsername(appConfig.getCassandraUserName());
        cluster.setPassword("password");
        cluster.setQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM));

        return cluster;
    }

}

但是,当我尝试将@refreshscope与该配置类一起使用时,应用程序无法启动。这是它在控制台中显示的内容:


***************************

APPLICATION FAILED TO START

***************************

Description:

Parameter 2 of constructor in org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration required a bean of type 'com.datastax.driver.core.Cluster' that could not be found.
    - Bean method 'cassandraCluster' not loaded because auto-configuration 'CassandraAutoConfiguration' was excluded

Action:

Consider revisiting the entries above or defining a bean of type 'com.datastax.driver.core.Cluster' in your configuration.

在cassandrabean中使用@refreshscope有什么指导原则吗?如果有人早就这么做了,你能分享一下吗?

nnsrf1az

nnsrf1az1#

你在混合一些东西。
配置包含属性和bean定义。 @RefreshScopeAppConfig 对springboot的自动配置造成一些干扰,并且声明的bean没有被使用(这就是为什么您会看到 Parameter 2 of constructor… ).
为了清理,我们将尽可能地重用springboot提供的内容,并且只声明真正需要的内容。
按照以下步骤解决问题(基于上面的代码):
创建 @ConfigurationProperties 封装属性的bean,或者更好的重用 CassandraProperties .
重新启用 CassandraAutoConfiguration 把你自己的拿走 MappingContext 以及 CassandraConverter 豆子,留着吧 Cluster 以及 Session bean定义
声明 Cluster 以及 Session 根据需要种植豆子,并让它们使用 @RefreshScope . 你的 @Configuration 类应该如下所示:
配置示例:

@Configuration
public class MyConfig {

    @Bean(destroyMethod = "close")
    @RefreshScope
    public Cluster cassandraCluster(CassandraProperties properties) {
        Cluster.Builder builder = Cluster.builder().addContactPoints(properties.getContactPoints().toArray(new String[0]))
                .withoutJMXReporting();

        return builder.build();
    }

    @Bean(destroyMethod = "close")
    @RefreshScope
    public Session cassandraSession(CassandraProperties properties, Cluster cluster) {
        return cluster.connect(properties.getKeyspaceName());
    }
}

相关问题