1、druid官方github地址
https://github.com/alibaba/druid
整合第三方技术的两种方式
- 自定义
- 找starter
2、自定义方式 把注册类的方式写在一起,可以对比看看
1、xml的方式创建数据源
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.17</version> </dependency> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="20" /> <property name="initialSize" value="1" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="1" /> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxOpenPreparedStatements" value="20" />
1、注册类的的方式创建数据源
@Deprecated //@Configuration public class MyDataSourceConfig { // 默认的自动配置是判断容器中没有才会配@ConditionalOnMissingBean(DataSource.class) // @ConfigurationProperties("spring.datasource")//在配置文件中写配置 // @Bean public DataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); // druidDataSource.setUrl(); // druidDataSource.setUsername(); // druidDataSource.setPassword(); //加入监控功能 // druidDataSource.setFilters("stat,wall"); // druidDataSource.setMaxActive(10); return druidDataSource; }
2、StatViewServlet 以XML的方式放入监控页
StatViewServlet的用途包括:
- 提供监控信息展示的html页面
- 提供监控信息的JSON API
<servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> //放入druid下的所有文件
</servlet-mapping>
以配置类的方式放入监控页
/** * 配置 druid的监控页功能 * @return */ // @Bean public ServletRegistrationBean statViewServlet(){ StatViewServlet statViewServlet = new StatViewServlet(); ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");//放入druid下的所有文件
registrationBean.addInitParameter("loginUsername","admin");//设置账号密码 registrationBean.addInitParameter("loginPassword","123456"); return registrationBean; }
3、StatFilter 开启监控,开启监控拦截请求,对请求做统计,监控的数据在监控页上
用于统计监控信息;如SQL监控、URI监控
需要给数据源中配置如下属性;可以允许多个filter,多个用,分割;如: <property name="filters" value="stat,slf4j" />
以配置类开启监控
public DataSource dataSource() throws SQLException { DruidDataSource druidDataSource = new DruidDataSource(); // druidDataSource.setUrl(); // druidDataSource.setUsername(); // druidDataSource.setPassword(); //加入监控功能 // druidDataSource.setFilters("stat,wall"); // druidDataSource.setMaxActive(10); return druidDataSource; }
以配置类开启web相关的监控 这些都是原生的servlet 所以需要使用这些注解
// @Bean public FilterRegistrationBean webStatFilter(){ WebStatFilter webStatFilter = new WebStatFilter(); FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter); filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; }
/*监控所有 exclusions排除”*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*”这些不监控
系统中所有filter:
别名 |
Filter类名 |
default |
com.alibaba.druid.filter.stat.StatFilter |
stat |
com.alibaba.druid.filter.stat.StatFilter |
mergeStat |
com.alibaba.druid.filter.stat.MergeStatFilter |
encoding |
com.alibaba.druid.filter.encoding.EncodingConvertFilter |
log4j |
com.alibaba.druid.filter.logging.Log4jFilter |
log4j2 |
com.alibaba.druid.filter.logging.Log4j2Filter |
slf4j |
com.alibaba.druid.filter.logging.Slf4jLogFilter |
commonlogging |
com.alibaba.druid.filter.logging.CommonsLogFilter |
慢SQL记录配置
<bean id="stat filter" class="com.alibaba.druid.filter.stat.StatFilter"> <property name="slowSqlMillis" value="10000" /> <property name="logSlowSql" value="true" /> </bean> 使用 slowSqlMillis 定义慢SQL的时长
以配置类的方式
// @Bean public StatFilter statFilter(){ StatFilter statFilter = new StatFilter; statFilter.setSlowSqlMillis(10000); statFilter.setLogSlowSql(true); return statFilter; }
这些set的方法可以直接在配置文件中写
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289749.html