1.起因
项目使用了 Greenplum 数据库和 MySQL 数据库,连接池使用的是 Druid,但是 Druid 是不支持 Greenplum 数据库的 😢 以下尝试未在生成环境测试请谨慎使用
。
2.依赖
只贴出核心依赖:
<!--连接池 Druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--多数据源管理 Dynamic--> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.4.2</version> </dependency> <!--数据源 MySQL+PostgreSQL(用于连接Greenplum)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
特别注意 如果直接使用greenplum
的依赖和配置将会报错:
java.lang.IllegalStateException: dbType not support : null, url jdbc:pivotal:greenplum://xxx.xx.xxx.xxx:5432:5432/postgres
<dependency> <groupId>com.jdbc</groupId> <artifactId>greenplum</artifactId> <version>5.1.4</version> </dependency>
spring: datasource: dynamic: datasource: gp: url: jdbc:pivotal:greenplum://xxx.xx.xxx.xxx:5432;DatabaseName=postgres username: gpadmin password: gpadmin driver-class-name: com.pivotal.jdbc.GreenplumDriver
3.配置
只贴出核心配置:
spring: # 多数据源不自动配置DruidDataSourceAutoConfigure autoconfigure: exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # datasource: druid: stat-view-servlet: enabled: true loginUsername: admin loginPassword: admin allow: web-stat-filter: enabled: true dynamic: # 全局 druid 参数配置(单独数据源配置为空时取全局配置)这里不再详细说明 druid: initial-size: 1 min-idle: 3 maxActive: 8 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 # 不可使用 SELECT 1 FROM DUAL GP数据库没有DUAL validationQuery: SELECT NOW() testWhileIdle: true testOnBorrow: false testOnReturn: false filters: stat,wall,slf4j connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 primary: mysql p6spy: true datasource: mysql: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver gp: url: jdbc:postgresql://xxx.xx.xxx.xxx:5432/postgres username: gpadmin password: gpadmin driver-class-name: org.postgresql.Driver
4.测试
启动项目,数据源初始化成功。
2022-05-26 14:16:14.836 INFO 12324 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-1,mysql} inited 2022-05-26 14:16:15.114 INFO 12324 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-2,gp} inited
查询 Greenplum 数据库也没有问题,SQL 监控也能够监控 Greenplum 数据库的执行情况,未在生成环境测试请谨慎使用
。
5. hikari
仅贴出核心配置:
spring: datasource: hikari: pool-name: HikariConPool #最小空闲连接数 minimum-idle: 3 #空闲链接最大存活时间,默认600000(10分钟) idle-timeout: 1800000 #最大连接数 maximum-pool-size: 20 #从连接池返回的链接自动提交 auto-commit: true #链接最大存活时间,0表示永久存活,默认1800000(30分钟) max-lifetime: 1800000 #连接超时时间,默认(30秒) connection-timeout: 30000
如果想验证hikari
的使用情况可添加日志配置:
logging: level: com.zaxxer.hikari.HikariConfig: debug com.zaxxer.hikari: debug
启动信息:
2022-05-26 17:08:04.217 INFO 10108 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : mysql - Starting... 2022-05-26 17:08:04.301 INFO 10108 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : mysql - Start completed. 2022-05-26 17:08:04.301 INFO 10108 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : gp - Starting... 2022-05-26 17:08:04.592 INFO 10108 --- [ restartedMain] com.zaxxer.hikari.pool.PoolBase : gp - Driver does not support get/set network timeout for connections. ([Pivotal][Greenplum JDBC Driver]This driver does not support the 'getNetworkTimeout' and 'setNetworkTimeout' methods.) 2022-05-26 17:08:04.592 INFO 10108 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : gp - Start completed.