springboot+druid sql监控页面无法显示sql语句

1、无法显示sql监控数据:

按照这篇文章配置好springboot+mybatis+druid,然后打开http://127.0.0.1:8080/druid 页面,可以看到数据源、web监控等信息,但是无法看到sql监控。一直怀疑是配置问题,后来发现是版本。

pom.xml中开始引入的是

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

改成

<dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>druid-spring-boot-starter</artifactId>
	    <version>1.1.9</version>
	</dependency>

重启后,还是没有sql监控数据。继续检查,使用druid进行统计的时候,需要配置一个filter和一个servlet,如下:

DruidWebStatFilter:

<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>/static/*,*.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>  

DruidStatServlet:

<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> 

接着,需要在spring中初始化Druid数据源

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
        init-method="init" destroy-method="close">  
        <property name="driverClassName" value="${jdbc.driverClassName}" />  
        <property name="url" value="${jdbc.url}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
        <property name="maxActive" value="${db.maxActive}" />  
        <property name="initialSize" value="${db.initialSize}" />  
        <property name="maxWait" value="${db.maxWait}" />  
        <property name="minIdle" value="${db.minIdle}" />  
        <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}" />  
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}" />  
        <property name="testWhileIdle" value="${db.testWhileIdle}" />  
        <property name="testOnBorrow" value="${db.testOnBorrow}" />  
        <property name="testOnReturn" value="${db.testOnReturn}" />  
        <property name="poolPreparedStatements" value="${db.poolPreparedStatements}" />  
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${db.maxPoolPreparedStatementPerConnectionSize}" />  
        <property name="filters" value="stat" />  
    </bean>  

注意: <property name="filters" value="stat" />属性,不然无法显示SQL检测信息。

看到这里,我们需要在springboot的数据源配置中加上如下代码:

 @Bean(name = "ds3DataSource")
    public DataSource ds3DataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        
        //连接池配置
        dataSource.setMaxActive(maxActive);
        dataSource.setMinIdle(minIdle);
        dataSource.setInitialSize(initialSize);
        dataSource.setMaxWait(maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        dataSource.setValidationQuery("SELECT 'x'");
        
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(30);
        
        try {
			dataSource.setFilters("stat");
		} catch (SQLException e) {
			e.printStackTrace();
		}
        
        return dataSource;
    }

如最后一行。之后重启系统,即可在界面上看到sql监控数据了。

2、启动报错:

按照上面配置,在本地可以跑起来(本地是jdk1.8),打包放到服务器上启动就会报错(jdk1.7)。

[07-17 11:42:23] [ERROR] [org.springframework.beans.factory.support.DefaultListableBeanFactory:581] Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bf16be0: startup date [Tue Jul 17 11:42:22 CST 2018]; root of context hierarchy
	at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414)
	at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97)
	at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968)
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
	at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151)
	at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131)
	at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)
	at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5178)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)

解决方法:升级jdk到1.8,或者引入druid的1.1.10版本。

猜你喜欢

转载自blog.csdn.net/liuxiao723846/article/details/81078169