Spring boot 如何在springbootapplication中启用Cassandra CqlSession度量
我想启用cassandra cqlsession度量。当尝试注册cqlsession度量时,它在springboot应用程序中提供可选的.empty()。这里我使用的是cassandra datastax java驱动程序4.6 这是我的密码:Spring boot 如何在springbootapplication中启用Cassandra CqlSession度量,spring-boot,cassandra,spring-data-cassandra,Spring Boot,Cassandra,Spring Data Cassandra,我想启用cassandra cqlsession度量。当尝试注册cqlsession度量时,它在springboot应用程序中提供可选的.empty()。这里我使用的是cassandra datastax java驱动程序4.6 这是我的密码: @Autowired private CqlSession cqlsession; MetricRegistry metricRegistry = cqlsession.getMetrics() .orElseThrow(()
@Autowired
private CqlSession cqlsession;
MetricRegistry metricRegistry = cqlsession.getMetrics()
.orElseThrow(() -> new IllegalArgumentException("not able to get metrics"))
.getRegistry();
正在引发IllegalArgumentException错误
参考cassandra Datasax()的官方文件时。添加到项目中的同一组conf文件无法解决此问题我已通过以下方法解决了此问题: 配置类
import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.METRICS_NODE_ENABLED;
import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.METRICS_SESSION_ENABLED;
import org.springframework.boot.autoconfigure.cassandra.DriverConfigLoaderBuilderCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(CassandraProperties.class)
public class CassandraMetricsConfig {
@Bean
DriverConfigLoaderBuilderCustomizer configLoaderBuilderCustomizer(CassandraProperties cassandraProperties) {
return builder -> {
builder.withStringList(METRICS_SESSION_ENABLED, cassandraProperties.getSessionMetrics());
builder.withStringList(METRICS_NODE_ENABLED, cassandraProperties.getNodeMetrics());
};
}
}
属性类
@ConfigurationProperties(prefix = "cassandra")
@Data
public class CassandraProperties {
@NotNull
private List<String> sessionMetrics = new ArrayList<>();
@NotNull
private List<String> nodeMetrics = new ArrayList<>();
}
注意:这种方法仅适用于不需要额外配置(如cql请求)的度量。如果要监视cql请求,必须扩展示例以配置所需的属性。我已通过以下方法解决了此问题: 配置类
import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.METRICS_NODE_ENABLED;
import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.METRICS_SESSION_ENABLED;
import org.springframework.boot.autoconfigure.cassandra.DriverConfigLoaderBuilderCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(CassandraProperties.class)
public class CassandraMetricsConfig {
@Bean
DriverConfigLoaderBuilderCustomizer configLoaderBuilderCustomizer(CassandraProperties cassandraProperties) {
return builder -> {
builder.withStringList(METRICS_SESSION_ENABLED, cassandraProperties.getSessionMetrics());
builder.withStringList(METRICS_NODE_ENABLED, cassandraProperties.getNodeMetrics());
};
}
}
属性类
@ConfigurationProperties(prefix = "cassandra")
@Data
public class CassandraProperties {
@NotNull
private List<String> sessionMetrics = new ArrayList<>();
@NotNull
private List<String> nodeMetrics = new ArrayList<>();
}
注意:这种方法仅适用于不需要额外配置(如cql请求)的度量。如果要监视cql请求,必须扩展示例以配置所需的属性