【Kong】ODBC适配国产数据库

1.前言

Kong Gateway是一个运行在Nginx上的Lua应用程序,它与OpenResty一起发布。Kong Gateway底层数据库只支持PostgreSQL和Cassandra,从3.4.0版本官方已不在支持Cassandra。
微信截图_20231010220400.png

由于大部分国产数据并没有提供Lua语言驱动,所有笔者将采用ODBC适配达梦数据库,文中涉及到的lua源码需要读者自行实现。

2.环境

  • 安装bazel
cd "/usr/bin" && \
curl -fLO https://releases.bazel.build/6.1.0/release/bazel-6.1.0-linux-x86_64 && \ 
chmod +x bazel-6.1.0-linux-x86_64 && \
mv bazel-6.1.0-linux-x86_64 bazel
  • 基础依赖
yum install \
    automake \
    gcc \
    gcc-c++ \
    git \
    libyaml-devel \
    make \
    patch \
    perl \
    perl-IPC-Cmd \
    protobuf-devel \
    unzip \
    valgrind \
    valgrind-devel \
    zlib-devel
  • ODBC
yum install unixODBC unixODBC-devel

配置odbc数据库源配置文件在: /etc/odbc.ini

[DM8]
Description = DM ODBC DSN
#DM8数据库区动
Driver = /usr/lib/dm/libdodbc.so
#数据库连接地址
SERVER = localhost 
#此处为用户名
UID = SYSDBA 
#此处为连接的密码
PWD = 12345678s 
#数据库实例端口号
TCP_PORT = 5236

命令验证是否生效
微信截图_20231010225244.png

3.适配

  • kong-3.3.1-0.rockspec文件中增加odbc库
dependencies = {
 ......
 "luasql-odbc == 2.6.0",
}
build = {
  type = "builtin",
  modules = {
    ......
    ["kong.cluster_events.strategies.dm"] = "kong/cluster_events/strategies/dm.lua",
    ["kong.db.strategies.dm"] = "kong/db/strategies/dm/init.lua",
    ["kong.db.strategies.dm.connector"] = "kong/db/strategies/dm/connector.lua",
    ["kong.db.strategies.dm.tags"] = "kong/db/strategies/dm/tags.lua",
    ......
  }
}
  • 创建Lua文件
kong/cluster_events/strategies/dm.lua
#建立数据库连接
kong/db/strategies/dm/connector.lua
#访问数据库SELECT、INSERT、DELETE、UPDATE等
kong/db/strategies/dm/init.lua
kong/db/strategies/dm/tags.lua
  • 修改Lua文件
#配置文件
kong.conf.default
kong/templates/kong_defaults.lua
kong/db/strategies/init.lua
kong/cluster_events/init.lua
kong/conf_loader/init.lua
kong/db/schema/others/migrations.lua
#依赖的表、触发器、函数
kong/db/migrations/core/000_base.lua
#辅助表结构及插件依赖表结构可忽略但必须有数据库名称配置
#当然也可以把表结构拆分到相关的lua文件中
kong/db/migrations/core/003_100_to_110.lua
kong/db/migrations/core/004_110_to_120.lua
kong/db/migrations/core/005_120_to_130.lua
kong/db/migrations/core/006_130_to_140.lua
kong/db/migrations/core/007_140_to_150.lua
kong/db/migrations/core/008_150_to_200.lua
kong/db/migrations/core/009_200_to_210.lua
kong/db/migrations/core/010_210_to_211.lua
kong/db/migrations/core/011_212_to_213.lua
kong/db/migrations/core/012_213_to_220.lua
kong/db/migrations/core/013_220_to_230.lua
kong/db/migrations/core/014_230_to_270.lua
kong/db/migrations/core/015_270_to_280.lua
kong/db/migrations/core/016_280_to_300.lua
kong/db/migrations/core/017_300_to_310.lua
kong/db/migrations/core/018_310_to_320.lua
kong/db/migrations/core/019_320_to_330.lua
kong/db/migrations/operations/200_to_210.lua。

4.编译

bazel build //build:kong --verbose_failures --action_env=CFLAGS="-DUNIXODBC"
bazel build --config release //build:kong --verbose_failures --action_env=CFLAGS="-DUNIXODBC
#输出rpm文件到当前目录的bazel-bin/pkg/
bazel build --config release :kong_el7 --verbose_failures

5.安装

默认安装路径: /usr/local/share/lua/5.1/,日志路径: /usr/local/kong

  • yum install -y kong.el7.xxx.rpm
  • /etc/kong/kong.conf
  • kong migrations bootstrap
  • kong start

你可能感兴趣的:(【Kong】ODBC适配国产数据库)