1.使用的druid版本
<!-- 阿里数据库连接池 --> <dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.21</version> </dependency>
2.配置文件
# Spring配置 spring:# 数据源配置datasource:type: com.alibaba.druid.pool.DruidDataSource#开启多数据源取消这个配置在master和slave下配置#driverClassName: com.mysql.cj.jdbc.Driverdruid:# 主库数据源master:url: jdbc:mysql://192.168.1.23:3306/ccis_cat?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: V2MQgERfNwkoYXkiMcYLCsy45HAEc9EVrhR5VNAPTwFx4ycayVWvlqxkYYCQuujccoVFJ5MZVROkVyjUgsl2NQ==#DruidEncryptorUtils类生成密码和公钥,之后开启druid:filter:config:enabled=truepublicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMCh+KGwVH6CERudOz1Fyr+kgUSvjxwcbuv9b8rKAv++lwTA9iLqXc+eUW37PUWns+4+uJ6Y2WRsO084RmXhsi0CAwEAAQ==connectionProperties: config.decrypt=true;config.decrypt.key=${spring.datasource.druid.master.publicKey}slave:enabled: falseurl: jdbc:sqlserver://192.168.1.224:1433;SelectMethod=cursor;DatabaseName=genocsusername: sapassword: 123# 初始连接数initialSize: 5# 最小连接池数量minIdle: 10# 最大连接池数量maxActive: 20# 配置获取连接等待超时的时间maxWait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒minEvictableIdleTimeMillis: 300000# 配置一个连接在池中最大生存的时间,单位是毫秒maxEvictableIdleTimeMillis: 900000# 配置检测连接是否有效validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsewebStatFilter:enabled: truestatViewServlet:enabled: true# 设置白名单,不填则允许所有访问allow:url-pattern: /druid/*# 控制台管理用户名和密码login-username: adminlogin-password: en,.@1999filter:stat:enabled: true# 慢SQL记录log-slow-sql: trueslow-sql-millis: 1000merge-sql: truewall:config:multi-statement-allow: trueconfig:enabled: true
3.需要注意的地方
- 生成的公钥和密码配置在这里,用下面的工具类生成
#DruidEncryptorUtils类生成密码和公钥,之后开启druid:filter:config:enabled=true publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMCh+KGwVH6CERudOz1Fyr+kgUSvjxwcbuv9b8rKAv++lwTA9iLqXc+eUW37PUWns+4+uJ6Y2WRsO084RmXhsi0CAwEAAQ==
connectionProperties: config.decrypt=true;config.decrypt.key=${spring.datasource.druid.master.publicKey}
- 这个配置是必须开启的
config:
enabled: true
4.生成密码工具类
- 生成方法
import com.alibaba.druid.filter.config.ConfigTools;public class DruidEncryptorUtils {public static void main(String[] args) {try {String password = "G@2023";String[] arr = ConfigTools.genKeyPair(512);//System.out.println("privateKey:" + arr[0]);System.out.println("publicKey:" + arr[1]);System.out.println("password:" + ConfigTools.encrypt(arr[0], password));} catch (Exception e) {e.printStackTrace();}} }
- 生成的公钥和密码
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOkMzk15jEt3savaAVd+u7ZNidHJSJrOEmwloLqfAPbqtBvyzj5GQ6HViOUUB1WaXxjTdwxG9u/cE85NTMvDSZ0CAwEAAQ==
password:Q/RmtiEqStjze1g8zbj7uOOBColrcLNUJ7bEWKD6kSt9WTzTvXHjdOn4o/HmXqE7knH+4P44UbIWZzrKEJgXVA==