[置顶] 【Druid】Druid连接池(二)——配置监控界面
2017-12-02 11:55
633 查看
一、前言
在阿里的数据库连接池中,Druid是一个很好的选择。她可以实现程序员的一些特殊的需求,比如像密钥服务请求凭证,统计sql信息,sql性能收集,sql注入检测,sql翻译等。Druid在阿里巴巴内部广泛部署,由温少带队研究。
二、环境要求
druid jar包小编使用的maven项目,导入相关依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency>
三、配置文件相关信息
3.1 Spring-dao.xml
我们使用spring管理Druid,在数据库配置文件中,配置下面的信息:注意:要使用监控功能,
<property name="filters" value="stat" />filters属性要设置为stat。
Druid的监控统计功能是通过filter-chain扩展实现,如果你要打开监控统计功能,配置StatFilter ,StatFilter 的别名是stat,这个别名映射配置信息保存在druid-xxx.jar!/META-INF/druid-filter.properties。 在spring中使用别名配置方式如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 数据库连接池 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:conf/db.properties" /> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 基本属性 url username password driverClassName--> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!--<property name="driverClassName" value="${jdbc.driver}" />--> <!--配置初始化大小、最小、最多连接数--> <property name="initialSize" value="1"/> <property name="maxActive" value="100" /> <property name="minIdle" value="5" /> <!--配置获取连接等待超时时间--> <property name="maxWait" value="3000"/> <!--配置间隔多久进行一次检测,检测需要关闭的空闲连接,单位是毫秒--> <property name="timeBetweenEvictionRunsMillis" value="6000"/> <!--配置一个连接在连接池中,最小生存的时间,单位是毫秒--> <property name="minEvictableIdleTimeMillis" value="30000"/> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!--打开PSCache,并且指定每个连接上的PSCache的大小--> <property name="poolPreparedStatements" value="true"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 --> <property name="filters" value="stat" /> </bean> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dmsd.dao" /> </bean> </beans>
3.2 web.xml
StatViewServlet 是一个标准的javax.servlet.http.HttpServlet ,需要配置在你web应用中的WEB-INF/web.xml中。StatViewSerlvet 展示出来的监控信息比较敏感,是系统运行的内部情况,如果你需要做访问控制,可以配置allow和deny这两个参数。
配置druid监控:
<!-- 配置druid监控--> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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> </servlet-mapping>
3.3 显示界面
运行项目后,输入http://192.168.22.195:8082/teachingManagement-service/druid/sql.html,连接,就可以看到管理了界面:四、小结
配置过程还是很简单,有了管理监控界面,就可以更好的对sql性能进行监控,使用也非常方便。相关文章推荐
- [置顶] 【Druid】Druid连接池(三)——配置监控界面设置登录界面
- Druid连接池基本配置及监控配置
- Druid连接池及监控在spring中的配置
- spring boot druid连接池监控配置
- Druid连接池及监控在spring中的配置
- druid数据源及监控界面配置
- druid连接池监控在spring中的配置
- spring配置druid连接池和监控数据库访问性能
- druid 配置监控界面和开启spring支持
- 阿里巴巴 Druid 数据库连接池监控界面配置的参数解读
- druid连接池监控在spring中的配置(注)
- spring-boot 连接池 druid 的配置及监控
- Druid连接池及监控在spring中的配置
- 框架篇-Spring+Mybatis整合Druid连接池,并配置SQL监控
- [置顶] dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(二)之 JDBC连接池、监控组件 Druid
- SpringBoot实践之---Druid连接池的使用和监控配置
- mybatis使用spring-druid数据源连接池配置log4j打印sql语句以及开启监控平台
- Druid使用起步—在javaWeb项目中配置监控 连接池
- 【项目实践】Druid连接池及在Spring中的监控配置
- spring-boot 连接池 druid 的配置及监控