0%

liquibase 踩坑记

liquibase 是跨多种数据库平台的数据库迁移和初始化工具,但是在迁移到其它数据库平台的时候会遇到各种字段类型不一致导致出错。由于我使用的是 SpringBoot 和 hibernate,并且在 hibernate 初始化的时候打开了 validate,所以从不同数据库迁移会出问题。

uuid 出错的问题

当在 hibernate 里面使用了 uuid 的时候,在postgresql 里面是正常的,但是在使用 MySQL 的时候 hibernate 会报一下错误found [char (Types#CHAR)], but expecting [binary(255) (Types#BINARY)] ,所以需要 liquibase 里面写上以下配置。

1
2
3
4
5
6
<!--  uuid 类型的定义  -->
<property name="type.uuid" value="RAW(16)" dbms="oracle"/>
<property name="type.uuid" value="UUID" dbms="h2"/>
<property name="type.uuid" value="UNIQUEIDENTIFIER" dbms="mssql"/>
<property name="type.uuid" value="BINARY(16)" dbms="mysql"/>
<property name="type.uuid" value="uuid" dbms="postgresql"/>

然后修改 liquibase uuid 的 column 节点配置

1
2
3
<!--  把之前的 type="uuid" 改为 ${type.uuid}  -->
<column name="id" remarks="ID" type="${type.uuid}">
</column>

以上配置只支持 oracle,h2,mssql,mysql,postgresql,如需要其他数据库请根据下面的类型对应表查找liquibase 对应数据库的类型

liquibase 对应的各个数据库类型

这里整合了liquibase的各个数据库对应的类型,来自 stackoverflow,但是这里也不完全是和 hibernate 对应的,所以需要注意。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
boolean
MySQLDatabase: BIT(1)
SQLiteDatabase: BOOLEAN
H2Database: BOOLEAN
PostgresDatabase: BOOLEAN
UnsupportedDatabase: BOOLEAN
DB2Database: SMALLINT
MSSQLDatabase: [bit]
OracleDatabase: NUMBER(1)
HsqlDatabase: BOOLEAN
FirebirdDatabase: SMALLINT
DerbyDatabase: SMALLINT
InformixDatabase: BOOLEAN
SybaseDatabase: BIT
SybaseASADatabase: BIT

tinyint
MySQLDatabase: TINYINT
SQLiteDatabase: TINYINT
H2Database: TINYINT
PostgresDatabase: SMALLINT
UnsupportedDatabase: TINYINT
DB2Database: SMALLINT
MSSQLDatabase: [tinyint]
OracleDatabase: NUMBER(3)
HsqlDatabase: TINYINT
FirebirdDatabase: SMALLINT
DerbyDatabase: SMALLINT
InformixDatabase: TINYINT
SybaseDatabase: TINYINT
SybaseASADatabase: TINYINT

int
MySQLDatabase: INT
SQLiteDatabase: INTEGER
H2Database: INT
PostgresDatabase: INT
UnsupportedDatabase: INT
DB2Database: INTEGER
MSSQLDatabase: [int]
OracleDatabase: INTEGER
HsqlDatabase: INT
FirebirdDatabase: INT
DerbyDatabase: INTEGER
InformixDatabase: INT
SybaseDatabase: INT
SybaseASADatabase: INT

mediumint
MySQLDatabase: MEDIUMINT
SQLiteDatabase: MEDIUMINT
H2Database: MEDIUMINT
PostgresDatabase: MEDIUMINT
UnsupportedDatabase: MEDIUMINT
DB2Database: MEDIUMINT
MSSQLDatabase: [int]
OracleDatabase: MEDIUMINT
HsqlDatabase: MEDIUMINT
FirebirdDatabase: MEDIUMINT
DerbyDatabase: MEDIUMINT
InformixDatabase: MEDIUMINT
SybaseDatabase: MEDIUMINT
SybaseASADatabase: MEDIUMINT

bigint
MySQLDatabase: BIGINT
SQLiteDatabase: BIGINT
H2Database: BIGINT
PostgresDatabase: BIGINT
UnsupportedDatabase: BIGINT
DB2Database: BIGINT
MSSQLDatabase: [bigint]
OracleDatabase: NUMBER(38, 0)
HsqlDatabase: BIGINT
FirebirdDatabase: BIGINT
DerbyDatabase: BIGINT
InformixDatabase: INT8
SybaseDatabase: BIGINT
SybaseASADatabase: BIGINT

float
MySQLDatabase: FLOAT
SQLiteDatabase: FLOAT
H2Database: FLOAT
PostgresDatabase: FLOAT
UnsupportedDatabase: FLOAT
DB2Database: FLOAT
MSSQLDatabase: [float](53)
OracleDatabase: FLOAT
HsqlDatabase: FLOAT
FirebirdDatabase: FLOAT
DerbyDatabase: FLOAT
InformixDatabase: FLOAT
SybaseDatabase: FLOAT
SybaseASADatabase: FLOAT

double
MySQLDatabase: DOUBLE
SQLiteDatabase: DOUBLE
H2Database: DOUBLE
PostgresDatabase: DOUBLE PRECISION
UnsupportedDatabase: DOUBLE
DB2Database: DOUBLE
MSSQLDatabase: [float](53)
OracleDatabase: FLOAT(24)
HsqlDatabase: DOUBLE
FirebirdDatabase: DOUBLE PRECISION
DerbyDatabase: DOUBLE
InformixDatabase: DOUBLE PRECISION
SybaseDatabase: DOUBLE
SybaseASADatabase: DOUBLE

decimal
MySQLDatabase: DECIMAL
SQLiteDatabase: DECIMAL
H2Database: DECIMAL
PostgresDatabase: DECIMAL
UnsupportedDatabase: DECIMAL
DB2Database: DECIMAL
MSSQLDatabase: [decimal](18, 0)
OracleDatabase: DECIMAL
HsqlDatabase: DECIMAL
FirebirdDatabase: DECIMAL
DerbyDatabase: DECIMAL
InformixDatabase: DECIMAL
SybaseDatabase: DECIMAL
SybaseASADatabase: DECIMAL

number
MySQLDatabase: numeric
SQLiteDatabase: NUMBER
H2Database: NUMBER
PostgresDatabase: numeric
UnsupportedDatabase: NUMBER
DB2Database: numeric
MSSQLDatabase: [numeric](18, 0)
OracleDatabase: NUMBER
HsqlDatabase: numeric
FirebirdDatabase: numeric
DerbyDatabase: numeric
InformixDatabase: numeric
SybaseDatabase: numeric
SybaseASADatabase: numeric

blob
MySQLDatabase: LONGBLOB
SQLiteDatabase: BLOB
H2Database: BLOB
PostgresDatabase: BYTEA
UnsupportedDatabase: BLOB
DB2Database: BLOB
MSSQLDatabase: [varbinary](MAX)
OracleDatabase: BLOB
HsqlDatabase: BLOB
FirebirdDatabase: BLOB
DerbyDatabase: BLOB
InformixDatabase: BLOB
SybaseDatabase: IMAGE
SybaseASADatabase: LONG BINARY

function
MySQLDatabase: FUNCTION
SQLiteDatabase: FUNCTION
H2Database: FUNCTION
PostgresDatabase: FUNCTION
UnsupportedDatabase: FUNCTION
DB2Database: FUNCTION
MSSQLDatabase: [function]
OracleDatabase: FUNCTION
HsqlDatabase: FUNCTION
FirebirdDatabase: FUNCTION
DerbyDatabase: FUNCTION
InformixDatabase: FUNCTION
SybaseDatabase: FUNCTION
SybaseASADatabase: FUNCTION

UNKNOWN
MySQLDatabase: UNKNOWN
SQLiteDatabase: UNKNOWN
H2Database: UNKNOWN
PostgresDatabase: UNKNOWN
UnsupportedDatabase: UNKNOWN
DB2Database: UNKNOWN
MSSQLDatabase: [UNKNOWN]
OracleDatabase: UNKNOWN
HsqlDatabase: UNKNOWN
FirebirdDatabase: UNKNOWN
DerbyDatabase: UNKNOWN
InformixDatabase: UNKNOWN
SybaseDatabase: UNKNOWN
SybaseASADatabase: UNKNOWN

datetime
MySQLDatabase: datetime
SQLiteDatabase: TEXT
H2Database: TIMESTAMP
PostgresDatabase: TIMESTAMP WITHOUT TIME ZONE
UnsupportedDatabase: datetime
DB2Database: TIMESTAMP
MSSQLDatabase: [datetime]
OracleDatabase: TIMESTAMP
HsqlDatabase: TIMESTAMP
FirebirdDatabase: TIMESTAMP
DerbyDatabase: TIMESTAMP
InformixDatabase: DATETIME YEAR TO FRACTION(5)
SybaseDatabase: datetime
SybaseASADatabase: datetime

time
MySQLDatabase: time
SQLiteDatabase: time
H2Database: time
PostgresDatabase: TIME WITHOUT TIME ZONE
UnsupportedDatabase: time
DB2Database: time
MSSQLDatabase: [time](7)
OracleDatabase: DATE
HsqlDatabase: time
FirebirdDatabase: time
DerbyDatabase: time
InformixDatabase: INTERVAL HOUR TO FRACTION(5)
SybaseDatabase: time
SybaseASADatabase: time

timestamp
MySQLDatabase: timestamp
SQLiteDatabase: TEXT
H2Database: TIMESTAMP
PostgresDatabase: TIMESTAMP WITHOUT TIME ZONE
UnsupportedDatabase: timestamp
DB2Database: timestamp
MSSQLDatabase: [datetime]
OracleDatabase: TIMESTAMP
HsqlDatabase: TIMESTAMP
FirebirdDatabase: TIMESTAMP
DerbyDatabase: TIMESTAMP
InformixDatabase: DATETIME YEAR TO FRACTION(5)
SybaseDatabase: datetime
SybaseASADatabase: timestamp

date
MySQLDatabase: date
SQLiteDatabase: date
H2Database: date
PostgresDatabase: date
UnsupportedDatabase: date
DB2Database: date
MSSQLDatabase: [date]
OracleDatabase: date
HsqlDatabase: date
FirebirdDatabase: date
DerbyDatabase: date
InformixDatabase: date
SybaseDatabase: date
SybaseASADatabase: date

char
MySQLDatabase: CHAR
SQLiteDatabase: CHAR
H2Database: CHAR
PostgresDatabase: CHAR
UnsupportedDatabase: CHAR
DB2Database: CHAR
MSSQLDatabase: [char](1)
OracleDatabase: CHAR
HsqlDatabase: CHAR
FirebirdDatabase: CHAR
DerbyDatabase: CHAR
InformixDatabase: CHAR
SybaseDatabase: CHAR
SybaseASADatabase: CHAR

varchar
MySQLDatabase: VARCHAR
SQLiteDatabase: VARCHAR
H2Database: VARCHAR
PostgresDatabase: VARCHAR
UnsupportedDatabase: VARCHAR
DB2Database: VARCHAR
MSSQLDatabase: [varchar](1)
OracleDatabase: VARCHAR2
HsqlDatabase: VARCHAR
FirebirdDatabase: VARCHAR
DerbyDatabase: VARCHAR
InformixDatabase: VARCHAR
SybaseDatabase: VARCHAR
SybaseASADatabase: VARCHAR

nchar
MySQLDatabase: NCHAR
SQLiteDatabase: NCHAR
H2Database: NCHAR
PostgresDatabase: NCHAR
UnsupportedDatabase: NCHAR
DB2Database: NCHAR
MSSQLDatabase: [nchar](1)
OracleDatabase: NCHAR
HsqlDatabase: CHAR
FirebirdDatabase: NCHAR
DerbyDatabase: NCHAR
InformixDatabase: NCHAR
SybaseDatabase: NCHAR
SybaseASADatabase: NCHAR

nvarchar
MySQLDatabase: NVARCHAR
SQLiteDatabase: NVARCHAR
H2Database: NVARCHAR
PostgresDatabase: VARCHAR
UnsupportedDatabase: NVARCHAR
DB2Database: NVARCHAR
MSSQLDatabase: [nvarchar](1)
OracleDatabase: NVARCHAR2
HsqlDatabase: VARCHAR
FirebirdDatabase: NVARCHAR
DerbyDatabase: VARCHAR
InformixDatabase: NVARCHAR
SybaseDatabase: NVARCHAR
SybaseASADatabase: NVARCHAR

clob
MySQLDatabase: LONGTEXT
SQLiteDatabase: TEXT
H2Database: CLOB
PostgresDatabase: TEXT
UnsupportedDatabase: CLOB
DB2Database: CLOB
MSSQLDatabase: [varchar](MAX)
OracleDatabase: CLOB
HsqlDatabase: CLOB
FirebirdDatabase: BLOB SUB_TYPE TEXT
DerbyDatabase: CLOB
InformixDatabase: CLOB
SybaseDatabase: TEXT
SybaseASADatabase: LONG VARCHAR

currency
MySQLDatabase: DECIMAL
SQLiteDatabase: REAL
H2Database: DECIMAL
PostgresDatabase: DECIMAL
UnsupportedDatabase: DECIMAL
DB2Database: DECIMAL(19, 4)
MSSQLDatabase: [money]
OracleDatabase: NUMBER(15, 2)
HsqlDatabase: DECIMAL
FirebirdDatabase: DECIMAL(18, 4)
DerbyDatabase: DECIMAL
InformixDatabase: MONEY
SybaseDatabase: MONEY
SybaseASADatabase: MONEY

uuid
MySQLDatabase: char(36)
SQLiteDatabase: TEXT
H2Database: UUID
PostgresDatabase: UUID
UnsupportedDatabase: char(36)
DB2Database: char(36)
MSSQLDatabase: [uniqueidentifier]
OracleDatabase: RAW(16)
HsqlDatabase: char(36)
FirebirdDatabase: char(36)
DerbyDatabase: char(36)
InformixDatabase: char(36)
SybaseDatabase: UNIQUEIDENTIFIER
SybaseASADatabase: UNIQUEIDENTIFIER

一般在配置前端的文件夹的时候都会指定一个路径,如果你想使用根路径我这种方法不支持根路径,请使用另外一种支持根路径的配置方法。

配置

比如把前端放在 h5 路径下面

  1. 把前端打包的文件放在resource/META-INF/h5 目录

    img

  2. 修改Spring Boot WebMvc的配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    @Configuration
    public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/h5/**")
    .addResourceLocations("classpath:/META-INF/h5/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    // 配置如果用户访问的是根目录直接重定向掉h5页面
    registry.addRedirectViewController("/", "h5");
    // 配置用户如果访问h5路径但是没有带index.html 转发到 /h5/index.html 路径
    registry.addViewController("/h5")
    .setViewName("forward:/h5/index.html");
    registry.addViewController("/h5/")
    .setViewName("forward:/h5/index.html");
    // 一下是为了给前端 history路由的一些配置
    registry.addViewController("/h5/{spring:\\w+}")
    .setViewName("forward:/h5/index.html");
    registry.addViewController("/h5/**/{spring:\\w+}")
    .setViewName("forward/h5/index.html");
    registry.addViewController("/h5/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}")
    .setViewName("forward:/h5/index.html");
    }
    }

