美文网首页
整合Druid数据源

整合Druid数据源

作者: AnnieAri | 来源:发表于2018-07-11 11:03 被阅读0次

    在pom.xml添加依赖

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>LATEST</version>
    </dependency>
    

    application.properties或者application.yml数据源类型设置为Druid数据源

    spring:
      datasource:
    #   数据源基本配置
        username: root
        password: ****
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://****:3306/user
        type: com.alibaba.druid.pool.DruidDataSource #设置为Druid
    #   数据源其他配置
        initialSize: 5
        minIdle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
        filters: stat,wall #,log4j
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true  
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    

    导入Durid数据源

    @Configuration
    public class DruidConfig {
        @ConfigurationProperties(prefix = "spring.datasource")
        @Bean
        public DataSource druid() {
            return new DruidDataSource();
        }
        ///
        @Bean
        public ServletRegistrationBean statViewServlet() {
            ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
            Map<String,String> initParams = new HashMap<>();
            initParams.put("loginUsername","admin");
            initParams.put("loginPassword","123456");
            initParams.put("allow","");//允许登录账号  默认允许所有访问
            initParams.put("deny","192.168.1.1");//拒绝访问  
            bean.setInitParameters(initParams);
            return bean;
        }
        ///拦截 
        @Bean
        public FilterRegistrationBean webStatFilter(){
            FilterRegistrationBean bean = new FilterRegistrationBean();
            bean.setFilter(new WebStatFilter());
    
            Map<String,String> initParams = new HashMap<>();
            initParams.put("exclusions","*.js,*.css,/druid/*");///不拦截的请求  静态文件 和 druid的请求
            bean.setInitParameters(initParams);
            bean.setUrlPatterns(Arrays.asList("/*"));
            return bean;
        }
    }
    

    配置完成后重启程序
    在浏览器中访问http://localhost:8080/druid/

    image.png
    登录成功后的界面
    image.png
    @RestController
    public class HelloController {
        @Autowired
        JdbcTemplate jdbcTemplate;
        @RequestMapping("/hello")
        public Map<String,Object> map(){
            List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT * FROM employee");
            return list.get(0);
        }
    }
    
    

    进行一次接口访问: http://localhost:8080/hello

    sql监控 uri监控

    记录就都有了

    相关文章

      网友评论

          本文标题:整合Druid数据源

          本文链接:https://www.haomeiwen.com/subject/eczcpftx.html