SpringBoot整合Druid及配置数据源监控

一、SpringBoot整合Druid

1、pom.xml文件引入如下配置:截图及代码如下:
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rf</groupId>
    <artifactId>springboootdata</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboootdata</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、log4j.properties配置,lz为了测试,简单配置了控制台输出日志,如下图:
在这里插入图片描述

#应用于控制台
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

3、编写druid配置类,绑定application.yml文件中数据源其他配置的属性,截图及代码如下:
在这里插入图片描述

package com.rf.springbooot.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return  new DruidDataSource();
    }

4、测试连接数据库是否可以查到表数据,截图及代码如下:
在这里插入图片描述

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class HelloController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("query")
    @ResponseBody
    public  Map<String, Object> hello(){
         List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from sys_user");
         return  maps.get(0);
    }
}

5、浏览器访问 http://localhost:8080/query ,如又数据返回证明SpringBoot整合Druid成功。
在这里插入图片描述

二、配置数据源监控

1、在springboot整合Druid的基础上,在druid配置类中添加后台管理的Servlet方法web监控的filter方法,截图及代码如下:
在这里插入图片描述

package com.rf.springbooot.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return  new DruidDataSource();
    }



    /**
     * 配置druid监控
     * 1、配置一个后台管理的Servlet
     * */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,Object> map =new HashMap<>();
        map.put("loginUsername","111");
        map.put("loginPassword","111");
        map.put("allow","");//默认允许所有访问
        map.put("deny","192.168.0.1");//拒绝本地ip访问
        servletRegistrationBean.setInitParameters(map);
        return  servletRegistrationBean;
    }
    /**
     * 2、配置一个web监控的filter
     * */
    @Bean
    public FilterRegistrationBean webStatServlet(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String,Object> map =new HashMap<>();
        map.put("exclusions","*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(map);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }
}

2、启动springboot应用,在浏览器中输入 http://localhost:8080/druid ;输入用户名:111;密码:111;访问druid的后台管理,截图如下:
在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页