当前位置:网站首页>Ruoyi's solution to error reporting after integrating flyway
Ruoyi's solution to error reporting after integrating flyway
2022-07-28 16:52:00 【yelangking1】
ruoyi Series framework is a very good source platform in open source , Use loose open source protocols to open source code . Whether it's a single version 、 Front end and back-end separation and even micro service architecture , Corresponding codes are provided . be based on ruoyi You can make your own background system , You can also learn the integration of many technologies .
and flyway yes java The automatic database script management tool , Use flyway It can be automatically managed when the application is upgraded sql Script , So as to avoid the problems caused by users' forgetting and not executing scripts . It is very easy to use in multi version development sql Version management component .
Official ruoyi The framework is not integrated flyway, In the open source ecosystem, there are some fans based on ruoyi To integrate . This article will briefly share based on ruoyi Single version ( Other versions are similar ) How to integrate flyway, What problems will you encounter in the process of integration , Share three solutions at the same time .
One 、flyway stay ruoyi Integration in the framework
1、 stay ruoyi Of pom.xml In the definition of flyway rely on , The specific code is as follows :
<!-- Database version control core depends on -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<!-- Database version control plug-in -->
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
</plugin>2、 System profile application.yml Definition flyway Related configuration
flyway:
# Character encoding
encoding: utf-8
# A description of the baseline version when performing the migration
baseline-description: BaseLineInitialize
# If the connected database is not empty , Whether to initialize
# Find the target when migrating schema Non empty , And with tables without metadata , Whether to perform benchmark migration automatically , Default false.
baseline-on-migrate: true
# Appoint baseline Version number of , The default value is 1, Below this version number SQL file , migrate When it's ignored
# Start the benchmark migration against the existing schema The version of is labeled , The default value is 1.
baseline-version: 1.0.0
# Whether to turn on verification
# Check when migrating , The default is true
validate-on-migrate: true
# It's better to open the development environment outOfOrder, Production environment shut down outOfOrder
# Whether to allow unordered migration , Default false
out-of-order: true
# Whether to ignore error migration when reading metadata table , Default false
ignore-future-migrations: false
# When the connection is initialized SQL
init-sql: SELECT * FROM pg_tables WHERE tablename NOT LIKE'pg%' AND tablename NOT LIKE'sql_%' ORDER BY tablename;3、 Definition flyway To configure bean
package com.hngtghy.framework.config;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FlywayConfig {
@Autowired
private DataSource dataSource;
// Character encoding
@Value("${flyway.encoding}")
private String encoding;
// A description of the baseline version when performing the migration
@Value("${flyway.baseline-description}")
private String baselineDescription;
// Whether to perform benchmark migration automatically
@Value("${flyway.baseline-on-migrate}")
private boolean baselineOnMigrate;
// Appoint baseline Version number of
@Value("${flyway.baseline-version}")
private String baselineVersion;
// Check when migrating
@Value("${flyway.validate-on-migrate}")
private boolean validateOnMigrate;
// Whether to allow unordered migration
@Value("${flyway.out-of-order}")
private boolean outOfOrder;
// Whether to ignore error migration when reading metadata table
@Value("${flyway.ignore-future-migrations}")
private boolean ignoreFutureMigrations;
// When the connection is initialized SQL
@Value("${flyway.init-sql}")
private String initSql;
@PostConstruct
public void migrate() {
Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.encoding(encoding)
.baselineDescription(baselineDescription)
.baselineOnMigrate(baselineOnMigrate)
.baselineVersion(baselineVersion)
.validateOnMigrate(validateOnMigrate)
.outOfOrder(outOfOrder)
.ignoreFutureMigrations(ignoreFutureMigrations)
.initSql(initSql)
.load();
flyway.migrate();
}
}
4、 The most important thing is never to forget resources Define initial script under directory , Here's the picture :

5、 Start up main Remember to write the following code in the entry :
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,FlywayAutoConfiguration.class })Basically complete the above steps flyway The introduction of framework . The road to success is always full of obstacles , This is no exception , start-up main Method , You'll find that , The system reports an error .

Don't panic in case of abnormality , Because panic can't solve the problem . Check the log carefully , The main cause of the error is the system loading sys_config The table cannot be found when the information of . Why does the system load at startup ? Look forward to , Found in the ConfigServiceImpl Define a class loading initialization method :
/**
* When the project starts , Initialize parameters to cache
*/
@PostConstruct
public void init()
{
loadingConfigCache();
}The reason for this problem is this . Because during initialization , Will query the table from the database . While using flyway after , The database structure has not been initialized , So it must be wrong . The solution is , Give Way flyway Start... First , Initialize the database structure , Then run the corresponding parameters into the cache . Here are three ways to solve this problem .
Two 、 solve flyway Integrated error reporting method
1、 For small-scale applications , You can disable loading data into the cache during initialization . Because the data will be loaded into the cache during actual access , Therefore, the effect of speeding up is not very obvious . This solution is relatively simple . Will involve @PostConstruct Initially loaded bean Are commented out .
2、 What if you want to have an initial cache after the application system starts ? It's OK, too . See a blogger's article for specific methods , This way can also be a good solution . Blog address :https://www.easck.com/cos/2021/0626/619426.shtml , This article will not be repeated .
3、 stay springboot Use in @DependsOn Annotation to set flyway Startup sequence . Set the class of this annotation , You must initialize the pre class before starting . This is a good way to avoid exceptions . The specific code example is as follows :
@Service
@DependsOn("flywayConfig")
public class ConfigServiceImpl implements IConfigServiceThrough the above configuration, you can complete flyway Integration of , And successfully start the application .


summary : This article briefly introduces ruoyi System and flyway Database version control technology . It also explains how to ruiyi In the integration flyway Components . It focuses on integration flyway And three different solutions to this problem .
边栏推荐
- 排序3-选择排序与归并排序(递归实现+非递归实现)
- Quickly master kotlin set functions
- Leetcode learn complex questions with random pointer linked lists (detailed explanation)
- 2021-04-02
- 栈的介绍与实现(详解)
- CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
- mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
- 结构化设计的概要与原理--模块化
- About mit6.828_ HW9_ Some problems of barriers xv6 homework9
猜你喜欢

小程序:scroll-view默认滑倒最下面

遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅

Ansa secondary development - build ansa secondary development environment on Visual Studio code

Debugging methods of USB products (fx3, ccg3pa)

每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...

Sort 5-count sort

HM secondary development - data names and its use

Brother Ali teaches you how to correctly understand the problem of standard IO buffer

IM即时通讯软件开发网络请求成功率的优化

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
随机推荐
mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
信号屏蔽与处理
Best Cow Fences 题解
Best cow fences solution
NoSQL introduction practice notes I
在vs code上配置Hypermesh二次开发环境
Hdu1847 problem solving ideas
MD5加密验证
Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)
Redis series 4: sentinel (sentinel mode) with high availability
关于MIT6.828_HW9_barriers xv6 homework9的一些问题
Multiple commands produce ‘.../xxx.app/Assets.car‘问题
排序2-冒泡排序与快速排序(递归加非递归讲解)
Li Hongyi, machine learning 4. Deep learning
Multiple commands produce '... /xxx.app/assets.car' problem
排序4-堆排序与海量TopK问题
Sort 4-heap sort and massive TOPK problem
Sort 5-count sort
Call DLL file without source code
[learn slam from scratch] publish the coordinate system transformation relationship to topic TF