生产采用外部mysql部署时,为了方便后续定制sql及减少与infra沟通,采用flyway自动执行和管理sql脚本,具体过程及相关点记录一下,方便后续回顾,希望对你也有所帮助。
flyway的引入
版本问题
本着要用就是最新的原则,直接使用了8.5.5,启动报类缺失的错,后发现springboot本身有管理flyway版本,因此不需要指定版本即可
FlywayAutoConfiguration
查看源码可知,依赖一个DataSource,因此我引入了starter-jpa,配置了datasource相关属性,无奈还是没法字段创建flyway(知道原因的小伙伴可以留言给我,谢谢),因此只能自己创建一个DataSource
aws的secretmanager
由于数据库的密码信息是配置在aws的secretmanager中,因此需要在spring启动后进行配置拉取,并覆盖本地配置文件的值。
这里采用添加Listener的方式进行监听处理
# spring.factories
org.springframework.context.ApplicationListener=xx.AwsFetchConfigurationListener
public class AwsFetchConfigurationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
// 拉取配置,并添加到环境中
getCredential(environment).ifPresent(credential -> {
// all properties need to be config in AWS Secrets Management, otherwise it will occur error when setting
Properties props = new Properties();
props.