Skip to content

Commit b859154

Browse files
author
wuguokai
committed
upgrade plugin buildrun emqx backend mysql
1 parent e1f04e7 commit b859154

35 files changed

+2523
-404
lines changed

.DS_Store

-4 KB
Binary file not shown.

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ EMQX client connection and message save to MySQL
33

44
emqx 客户端连接状态和消息持久化到MySQL 插件
55

6+
### erlang 环境配置
7+
8+
若没配置erlang环境,请参考[erlang 环境配置](./erlang环境配置.md).
69

710
### 编译发布插件
811

9-
1、clone emqx-rel 项目
12+
1、clone emqx-rel 项目, 切换到改 tag v4.1.2
13+
14+
``` bash
15+
git clone https://github.com/emqx/emqx-rel.git
16+
cd emqx-rel
17+
git checkout v4.1.2
18+
make
19+
```
1020

11-
> git clone https://github.com/emqx/emqx-rel.git
21+
正常构建成功后,运行 `_build/emqx/rel/emqx/bin/emqx console` 启动 emqx
1222

23+
### 添加自定义插件
1324

1425
2.rebar.config 添加依赖
1526

@@ -41,6 +52,8 @@ emqx 客户端连接状态和消息持久化到MySQL 插件
4152

4253
> make
4354
55+
打开 `localhost:18083` 的插件栏可以看到 `buildrun_emqx_backend_mysql` 插件。
56+
4457
### config配置
4558

4659
File: etc/buildrun_emqx_backend_mysql.conf
@@ -68,7 +81,7 @@ mysql.query_timeout = 10s
6881

6982
### mqtt_client.sql, mqtt_msg.sql
7083

71-
mqtt_client.sql, mqtt_msg.sql到你的数据库中
84+
到你的数据库中执行[mqtt_client.sql](./sql/mqtt_client.sql), [mqtt_msg.sql](./sql/mqtt_msg.sql)
7285

7386
### 加载插件
7487

@@ -80,11 +93,17 @@ data/loaded_plugins
8093

8194
> 添加 {buildrun_emqx_backend_mysql, true}.
8295
96+
设置插件自动启动
97+
8398
注意:这种方式适用emqx未启动之前
8499

100+
或者
101+
102+
可以在emqx启动后,在 dashboard 插件栏点击启用 `buildrun_emqx_backend_mysql` 即可启用成功
103+
85104
### 使用
86105

87-
此插件会把public发布的消息保存到mysql中,但并不是全部。
106+
此插件会把public发布的消息保存到mysql中,但并不是全部。也可以在业务规则中添加存储类型的资源。
88107

89108
需要在发布消息的参数中 retain 值设置为 true。 这样这条消息才会被保存在mysql中
90109

