Druid连接池的数据源监控

一,连接池的配置

在pom.xml中添加,druid的maven信息

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

数据源设置:

要在init-method设置init方法,才能在监控页面中查看数据源等操作

<!--使用druid数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init">
        <!--注入连接属性-->
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--初始化连接池大小-->
        <property name="initialSize" value="5"></property>
        <!--设置最大连接数-->
        <property name="maxActive" value="10"></property>
        <!--设置等待时间-->
        <property name="maxWait" value="5000"/>
        <!--配置数据源监控的filter-->
        <property name="filters" value="stat"></property>
    </bean>

二,数据源监控的配置:

druid数据源监控是通过其support包下的StatViewServlet来提供的,所以要在web.xml进行servlet的配置
Druid连接池的数据源监控_第1张图片
StatViewServlet的部分代码如下:

public class StatViewServlet extends ResourceServlet {
    private static final Log LOG = LogFactory.getLog(StatViewServlet.class);
    private static final long serialVersionUID = 1L;
    public static final String PARAM_NAME_RESET_ENABLE = "resetEnable";
    public static final String PARAM_NAME_JMX_URL = "jmxUrl";
    public static final String PARAM_NAME_JMX_USERNAME = "jmxUsername";
    public static final String PARAM_NAME_JMX_PASSWORD = "jmxPassword";
    private DruidStatService statService = DruidStatService.getInstance();
    private String jmxUrl = null;
    private String jmxUsername = null;
    private String jmxPassword = null;
    private MBeanServerConnection conn = null;

    public StatViewServlet() {
        super("support/http/resources");
    }

其资源路径在
“support/http/resources” 主要是一些静态的前端资源文件,所以要在filter中过滤

web.xml配置

<servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
    
    <init-param>
      <param-name>loginUsernameparam-name>
      <param-value>adminparam-value>
    init-param>
    
    <init-param>
      <param-name>loginPasswordparam-name>
      <param-value>rootparam-value>
    init-param>
    
    <init-param>
      <param-name>allowparam-name>
      <param-value>param-value>
    init-param>
    
    <init-param>
      <param-name>denyparam-name>
      <param-value>param-value>
    init-param>
  servlet>
  <servlet-mapping>
    <servlet-name>StatViewServletservlet-name>
    <url-pattern>/druid/*url-pattern>
  servlet-mapping>


  <filter>
    <filter-name>WebStatFilterfilter-name>
    <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
    
    <init-param>
      <param-name>exclusionsparam-name>
      <param-value>*.js,*.gif,*.css,*.ico,*.jpg,*.png,/druid/*param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>WebStatFilterfilter-name>
    <servlet-name>StatViewServletservlet-name>
  filter-mapping>
  

配置完成访问项目路径下的 “/druid/就能进入监控页面,登入用户名和密码就是在servlet中配置的

Druid连接池的数据源监控_第2张图片

你可能感兴趣的:(Druid连接池的数据源监控)