Spring Boot集成Liquibase操作Cassandra未执行变更日志排查
问题:Cassandra连接成功但Liquibase变更日志未执行
我们团队开发了一个以Job形式运行的应用,可通过Java参数和Profile切换PostgreSQL/Cassandra做数据库初始化。PostgreSQL运行正常,但Cassandra能连接成功,却不执行变更日志。
环境信息
Cassandra版本
4.1.0
项目依赖
implementation 'org.hibernate:hibernate-core' implementation 'com.zaxxer:HikariCP' implementation 'org.springframework:spring-jdbc' implementation 'org.liquibase:liquibase-core' implementation 'org.postgresql:postgresql' implementation "org.springframework.data:spring-data-cassandra" implementation ('org.liquibase.ext:liquibase-cassandra:4.18.0'){ exclude group: 'org.slf4j', module: 'slf4j-jdk14' } implementation 'org.yaml:snakeyaml'//TODO ? implementation "org.projectlombok:lombok" implementation "org.springframework.boot:spring-boot" implementation "org.springframework.boot:spring-boot-autoconfigure" implementation "org.springframework.cloud:spring-cloud-config-client" implementation "org.springframework:spring-beans" implementation "org.springframework:spring-context" implementation "org.springframework:spring-core" implementation "org.slf4j:slf4j-api" implementation "ch.qos.logback:logback-classic" implementation "org.slf4j:jul-to-slf4j" implementation "org.slf4j:log4j-over-slf4j"
application-cassandra.yaml配置
server: port: 8083 spring: config: liquibase.change-log: classpath:/${java-param}/changelog.xml spring: autoconfigure: exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration data: cassandra: port: 9042 contact-points: 127.0.0.1 local-datacenter: datacenter1 keyspace-name: hs360_pokus
Liquibase变更日志
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd"> <changeSet id="1" author="Liquibase"> <createTable tableName="test_table"> <column name="test_id" type="int"> <constraints primaryKey="true"/> </column> <column name="test_column" type="varchar"/> </createTable> </changeSet> </databaseChangeLog>
问题排查与修复方案
1. 修正配置层级错误
你的application-cassandra.yaml中spring节点嵌套了另一个spring,导致Cassandra配置无法被正确解析。修正后的配置如下:
server: port: 8083 spring: config: liquibase.change-log: classpath:/${java-param}/changelog.xml autoconfigure: exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration data: cassandra: port: 9042 contact-points: 127.0.0.1 local-datacenter: datacenter1 keyspace-name: hs360_pokus
2. 补充Liquibase Cassandra专属配置
Spring Boot默认Liquibase自动配置依赖DataSource,但Cassandra无标准DataSource实现,需手动指定Liquibase的Cassandra连接参数:
liquibase: driver-class-name: com.datastax.oss.driver.api.core.CqlSession url: cassandra://127.0.0.1:9042/hs360_pokus database-change-log: classpath:/${java-param}/changelog.xml
若Cassandra未开启认证,可省略user和password配置项。
3. 添加Cassandra Java驱动依赖
spring-data-cassandra未自动引入完整驱动包,需补充:
implementation 'com.datastax.oss:java-driver-core:4.15.0' // 版本匹配Cassandra 4.1.0即可
4. 适配Cassandra语法修改变更日志
Cassandra为NoSQL数据库,表结构语法与关系型数据库不同,需调整变更日志:
<?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd"> <changeSet id="1" author="Liquibase"> <ext:createTable tableName="test_table"> <column name="test_id" type="int"> <constraints primaryKey="true" partitionKey="true"/> </column> <column name="test_column" type="text"/> <!-- Cassandra用text替代varchar --> </ext:createTable> </changeSet> </databaseChangeLog>
注意使用ext:前缀调用Cassandra扩展操作。
5. 手动配置Liquibase Bean(可选)
若排除DataSourceAutoConfiguration后Liquibase自动配置未触发,可手动注册Bean:
@Configuration @Profile("cassandra") public class CassandraLiquibaseConfig { @Autowired private CassandraTemplate cassandraTemplate; @Bean public Liquibase liquibase() throws LiquibaseException { CqlSession session = cassandraTemplate.getCqlSession(); CassandraDatabase database = new CassandraDatabase(session); Liquibase liquibase = new Liquibase( "classpath:/${java-param}/changelog.xml", new ClassLoaderResourceAccessor(), database ); liquibase.update(new Contexts(), new LabelExpression()); return liquibase; } }
内容的提问来源于stack exchange,提问作者Tomas
相关产品推荐
相关产品推荐