@@ -100,6 +119,14 @@ eg:
100119
}
101120
```
102121

122+
### 构建镜像
123+
124+
`emqx-rel` 项目自带构建镜像的功能
125+
126+
``` bash
127+
make emqx-docker-build
128+
```
129+
103130
### 最后
104131

105132
有什么问题和功能需求都可以给我提issue,欢迎关注。

TODO

Lines changed: 0 additions & 3 deletions
This file was deleted.

erlang环境配置.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# erlang环境配置
2+
3+
erlang 推荐使用 22 版本,经过测试用 23 版本编译出来运行有问题。各平台安装 erlang 详情请参考官方文档。 linux 系统可以使用 kerl 脚本安装,构建最新版本的 emqx 时注意编译的 gcc 版本不能用 10,编译通不过,推荐用 gcc-9
4+
5+
下载 kerl
6+
7+
``` bash
8+
curl -O https://raw.githubusercontent.com/kerl/kerl/master/kerl
9+
chmod +x kerl
10+
```
11+
12+
安装 erlang
13+
14+
``` bash
15+
kerl update releases
16+
# 如果直接构建报错,修改 ~/.kerl/builds/22.3/otp_src_22.3/Makefile
17+
# 将 gcc 改为 gcc9
18+
kerl build 22.3 22.3
19+
kerl install 22.3 /opt/erlang/22.3
20+
# 启用环境
21+
source /opt/erlang/22.3/activate
22+
# 设置环境变量,这样不用每次手动启用
23+
echo 'source /opt/erlang/22.3/activate' >> ~/.bashrc
24+
```
25+
26+
安装 rebar3
27+
28+
``` bash
29+
curl -OL https://s3.amazonaws.com/rebar3/rebar3
30+
./rebar3 local install
31+
# 然后根据输出操作
32+
```
33+
34+
<a id="orgeb79a2a"></a>
35+
36+
## emacs 编辑器配置
37+
38+
需要安装 erlang 包,提供 erlang-mode 再安装 erlang-lsp
39+
40+
``` bash
41+
git clone https://github.com/erlang-ls/erlang_ls.git
42+
cd erlang_ls && make
43+
```

etc/.DS_Store

-6 KB
Binary file not shown.

etc/buildrun_emqx_backend_mysql.conf

Lines changed: 0 additions & 18 deletions
This file was deleted.

etc/emqx_backend_mysql.conf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
##====================================================================
2+
## Configuration for EMQ X MySQL Backend
3+
##====================================================================
4+
5+
## MySQL Server
6+
backend.mysql.pool1.server = 127.0.0.1:3306
7+
8+
## MySQL Pool Size
9+
backend.mysql.pool1.pool_size = 8
10+
## MySQL Username
11+
backend.mysql.pool1.user = root
12+
## MySQL Password
13+
backend.mysql.pool1.password = public
14+
## MySQL Database
15+
backend.mysql.pool1.database = mqtt
16+
17+
## Client Connected Record
18+
backend.mysql.hook.client.connected.1 = {"action": {"function": "on_client_connected"}, "pool": "pool1"}
19+
20+
## Session Created Record
21+
backend.mysql.hook.client.connected.2 = {"action": {"function": "on_subscribe_lookup"}, "pool": "pool1"}
22+
23+
## Client DisConnected Record
24+
backend.mysql.hook.client.disconnected.1 = {"action": {"function": "on_client_disconnected"}, "pool": "pool1"}
25+
26+
## Lookup Unread Message QOS > 0
27+
backend.mysql.hook.session.subscribed.1 = {"topic": "#", "action": {"function": "on_message_fetch"}, "offline_opts": {"max_returned_count": 500, "time_range": "2h"}, "pool": "pool1"}
28+
29+
## Lookup Retain Message
30+
backend.mysql.hook.session.subscribed.2 = {"topic": "#", "action": {"function": "on_retain_lookup"}, "pool": "pool1"}
31+
32+
## Delete Acked Record
33+
backend.mysql.hook.session.unsubscribed.1= {"topic": "#", "action": {"sql": ["delete from mqtt_acked where clientid = ${clientid} and topic = ${topic}"]}, "pool": "pool1"}
34+
35+
## Store Publish Message QOS > 0
36+
backend.mysql.hook.message.publish.1 = {"topic": "#", "action": {"function": "on_message_publish"}, "pool": "pool1"}
37+
38+
## Store Retain Message
39+
backend.mysql.hook.message.publish.2 = {"topic": "#", "action": {"function": "on_message_retain"}, "pool": "pool1"}
40+
41+
## Delete Retain Message
42+
backend.mysql.hook.message.publish.3 = {"topic": "#", "action": {"function": "on_retain_delete"}, "pool": "pool1"}
43+
44+
## Store Ack
45+
backend.mysql.hook.message.acked.1 = {"topic": "#", "action": {"function": "on_message_acked"}, "pool": "pool1"}
46+
47+
## Max number of fetch offline messages
48+
## max_returned_count = 500
49+
50+
## Time Range
51+
## d - day
52+
## h - hour
53+
## m - minute
54+
## s - second
55+
## time_range = 2h
56+
## backend.mysql.hook.session.subscribed.1 = {"topic": "#", "action": {"function": "on_message_fetch"}, "offline_opts": {"max_returned_count": 500, "time_range": "2h"}, "pool": "pool1"}

include/buildrun_emqx_backend_mysql.hrl

Lines changed: 0 additions & 17 deletions
This file was deleted.

include/emqx_backend_mysql.hrl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-define(APP, emqx_backend_mysql).

priv/buildrun_emqx_backend_mysql.schema

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)