当前位置:网站首页>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 .
边栏推荐
- Li Hongyi, machine learning 5. Tips for neural network design
- HyperMesh auto save (enhanced) plug-in instructions
- "Weilai Cup" 2022 Niuke summer multi school training camp 3 h.hacker sam+ segment tree /dp/ divide and conquer (without the largest sub segment and of the inspection interval)
- Interesting kotlin 0x06:list minus list
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- HDU1847解题思路
- 2021-04-02
- LeetCode-学会对无序链表进行插入排序(详解)
- Reset grafana login password to default password
- Oracle table partition
猜你喜欢

Im im development optimization improves connection success rate, speed, etc

快速掌握 Kotlin 集合函数

LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数

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

有趣的 Kotlin 0x08:What am I

Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)

Sort 5-count sort

Learn ABAQUS script programming script in an hour

ANSA二次开发 - 界面开发工具介绍

LeetCode每日一练 —— 160. 相交链表
随机推荐
Tcp/ip related
队列的介绍与实现(详解)
微软100题-天天做-第11题
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
Asp.net large file block upload breakpoint resume demo
Analysis of echo service model in the first six chapters of unp
Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
“蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
给定正整数N、M,均介于1~10 ^ 9之间,N <= M,找出两者之间(含N、M)的位数为偶数的数有多少个
Solve the width overflow of rich text pictures such as uniapp
排序3-选择排序与归并排序(递归实现+非递归实现)
Call DLL file without source code
Introduction and implementation of queue (detailed explanation)
Ansa secondary development - Introduction to interface development tools
PHP image upload
HyperMesh auto save (enhanced) plug-in instructions
Li Hongyi, machine learning 5. Tips for neural network design
HM二次开发 - Data Names及其使用
结构化设计的概要与原理--模块化
Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)