1
- # table-json 多数据源 mysql and tdengine
1
+ # table-json 整合 tdengine
2
2
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
+ ```
4
15
5
16
#### ActiveRecordPluginConfiguration 类
6
17
@@ -27,53 +38,7 @@ public class ActiveRecordPluginConfiguration {
27
38
28
39
@AInitialization
29
40
public void activeRecordPlugin () throws Exception {
30
- configArpForMysql();
31
41
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;
77
42
}
78
43
79
44
public DataSource getTdengineDataSource () {
@@ -132,14 +97,12 @@ public class ActiveRecordPluginConfiguration {
132
97
```
133
98
134
99
1 . ** 数据源配置** :
135
- - ` getMysqlDataSource() ` : 此方法创建并配置 MySQL 的数据源。使用 HikariCP 作为连接池管理器。
136
100
- ` getTdengineDataSource() ` : 此方法创建并配置 TDengine 的数据源。同样使用 HikariCP,并设置了针对 TDengine 特有的属性和连接测试查询。
137
101
2 . ** ActiveRecordPlugin 配置** :
138
102
139
- - ` configArpForMysql() ` : 为 MySQL 配置 ActiveRecordPlugin。设置了 SQL 模板路径、开发模式、SQL 显示等。
140
103
- ` configArpForTdEngine() ` : 为 TDengine 配置 ActiveRecordPlugin。同样设置了开发模式和 SQL 显示,数据库标识名为 "tdengine"。
141
104
142
- 3 . ** 资源清理** : 在 ` getMysqlDataSource() ` 和 ` getTdengineDataSource() ` 方法中,使用 ` TioBootServer.addDestroyMethod ` 来确保应用停止时数据源能够正确关闭。
105
+ 3 . ** 资源清理** : 在 ` getTdengineDataSource() ` 方法中,使用 ` TioBootServer.addDestroyMethod ` 来确保应用停止时数据源能够正确关闭。
143
106
144
107
#### 测试类
145
108
@@ -303,4 +266,4 @@ public class PenRawDataControllerTest {
303
266
System . out. println(dbJsonBean);
304
267
}
305
268
}
306
- ···
269
+ ```
0 commit comments