当前位置:   article > 正文

druid连接池数据源配置和控制台页面开启_druid 如何开启控制台

druid 如何开启控制台

配置类如下:

  1. package com.example.spring.druid;
  2. import java.util.HashMap;
  3. import javax.servlet.Servlet;
  4. import javax.sql.DataSource;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  9. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import com.alibaba.druid.pool.DruidDataSource;
  13. import com.alibaba.druid.support.http.StatViewServlet;
  14. import com.alibaba.druid.support.http.WebStatFilter;
  15. /**
  16. * @DESC druid配置类,会被springboot扫描然后将相应的service加到容器中
  17. * @author guchuang
  18. *
  19. */
  20. @Configuration
  21. public class DruidConfiguration {
  22. private static Logger log = LoggerFactory.getLogger(DruidConfiguration.class);
  23. @Bean
  24. @ConfigurationProperties(prefix="spring.datasource")
  25. public DataSource druid() {
  26. return new DruidDataSource();
  27. }
  28. /**
  29. * 配置druid管理页面的访问控制
  30. * 访问网址: http://127.0.0.1:8080/druid
  31. * @return
  32. */
  33. @Bean
  34. public ServletRegistrationBean<Servlet> druidServlet() {
  35. log.info("init Druid Servlet Configuration");
  36. ServletRegistrationBean<Servlet> servletRegistrationBean = new ServletRegistrationBean<>();
  37. servletRegistrationBean.setServlet(new StatViewServlet()); //配置一个拦截器
  38. servletRegistrationBean.addUrlMappings("/druid/*"); //指定拦截器只拦截druid管理页面的请求
  39. HashMap<String, String> initParam = new HashMap<String,String>();
  40. initParam.put("loginUsername", "admin"); //登录druid管理页面的用户名
  41. initParam.put("loginPassword", "admin"); //登录druid管理页面的密码
  42. initParam.put("resetEnable", "true"); //是否允许重置druid的统计信息
  43. initParam.put("allow", ""); //ip白名单,如果没有设置或为空,则表示允许所有访问
  44. servletRegistrationBean.setInitParameters(initParam);
  45. return servletRegistrationBean;
  46. }
  47. @Bean
  48. public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
  49. FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>();
  50. filterRegistrationBean.setFilter(new WebStatFilter());
  51. filterRegistrationBean.addUrlPatterns("/*");
  52. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  53. return filterRegistrationBean;
  54. }
  55. }

配置文件如下:

  1. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  2. spring.datasource.driver-class-name=org.postgresql.Driver
  3. spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/druiddb
  4. spring.datasource.username=postgres
  5. spring.datasource.password=postgres
  6. #config druid
  7. spring.datasource.initialSize=5
  8. spring.datasource.minIdle=5
  9. spring.datasource.maxActive=20
  10. spring.datasource.maxWait=10000
  11. spring.datasource.timeBetweenEvictionRunMillis=60000
  12. spring.datasource.minEvictableIdleTimeMillis=10000
  13. spring.datasource.validationQuery=SELECT 'x'
  14. spring.datasource.testWhileIdle=true
  15. spring.datasource.testOnBorrow=true
  16. spring.datasource.testOnReturn=false
  17. spring.datasource.poolPreparedStatements=true
  18. spring.datasource.maxPoolPreparedStatementPerConnectionSize=10
  19. spring.datasource.filters=stat

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/355066
推荐阅读
相关标签
  

闽ICP备14008679号