-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
microrain
authored and
microrain
committed
Dec 24, 2024
1 parent
d8236da
commit a42cbac
Showing
22 changed files
with
3,661 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"label": "外部的", | ||
"position": 4, | ||
"link": { | ||
"type": "generated-index" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
--- | ||
title: '数据库客户端' | ||
sidebar_position: 1 | ||
hide_title: true | ||
keywords: [规则引擎,数据库,SQL,MySQL,PostgreSQL] | ||
description: '详细介绍SagooIOT平台规则引擎中数据库客户端节点的功能和使用方法,包括数据库连接配置、SQL操作、变量替换等内容,帮助用户实现数据库操作功能。' | ||
--- | ||
|
||
|
||
数据库客户端节点是一个用于执行SQL操作的组件,支持多种数据库类型,可以执行查询、更新、插入和删除等操作。该组件基于Go标准库的database/sql接口实现,支持任何实现了该接口的第三方数据库驱动。 | ||
|
||
## 基本信息 | ||
|
||
- **组件类型**: `dbClient` | ||
- **功能**: 执行SQL操作 | ||
- **支持数据库**: MySQL、PostgreSQL等多种数据库 | ||
|
||
## 配置说明 | ||
|
||
### 1. 基本配置 | ||
| 字段 | 类型 | 必填 | 说明 | 默认值 | | ||
|------|------|------|------|--------| | ||
| driverName | string | 是 | 数据库驱动名称 | mysql | | ||
| dsn | string | 是 | 数据库连接字符串 | - | | ||
| poolSize | int | 否 | 连接池大小 | - | | ||
| sql | string | 是 | SQL语句 | - | | ||
| params | array | 否 | SQL参数列表 | - | | ||
| getOne | bool | 否 | 是否只返回一条记录 | false | | ||
|
||
### 2. 连接字符串格式 | ||
不同数据库的DSN格式: | ||
|
||
**MySQL格式**: | ||
``` | ||
username:password@tcp(host:port)/database | ||
``` | ||
|
||
**PostgreSQL格式**: | ||
``` | ||
postgres://username:password@host:port/database?sslmode=disable | ||
``` | ||
|
||
## 功能特性 | ||
|
||
### 1. SQL操作支持 | ||
- **查询(SELECT)** | ||
- **更新(UPDATE)** | ||
- **插入(INSERT)** | ||
- **删除(DELETE)** | ||
|
||
### 2. 变量支持 | ||
SQL语句和参数支持使用变量: | ||
- `${metadata.key}`: 使用元数据中的值 | ||
- `${msg.key}`: 使用消息中的值 | ||
|
||
### 3. 连接池管理 | ||
- 支持连接池配置 | ||
- 自动管理连接生命周期 | ||
- 支持连接复用 | ||
|
||
## 使用示例 | ||
|
||
### 1. 基础查询配置 | ||
```json | ||
{ | ||
"id": "node1", | ||
"type": "dbClient", | ||
"name": "查询数据", | ||
"configuration": { | ||
"driverName": "mysql", | ||
"dsn": "root:root@tcp(127.0.0.1:3306)/test", | ||
"sql": "SELECT * FROM users WHERE age > ?", | ||
"params": [18] | ||
} | ||
} | ||
``` | ||
|
||
### 2. 带变量的更新操作 | ||
```json | ||
{ | ||
"id": "node2", | ||
"type": "dbClient", | ||
"name": "更新数据", | ||
"configuration": { | ||
"driverName": "mysql", | ||
"dsn": "root:root@tcp(127.0.0.1:3306)/test", | ||
"sql": "UPDATE users SET status = ? WHERE id = ${msg.userId}", | ||
"params": ["active"] | ||
} | ||
} | ||
``` | ||
|
||
### 3. 插入操作 | ||
```json | ||
{ | ||
"id": "node3", | ||
"type": "dbClient", | ||
"name": "插入数据", | ||
"configuration": { | ||
"driverName": "mysql", | ||
"dsn": "root:root@tcp(127.0.0.1:3306)/test", | ||
"sql": "INSERT INTO logs (device_id, message) VALUES (?, ?)", | ||
"params": ["${metadata.deviceId}", "${msg.content}"] | ||
} | ||
} | ||
``` | ||
|
||
## 执行结果 | ||
|
||
### 1. 查询操作 | ||
- 结果保存在`msg.Data`中 | ||
- `getOne=true`时返回单个对象 | ||
- `getOne=false`时返回数组 | ||
|
||
### 2. 更新操作 | ||
结果保存在`msg.Metadata`中: | ||
- `rowsAffected`: 影响的行数 | ||
- 原始`msg.Data`保持不变 | ||
|
||
### 3. 插入操作 | ||
结果保存在`msg.Metadata`中: | ||
- `rowsAffected`: 影响的行数 | ||
- `lastInsertId`: 最后插入的ID(如果有) | ||
- 原始`msg.Data`保持不变 | ||
|
||
### 4. 删除操作 | ||
结果保存在`msg.Metadata`中: | ||
- `rowsAffected`: 影响的行数 | ||
- 原始`msg.Data`保持不变 | ||
|
||
## 第三方数据库支持 | ||
|
||
### 1. 支持的数据库和驱动 | ||
| 数据库类型 | 驱动包 | driverName | DSN格式示例 | | ||
|-----------|--------|------------|-------------| | ||
| MySQL | github.com/go-sql-driver/mysql | mysql | root:root@tcp(127.0.0.1:3306)/test | | ||
| PostgreSQL | github.com/lib/pq | postgres | postgres://user:pass@127.0.0.1:5432/test?sslmode=disable | | ||
| Microsoft SQL Server | github.com/denisenkom/go-mssqldb | mssql | server=127.0.0.1;user id=root;password=root;database=test | | ||
| Oracle | github.com/godror/godror | oracle | username/password@//127.0.0.1:1521/test | | ||
| TDengine | github.com/taosdata/driver-go/v3/taosRestful | taosRestful | root:root@tcp(127.0.0.1:6030)/test | | ||
|
||
### 2. 添加新的数据库支持 | ||
导入相应的驱动包: | ||
```go | ||
import ( | ||
_ "github.com/taosdata/driver-go/v3/taosRestful" | ||
) | ||
``` | ||
|
||
## 最佳实践 | ||
|
||
### 1. 连接管理 | ||
- 合理设置连接池大小 | ||
- 及时关闭不需要的连接 | ||
- 使用连接复用机制 | ||
|
||
### 2. SQL操作 | ||
- 使用参数化查询防止SQL注入 | ||
- 合理使用事务 | ||
- 注意SQL性能优化 | ||
|
||
### 3. 错误处理 | ||
- 检查SQL语法 | ||
- 处理连接异常 | ||
- 记录错误日志 | ||
|
||
## 注意事项 | ||
|
||
1. **安全性** | ||
- 避免SQL注入风险 | ||
- 妥善保管数据库凭证 | ||
- 限制数据库操作权限 | ||
|
||
2. **性能优化** | ||
- 合理使用连接池 | ||
- 优化SQL查询 | ||
- 避免大量数据操作 | ||
|
||
3. **错误处理** | ||
- 处理连接超时 | ||
- 处理查询错误 | ||
- 实现重试机制 | ||
|
||
通过合理配置和使用数据库客户端节点,您可以实现各种数据库操作功能。请根据实际需求选择合适的配置选项,并注意遵循安全和性能优化建议。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
--- | ||
title: '发送邮件' | ||
sidebar_position: 5 | ||
hide_title: true | ||
keywords: [规则引擎,邮件发送,SMTP,邮件通知] | ||
description: '详细介绍SagooIOT平台规则引擎中发送邮件节点的功能和使用方法,包括SMTP配置、TLS加密、邮件模板等内容,帮助用户实现邮件通知功能。' | ||
--- | ||
|
||
|
||
发送邮件节点是一个用于发送电子邮件的组件,支持HTML和纯文本格式,可以配置多个收件人、抄送和密送,并支持TLS加密连接。该组件还支持使用模板变量动态生成邮件主题和内容。 | ||
|
||
## 基本信息 | ||
|
||
- **组件类型**: `sendEmail` | ||
- **功能**: 发送HTML或纯文本格式的电子邮件 | ||
- **支持特性**: | ||
- 多收件人 | ||
- 抄送/密送 | ||
- TLS加密 | ||
- 变量替换 | ||
- HTML格式 | ||
|
||
## 配置说明 | ||
|
||
### 1. SMTP服务器配置 | ||
|
||
| 字段 | 类型 | 必填 | 说明 | 默认值 | | ||
|------|------|------|------|--------| | ||
| smtpHost | string | 是 | SMTP服务器地址 | - | | ||
| smtpPort | int | 是 | SMTP服务器端口 | - | | ||
| username | string | 是 | SMTP认证用户名 | - | | ||
| password | string | 是 | SMTP认证密码 | - | | ||
| enableTls | bool | 否 | 是否启用TLS | false | | ||
| connectTimeout | int | 否 | 连接超时时间(秒) | 10 | | ||
|
||
### 2. 邮件内容配置 | ||
|
||
| 字段 | 类型 | 必填 | 说明 | 默认值 | | ||
|------|------|------|------|--------| | ||
| from | string | 是 | 发件人地址 | - | | ||
| to | string | 是 | 收件人列表 | - | | ||
| cc | string | 否 | 抄送列表 | - | | ||
| bcc | string | 否 | 密送列表 | - | | ||
| subject | string | 是 | 邮件主题 | - | | ||
| body | string | 是 | 邮件正文 | - | | ||
|
||
## 功能特性 | ||
|
||
### 1. 邮件格式 | ||
- 支持HTML富文本 | ||
- 支持纯文本格式 | ||
- 自动处理字符编码 | ||
|
||
### 2. 收件人管理 | ||
- 支持多个收件人 | ||
- 支持抄送(CC) | ||
- 支持密送(BCC) | ||
- 自动验证邮箱格式 | ||
|
||
### 3. 安全特性 | ||
- TLS加密支持 | ||
- SMTP认证 | ||
- 密码保护 | ||
- 连接超时控制 | ||
|
||
### 4. 模板变量 | ||
- 支持主题变量替换 | ||
- 支持正文变量替换 | ||
- 支持元数据引用 | ||
|
||
## 使用示例 | ||
|
||
### 1. 基础邮件配置 | ||
```json | ||
{ | ||
"id": "email_node_1", | ||
"type": "sendEmail", | ||
"name": "邮件通知", | ||
"configuration": { | ||
"smtpHost": "smtp.example.com", | ||
"smtpPort": 587, | ||
"username": "[email protected]", | ||
"password": "your-password", | ||
"enableTls": true, | ||
"email": { | ||
"from": "[email protected]", | ||
"to": "[email protected]", | ||
"subject": "测试邮件", | ||
"body": "这是一封测试邮件" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### 2. HTML格式邮件 | ||
```json | ||
{ | ||
"id": "html_email_node", | ||
"type": "sendEmail", | ||
"name": "HTML邮件", | ||
"configuration": { | ||
"smtpHost": "smtp.example.com", | ||
"smtpPort": 587, | ||
"username": "[email protected]", | ||
"password": "your-password", | ||
"enableTls": true, | ||
"email": { | ||
"from": "[email protected]", | ||
"to": "[email protected],[email protected]", | ||
"cc": "[email protected]", | ||
"subject": "HTML测试邮件", | ||
"body": "<h1>测试邮件</h1><p>这是一封<strong>HTML</strong>格式的测试邮件</p>" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### 3. 使用变量的配置 | ||
```json | ||
{ | ||
"id": "template_email_node", | ||
"type": "sendEmail", | ||
"name": "模板邮件", | ||
"configuration": { | ||
"smtpHost": "smtp.example.com", | ||
"smtpPort": 587, | ||
"username": "[email protected]", | ||
"password": "your-password", | ||
"enableTls": true, | ||
"email": { | ||
"from": "[email protected]", | ||
"to": "[email protected]", | ||
"subject": "警报: ${alarmType}", | ||
"body": "设备 ${deviceName} 在 ${timestamp} 触发了 ${alarmType} 警报" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## 执行结果 | ||
|
||
### 1. 成功场景 | ||
- 邮件成功发送到所有收件人 | ||
- 原始消息内容保持不变 | ||
- 消息发送到Success链路 | ||
|
||
### 2. 失败场景 | ||
- SMTP连接失败 | ||
- 认证失败 | ||
- 发送超时 | ||
- 收件人地址无效 | ||
- 邮件内容格式错误 | ||
- 在metadata中添加error字段 | ||
- 消息发送到Failure链路 | ||
|
||
## 最佳实践 | ||
|
||
### 1. SMTP配置 | ||
- 使用TLS加密保护通信安全 | ||
- 使用授权码替代登录密码 | ||
- 设置合适的超时时间 | ||
- 正确配置端口号 | ||
|
||
### 2. 邮件内容 | ||
- 使用有意义的发件人地址 | ||
- 合理使用抄送和密送 | ||
- HTML内容注意兼容性 | ||
- 避免发送过大附件 | ||
|
||
### 3. 变量使用 | ||
- 合理规划变量命名 | ||
- 提供默认值处理 | ||
- 注意转义特殊字符 | ||
- 验证变量有效性 | ||
|
||
## 注意事项 | ||
|
||
1. **安全性** | ||
- 不要在配置中明文存储密码 | ||
- 使用TLS加密保护通信 | ||
- 注意保护授权码安全 | ||
- 避免暴露敏感信息 | ||
|
||
2. **性能** | ||
- 控制邮件大小 | ||
- 合理设置超时时间 | ||
- 避免频繁发送 | ||
- 注意并发控制 | ||
|
||
3. **兼容性** | ||
- 测试不同邮件客户端 | ||
- 验证HTML格式兼容性 | ||
- 检查字符编码 | ||
- 注意附件格式 | ||
|
||
4. **特殊说明** | ||
- 主流邮件服务商需要使用授权码 | ||
- 不同服务商的端口可能不同 | ||
- TLS端口一般为587或465 | ||
- 变量使用`${key}`格式引用 | ||
|
||
通过合理配置和使用发送邮件节点,您可以实现可靠的邮件通知功能。请根据实际需求选择合适的配置选项,并注意遵循安全和性能优化建议。 |
Oops, something went wrong.