Alibaba Druid连接池接入

maven引入


    com.alibaba
    druid
    1.1.17


    com.alibaba
    druid-spring-boot-starter
    1.1.17

 

配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://10.0.0.189:5432/test
    username: postgres
    password: test123
    druid:
      #初始化连接大小
      initial-size: 8
      #最小空闲连接数
      min-idle: 5
      #最大连接数
      max-active: 10
      #查询超时时间
      query-timeout: 30000
      #事务查询超时时间
      transaction-query-timeout: 30000
      #关闭空闲连接超时时间
      remove-abandoned-timeout: 1800
      filters: stat,config

 

编写测试

@Autowired
JdbcTemplate jdbcTemplate;
@GetMapping("test")
public String test() {



    List> list= jdbcTemplate.query("select * from os.auth_user limi t 10", new ResultSetExtractor>>() {

        @Override
        public List> extractData(ResultSet rs) throws SQLException, DataAccessException {
            while (rs.next())
            {
                System.out.print("Column 1 returned ");
                System.out.println(rs.getString(1));
            }
            return null;
        }
    });
    return "1";
}

 

注意:此处sql故意写错目的是为了报错,显示如下信息,标红处可以看出走了druid

2019-06-18 14:20:05 14151 [http-nio-18081-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from os.auth_user limi t 10]; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "t"

位置:33] with root cause

org.postgresql.util.PSQLException: ERROR: syntax error at or near "t"

位置:33

at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)

at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)

……

at com.alibaba.druid.filter.FilterChainImpl.statement_executeQuery(FilterChainImpl.java:2831)

at com.alibaba.druid.filter.FilterAdapter.statement_executeQuery(FilterAdapter.java:2504)

at com.alibaba.druid.filter.FilterChainImpl.statement_executeQuery(FilterChainImpl.java:2828)

at com.alibaba.druid.filter.FilterAdapter.statement_executeQuery(FilterAdapter.java:2504)

at com.alibaba.druid.filter.FilterEventAdapter.statement_executeQuery(FilterEventAdapter.java:302)

at com.alibaba.druid.filter.FilterChainImpl.statement_executeQuery(FilterChainImpl.java:2828)

at com.alibaba.druid.proxy.jdbc.StatementProxyImpl.executeQuery(StatementProxyImpl.java:221)

at com.alibaba.druid.pool.DruidPooledStatement.executeQuery(DruidPooledStatement.java:296)

……

 

 

监控配置

注:监控只支持Servlet,不支持react

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class DruidConfiguration {
    /**
     * 注册Servlet信息, 配置监控视图
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        //白名单:
        servletRegistrationBean.addInitParameter("allow","192.168.6.195");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny","192.168.6.73");
        //登录查看信息的账号密码, 用于登录Druid监控后台
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "true");
        return servletRegistrationBean;

    }

    /**
     * 注册Filter信息, 监控拦截器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

你可能感兴趣的:(java,alibaba,druid)