Grails3更换连接池为阿里巴巴Druid

Grails3默认数据库连接池为tomcat-jdbc pool,性能较druidHikariCP差太多,所以想换成其他连接池

  • 由于默认使用的是tomcat-jdbc连接池,需要去掉依赖
  1. // runtime "com.h2database:h2"
  2. // runtime "org.apache.tomcat:tomcat-jdbc"
  3. // 添加阿里巴巴druid连接池
  4. compile group: 'com.alibaba', name: 'druid', version: '1.1.18'
  5. compile 'mysql:mysql-connector-java:5.1.40'
  • grails-app/conf/applicaiton.yml配置也要注释掉默认的连接池
  1. dataSource:
  2. # pooled: true
  3. jmxExport: true
  4. driverClassName: com.mysql.jdbc.Driver
  5. username: root
  6. password: root
  7. dialect: org.hibernate.dialect.MySQL5InnoDBDialect
  8. url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
  9. # 为了兼容grails,让Hibernate自动建表,所以在dataSource下增加连接池相关的配置
  10. druid:
  11. minIdle: 1
  12. maxActive: 40
  13. maxWait: 60000
  14. initialSize: 20
  15. testWhileIdle: true
  16. testOnBorrow: false
  17. testOnReturn: false
  18. removeAbandoned: true
  19. filters: stat,wall,slf4j
  20. validationQuery: select 'x'
  21. useGlobalDataSourceStat: true
  22. poolPreparedStatements: true
  23. minEvictableIdleTimeMillis: 300000
  24. timeBetweenEvictionRunsMillis: 60000
  25. connectionProperties: clientEncoding=UTF-8
  26. maxPoolPreparedStatementPerConnectionSize: 20
  27. environments:
  28. development:
  29. dataSource:
  30. dbCreate: create-drop
  31. url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
  32. test:
  33. dataSource:
  34. dbCreate: update
  35. url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
  36. production:
  37. dataSource:
  38. dbCreate: update
  39. url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
  40. server:
  41. port: 8081 # TRANSACTION_READ_COMMITTED
  • grails-app/conf/spring/resources.groovy配置
  1. // Place your Spring DSL code here
  2. beans = {
  3. // 监控管理页面配置(管理用户名、密码)
  4. druidConsoleServlet(org.springframework.boot.web.servlet.ServletRegistrationBean) {
  5. servlet = bean(com.alibaba.druid.support.http.StatViewServlet)
  6. urlMappings = ["/druid/*"]
  7. // urlMappings = ["*.js","*.gif","*.jpg","*.png","*.css","*.ico","/druid/*"]
  8. initParameters = [
  9. "loginUsername": "root",
  10. "loginPassword": "123456",
  11. "allow": "127.0.0.1",
  12. "deny": ""
  13. ]
  14. // loadOnStartup = 2
  15. }
  16. // alibaba druid配置
  17. dataSource(com.alibaba.druid.pool.DruidDataSource) { bean ->
  18. bean.initMethod = 'init'
  19. bean.destroyMethod = 'close'
  20. driverClassName = grailsApplication.config.dataSource.driverClassName
  21. url = grailsApplication.config.dataSource.url
  22. username = grailsApplication.config.dataSource.username
  23. password = grailsApplication.config.dataSource.password
  24. initialSize = grailsApplication.config.dataSource.druid.initialSize
  25. minIdle = grailsApplication.config.dataSource.druid.minIdle
  26. maxActive = grailsApplication.config.dataSource.druid.maxActive
  27. maxWait = grailsApplication.config.dataSource.druid.maxWait
  28. timeBetweenEvictionRunsMillis = grailsApplication.config.dataSource.druid.timeBetweenEvictionRunsMillis
  29. minEvictableIdleTimeMillis = grailsApplication.config.dataSource.druid.minEvictableIdleTimeMillis
  30. validationQuery = grailsApplication.config.dataSource.druid.validationQuery
  31. removeAbandoned = grailsApplication.config.dataSource.druid.removeAbandoned
  32. testWhileIdle = grailsApplication.config.dataSource.druid.testWhileIdle
  33. connectionProperties = grailsApplication.config.dataSource.druid.connectionProperties
  34. testOnBorrow = grailsApplication.config.dataSource.druid.testOnBorrow
  35. poolPreparedStatements = grailsApplication.config.dataSource.druid.poolPreparedStatements
  36. testOnReturn = grailsApplication.config.dataSource.druid.testOnReturn
  37. useGlobalDataSourceStat = grailsApplication.config.dataSource.druid.useGlobalDataSourceStat
  38. maxPoolPreparedStatementPerConnectionSize = grailsApplication.config.dataSource.druid.maxPoolPreparedStatementPerConnectionSize
  39. filters = grailsApplication.config.dataSource.druid.filters
  40. }
  41. }
  • grails-app/conf/logback.groovy添加日志配置
  1. // 日志采集
  2. logger('com.alibaba', DEBUG, ['STDOUT'], false)
  • 启动项目,出现日志
  1. 2019-07-03 03:15:06.691 WARN --- [ main] com.alibaba.druid.pool.DruidDataSource : removeAbandoned is true, not use in productiion.
  2. 2019-07-03 03:15:07.078 INFO --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
  3. Grails application running at http://localhost:8081 in environment: development
  • web监控地址
  1. http://127.0.0.1:8081/druid/index.html

总结

web监控无法记录sql日志,防火墙也没用起来,只是连接池生效了,如何测试连接池生效呢?关闭数据库服务,访问一下,控制台报无法连接数据库的错误,重启数据库服务,再次刷新页面,数据库连接上,能够取到数据,希望有兴趣的朋友可以研究下如何在web端能跟踪sql,以及把防火墙用起来。

参考

参考地址1
参考地址2
Grails2参考



0
0
0

添加评论

正在回复:
取消
2
0
0
0