Grails3更換鏈接池爲阿里巴巴Druid

Grails3默認數據庫鏈接池爲tomcat-jdbc pool,性能較druidHikariCP差太多,因此想換成其餘鏈接池css

  • 因爲默認使用的是tomcat-jdbc鏈接池,須要去掉依賴
// runtime "com.h2database:h2"
// runtime "org.apache.tomcat:tomcat-jdbc"

// 添加阿里巴巴druid鏈接池
compile group: 'com.alibaba', name: 'druid', version: '1.1.18'
compile 'mysql:mysql-connector-java:5.1.40'
複製代碼
  • grails-app/conf/applicaiton.yml配置也要註釋掉默認的鏈接池
dataSource:
# pooled: true
 jmxExport: true
 driverClassName: com.mysql.jdbc.Driver
 username: root
 password: root
 dialect: org.hibernate.dialect.MySQL5InnoDBDialect
 url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
    # 爲了兼容grails,讓Hibernate自動建表,因此在dataSource下增長鏈接池相關的配置
 druid:
 minIdle: 1
 maxActive: 40
 maxWait: 60000
 initialSize: 20
 testWhileIdle: true
 testOnBorrow: false
 testOnReturn: false
 removeAbandoned: true
 filters: stat,wall,slf4j
 validationQuery: select 'x'
 useGlobalDataSourceStat: true
 poolPreparedStatements: true
 minEvictableIdleTimeMillis: 300000
 timeBetweenEvictionRunsMillis: 60000
 connectionProperties: clientEncoding=UTF-8
 maxPoolPreparedStatementPerConnectionSize: 20
environments:
 development:
 dataSource:
 dbCreate: create-drop
 url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
 test:
 dataSource:
 dbCreate: update
 url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
 production:
 dataSource:
 dbCreate: update
 url: jdbc:mysql://localhost:3306/g3?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
server:
 port: 8081  # TRANSACTION_READ_COMMITTED
複製代碼
  • grails-app/conf/spring/resources.groovy配置
// Place your Spring DSL code here
beans = {

    // 監控管理頁面配置(管理用戶名、密碼)
    druidConsoleServlet(org.springframework.boot.web.servlet.ServletRegistrationBean) {
        servlet = bean(com.alibaba.druid.support.http.StatViewServlet)
        urlMappings = ["/druid/*"]
// urlMappings = ["*.js","*.gif","*.jpg","*.png","*.css","*.ico","/druid/*"]
        initParameters = [
                "loginUsername": "root",
                "loginPassword": "123456",
                "allow": "127.0.0.1",
                "deny": ""
        ]
// loadOnStartup = 2
    }


    // alibaba druid配置
    dataSource(com.alibaba.druid.pool.DruidDataSource) { bean ->
        bean.initMethod = 'init'
        bean.destroyMethod = 'close'
        driverClassName = grailsApplication.config.dataSource.driverClassName
        url = grailsApplication.config.dataSource.url
        username = grailsApplication.config.dataSource.username
        password = grailsApplication.config.dataSource.password
        initialSize = grailsApplication.config.dataSource.druid.initialSize
        minIdle = grailsApplication.config.dataSource.druid.minIdle
        maxActive = grailsApplication.config.dataSource.druid.maxActive
        maxWait = grailsApplication.config.dataSource.druid.maxWait
        timeBetweenEvictionRunsMillis = grailsApplication.config.dataSource.druid.timeBetweenEvictionRunsMillis
        minEvictableIdleTimeMillis = grailsApplication.config.dataSource.druid.minEvictableIdleTimeMillis
        validationQuery = grailsApplication.config.dataSource.druid.validationQuery
        removeAbandoned = grailsApplication.config.dataSource.druid.removeAbandoned
        testWhileIdle = grailsApplication.config.dataSource.druid.testWhileIdle
        connectionProperties = grailsApplication.config.dataSource.druid.connectionProperties
        testOnBorrow = grailsApplication.config.dataSource.druid.testOnBorrow
        poolPreparedStatements = grailsApplication.config.dataSource.druid.poolPreparedStatements
        testOnReturn = grailsApplication.config.dataSource.druid.testOnReturn
        useGlobalDataSourceStat = grailsApplication.config.dataSource.druid.useGlobalDataSourceStat
        maxPoolPreparedStatementPerConnectionSize = grailsApplication.config.dataSource.druid.maxPoolPreparedStatementPerConnectionSize
        filters = grailsApplication.config.dataSource.druid.filters
    }
}
複製代碼
  • grails-app/conf/logback.groovy添加日誌配置
// 日誌採集
logger('com.alibaba', DEBUG, ['STDOUT'], false)
複製代碼
  • 啓動項目,出現日誌
2019-07-03 03:15:06.691  WARN --- [           main] com.alibaba.druid.pool.DruidDataSource   : removeAbandoned is true, not use in productiion.
2019-07-03 03:15:07.078  INFO --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
Grails application running at http://localhost:8081 in environment: development
複製代碼
  • web監控地址
http://127.0.0.1:8081/druid/index.html
複製代碼

總結

web監控沒法記錄sql日誌,防火牆也沒用起來,只是鏈接池生效了,如何測試鏈接池生效呢?關閉數據庫服務,訪問一下,控制檯報沒法鏈接數據庫的錯誤,重啓數據庫服務,再次刷新頁面,數據庫鏈接上,可以取到數據,但願有興趣的朋友能夠研究下如何在web端能跟蹤sql,以及把防火牆用起來。html

參考

參考地址1 參考地址2 Grails2參考java

相關文章
相關標籤/搜索