[Spring Boot quick start] Spring Boot integrates Druid data monitoring

preface

  Druid Spring Boot Starter is used to help you easily integrate Druid database connection pool and monitoring in Spring Boot projects. This article will integrate Druid with Spring Boot for data source monitoring.

First met Druid

   Druid is a JDBC application component open source by Alibaba. It includes three parts: DruidDriver agent Driver, which can provide a plug-in system based on Filter Chain mode; DruidDataSource efficient and manageable database connection pool; SQL parser SQL syntax analysis; Druid is the best database connection pool in the Java language. Druid can provide powerful monitoring and extension functions.

Download Druid

Druid is an open source project. The source code is hosted on github. The address of the source code warehouse is github.com/alibaba/dru... . At the same time, every time Druid releases the official version and snapshot, it will package the source code. You can find the source code of the relevant version from the download address above.

DruidDataSource supports databases

    theoretically, all databases driven by jdbc are supported. Actually tested

databaseSupport status
mysqlSupport, large-scale use
oracleSupport, large-scale use
sqlserversupport
postgressupport
db2support
h2support
derbysupport
sqlitesupport
sybasesupport

Druid automatically identifies DriverClass

   Druid identifies DriverClass according to the url prefix, which makes the configuration more convenient and concise.

prefixDriverCLassDescription information
jdbc:odpscom.aliyun.odps.jdbc.OdpsDriver
jdbc:derbyorg.apache.derby.jdbc.EmbeddedDriver
jdbc:mysqlcom.mysql.jdbc.Driver
jdbc:oracleoracle.jdbc.driver.OracleDriver
jdbc:microsoftcom.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc:sybase:Tdscom.sybase.jdbc2.jdbc.SybDriver
jdbc:jtdsnet.sourceforge.jtds.jdbc.Driver
jdbc:postgresqlorg.postgresql.Driver
jdbc:fakecom.alibaba.druid.mock.MockDriver
jdbc:mockcom.alibaba.druid.mock.MockDriver
jdbc:hsqldborg.hsqldb.jdbcDriver
jdbc:db2com.ibm.db2.jdbc.app.DB2DriverDB2's JDBC Driver is very confusing, and this match is not necessarily correct
jdbc:sqliteorg.sqlite.JDBC
jdbc:ingrescom.ingres.jdbc.IngresDriver
jdbc:h2org.h2.Driver
jdbc:mckoicom.mckoi.JDBCDriver
jdbc:cloudscapecom.cloudscape.core.JDBCDriver
jdbc:informix-sqlicom.informix.jdbc.IfxDriver
jdbc:timestencom.timesten.jdbc.TimesTenDriver
jdbc:as400com.ibm.as400.access.AS400JDBCDriver
jdbc:sapdbcom.sap.dbtech.jdbc.DriverSapDB
jdbc:JSQLConnectcom.jnetdirect.jsql.JSQLDriver
jdbc:JTurbocom.newatlanta.jturbo.driver.Driver
jdbc:firebirdsqlorg.firebirdsql.jdbc.FBDriver
jdbc:interbaseinterbase.interclient.Driver
jdbc:pointbasecom.pointbase.jdbc.jdbcUniversalDriver
jdbc:edbcca.edbc.jdbc.EdbcDriver
jdbc:mimer:multi1com.mimer.jdbc.Driver

Quick start

Join dependency

  Druid 0.1. The versions after 18 are released to the maven central warehouse, so you only need to be in the project POM Just add dependency to XML.

      <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.11</version>
        </dependency>

  the main dependency used is druid, and others are MySQL and MyBatis. Easy to connect to the database.

Configure DruidMoniterConfig

   configure a Druid monitoring and management background, mainly to view the monitoring and management information on the web side. The main configuration information includes user name, user name and password, access path allowed, IP of blacklist, etc. Then you need to configure a web monitoring filter to filter static files

/**
 * @ClassName DruidMoniterConfig
 * @Description: DruidMoniterConfig
 * @Author JavaZhan @Official account: Java full stack architect
 * @Date 2020/6/13
 * @Version V1.0
 **/
@Configuration
public class DruidMoniterConfig{
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,String> initParams = new HashMap<>();
        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","admin");
        initParams.put("allow","");
        initParams.put("deny","192.168.127.98");

        bean.setInitParameters(initParams);
        return bean;
    }

  
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return  bean;
    }
}

Basic configuration information

   the configuration file mainly contains data source related configuration information such as data source URL, database user name, database password and driver class.

server.port=8888

# mysql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=test
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

mybatis.mapper-locations=classpath*:mapper/**/*.xml

Startup class

/**
 * @ClassName DemoMyBatisApplication
 * @Description: DemoMyBatisApplication
 * @Author JavaZhan @Official account: Java full stack architect
 * @Date 2020/6/13
 * @Version V1.0
 **/
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoMyBatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoMyBatisApplication.class, args);
    }

}

Monitoring page

  after starting the project, enter in the browser http://127.0.0.1:8888/druid/ , auto jump to http://127.0.0.1:8888/druid/login.html Page, you need to enter user name and password information

After entering the user name and password, enter the monitoring page. The page information is as follows:

It mainly includes: data source, SQL monitoring, SQL firewall, Web application, URI monitoring, session monitoring, Spring monitoring, JSON API and other information. Well, the Spring Boot integration Druid data monitoring has been completed.

Keywords: Database PostgreSQL Spring Spring Boot

Added by Tryfan on Sun, 19 Dec 2021 14:56:07 +0200