Skip to content

Commit a20e0c8

Browse files
author
litongjava
committed
add tdengine
1 parent 516fc8e commit a20e0c8

File tree

3 files changed

+323
-53
lines changed

3 files changed

+323
-53
lines changed

docs/.vuepress/config/sidebar-zh.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
"15_table-to-json/02.md",
127127
"15_table-to-json/03.md",
128128
"15_table-to-json/04.md",
129-
"15_table-to-json/05.md"
129+
"15_table-to-json/05.md",
130+
"15_table-to-json/06.md"
130131
]
131132
},
132133
{

docs/zh/15_table-to-json/05.md

+15-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
# table-json 多数据源 mysql and tdengine
1+
# table-json 整合 tdengine
22

3-
如何在 tio-boot 应用程序中配置多个数据源,这里以 MySQL 和 TDengine 为例。
3+
#### 添加配置信息
4+
5+
app.properties
6+
7+
```properties
8+
tdengine.host=192.168.3.9
9+
tdengine.port=6041
10+
tdengine.username=root
11+
tdengine.password=root123
12+
tdengine.database=manliang_pen
13+
tdengine.driverClassName=com.taosdata.jdbc.rs.RestfulDriver
14+
```
415

516
#### ActiveRecordPluginConfiguration 类
617

@@ -27,53 +38,7 @@ public class ActiveRecordPluginConfiguration {
2738

2839
@AInitialization
2940
public void activeRecordPlugin() throws Exception {
30-
configArpForMysql();
3141
configArpForTdEngine();
32-
33-
}
34-
35-
public DataSource getMysqlDataSource() {
36-
String jdbcUrl = EnvironmentUtils.get("jdbc.url");
37-
String jdbcUser = EnvironmentUtils.get("jdbc.user");
38-
String jdbcPswd = EnvironmentUtils.get("jdbc.pswd");
39-
int maximumPoolSize = EnvironmentUtils.getInt("jdbc.MaximumPoolSize", 2);
40-
41-
HikariConfig config = new HikariConfig();
42-
// 设定基本参数
43-
config.setJdbcUrl(jdbcUrl);
44-
config.setUsername(jdbcUser);
45-
config.setPassword(jdbcPswd);
46-
config.setMaximumPoolSize(maximumPoolSize);
47-
48-
HikariDataSource hikariDataSource = new HikariDataSource(config);
49-
TioBootServer.addDestroyMethod(hikariDataSource::close);
50-
return hikariDataSource;
51-
}
52-
53-
public ActiveRecordPlugin configArpForMysql() {
54-
String property = EnvironmentUtils.get(ConfigKeys.APP_ENV);
55-
56-
DataSource mysqlDataSource = getMysqlDataSource();
57-
ActiveRecordPlugin mysqlArp = new ActiveRecordPlugin(mysqlDataSource);
58-
mysqlArp.setContainerFactory(new OrderedFieldContainerFactory());
59-
60-
Engine engine = mysqlArp.getEngine();
61-
engine.setSourceFactory(new ClassPathSourceFactory());
62-
engine.setCompressorOn(' ');
63-
engine.setCompressorOn('\n');
64-
65-
if ("dev".equals(property)) {
66-
mysqlArp.setDevMode(true);
67-
engine.setDevMode(true);
68-
mysqlArp.setShowSql(true);
69-
}
70-
71-
mysqlArp.addSqlTemplate("/sql/all_sqls.sql");
72-
mysqlArp.start();
73-
TioBootServer.addDestroyMethod(mysqlArp::stop);
74-
// add
75-
TioRequestParamUtils.types.add("bigint");
76-
return mysqlArp;
7742
}
7843

7944
public DataSource getTdengineDataSource() {
@@ -132,14 +97,12 @@ public class ActiveRecordPluginConfiguration {
13297
```
13398

13499
1. **数据源配置**:
135-
- `getMysqlDataSource()`: 此方法创建并配置 MySQL 的数据源。使用 HikariCP 作为连接池管理器。
136100
- `getTdengineDataSource()`: 此方法创建并配置 TDengine 的数据源。同样使用 HikariCP,并设置了针对 TDengine 特有的属性和连接测试查询。
137101
2. **ActiveRecordPlugin 配置**:
138102

139-
- `configArpForMysql()`: 为 MySQL 配置 ActiveRecordPlugin。设置了 SQL 模板路径、开发模式、SQL 显示等。
140103
- `configArpForTdEngine()`: 为 TDengine 配置 ActiveRecordPlugin。同样设置了开发模式和 SQL 显示,数据库标识名为 "tdengine"。
141104

142-
3. **资源清理**: 在 `getMysqlDataSource()``getTdengineDataSource()` 方法中,使用 `TioBootServer.addDestroyMethod` 来确保应用停止时数据源能够正确关闭。
105+
3. **资源清理**: 在 `getTdengineDataSource()` 方法中,使用 `TioBootServer.addDestroyMethod` 来确保应用停止时数据源能够正确关闭。
143106

144107
#### 测试类
145108

@@ -303,4 +266,4 @@ public class PenRawDataControllerTest {
303266
System.out.println(dbJsonBean);
304267
}
305268
}
306-
···
269+
```

0 commit comments

Comments
 (0)