介绍 k3s

k3s 是Rancher Labs(以下简称Rancher)宣布推出轻量级Kubernetes发行版K3s(已开源),这款产品专为在资源有限的环境中运行Kubernetes的研发和运维人员设计。Rancher此次发布的K3s项目,将满足在边缘计算环境中运行在x86、ARM64和ARMv7处理器上的小型、易于管理的Kubernetes集群日益增长的需求。

开始安装

  1. 安装 docker
    官方文档
    这里推荐使用官方的脚本一键安装

    1
    2
    $ curl -fsSL https://get.docker.com -o get-docker.sh
    $ sudo sh get-docker.sh

    如果你想使用 k3s 默认的 contanerd 容器可以跳过,但是由于我对 contanerd 不熟悉所以我在这里使用的是 docker

  2. 安装 k3s server
    安装 k3s server 有几种方式,由于 k3s 的执行文件是是放在 github上的,但是在中国由于网速不好可能会导致下载不下来,所以可以先使用其它工具下载可执行文件然后上传到服务器上的 /usr/local/bin 目录下,然后执行 chmod 0755 /usr/local/bin/k3s 命令给与执行权限

    直接下载安装:

    1
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --no-deploy traefik --docker" sh -s -

    自己下载安装:

    1
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --no-deploy traefik --docker" INSTALL_K3S_BIN_DIR="/usr/local/bin" INSTALL_K3S_SKIP_DOWNLOAD=true sh -s -

    参数说明:
    INSTALL_K3S_EXEC k3s 启动的参数 –no-deploy traefik 是说不需要部署 traefik ingress,_–docker_是说使用 docker 容器
    INSTALL_K3S_BIN _DIRk3s 可执行文件的目录
    INSTALL_K3S_SKIP _DOWNLOAD true 跳过下载k3s 可执行文件

    查看 k3s server 是否安装成功

    1
    $ systemctl status k3s

    如果k3s 的状态为运行的话说明成功
    获取节点列表验证

    1
    $ kubectl get nodes
  3. 安装 k3s-agent
    首先在 k3s 的 server 服务器上查看节点的 token

    1
    $ cat /var/lib/rancher/k3s/server/node-token

    输出为 xxxxxxx::node:xxxxxx
    进入到 需要安装 agent 节点服务器执行

    1
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="agent --docker"  K3S_URL=https://192.168.0.1:6443 K3S_TOKEN=xxxxxxx::node:xxxxxx sh -s -

    如果是自己下载并上传的话执行

    1
    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="agent --docker" INSTALL_K3S_BIN_DIR="/usr/local/bin" K3S_URL=https://192.168.0.1:6443 K3S_TOKEN=xxxxxxx::node:xxxxxx INSTALL_K3S_SKIP_DOWNLOAD=true sh -s -

    参数基本都是一样的,现在多加了一个 K3S_TOKENK3S_URL 参数
    K3S_TOKEN 是主节点的 token
    K3S_URL 是主节点的 ip 地址和端口一般 https 和 6443 是默认的

    验证安装:

    1
    $ systemctl status k3s-agent

    如果 Active: active (running) 说明启动成功
    进入k3s server 的服务器查看子节点是否添加成功

    1
    $ kubectl get nodes

    如果列出了 agent 的节点说明安装成功,如果没有请检查 url 和 token 是否正确,并检查 agent 和 server 的网络是否能相互连接,还有就是 server 的防火墙是否打开了 6443端口

遇到的问题

当 agent 已经加入到了集群,但是发现部署在 server 里面的 pod 不能访问 agent 里面的 pod,发现 dns 是正常的,说明就是网络问题,进过搜索发现只要在 server 服务器和 agent 服务器执行 sudo iptable -F 就行