This is the 25th day of my participation in the August Genwen Challenge.More challenges in August
preface
The Druid Spring Boot Starter is designed to help you easily integrate Druid database connection pools and monitoring into your Spring Boot projects. This article will integrate Druid with Spring Boot for data source monitoring.
First Druid
Druid is a JDBC application component provided by Alibaba open source. It consists of three parts: DruidDriver agent Driver, which provides filter-chain plug-in system. DruidDataSource efficient and manageable database connection pool SQLParser SQL syntax analysis; Druid is the best database connection pool in the Java language. Druid provides powerful monitoring and extension capabilities.
Download the Druid
Druid is an open source project. The source code is hosted on Github at github.com/alibaba/dru… . Druid also packages the source code each time it is released as an official release and a snapshot. You can find the source code at the download address above.
DruidDataSource supports databases
Theoretically, all jDBC-driven databases are supported. It’s actually been tested
The database | Support state |
---|---|
mysql | Support, large scale use |
oracle | Support, large scale use |
sqlserver | support |
postgres | support |
db2 | support |
h2 | support |
derby | support |
sqlite | support |
sybase | support |
Druid automatically identifies DriverClass
Druid identifies DriverClass by the URL prefix, which makes configuration easier and simpler.
The prefix | DriverCLass | Description information |
---|---|---|
jdbc:odps | com.aliyun.odps.jdbc.OdpsDriver | |
jdbc:derby | org.apache.derby.jdbc.EmbeddedDriver | |
jdbc:mysql | com.mysql.jdbc.Driver | |
jdbc:oracle | oracle.jdbc.driver.OracleDriver | |
jdbc:microsoft | com.microsoft.jdbc.sqlserver.SQLServerDriver | |
jdbc:sybase:Tds | com.sybase.jdbc2.jdbc.SybDriver | |
jdbc:jtds | net.sourceforge.jtds.jdbc.Driver | |
jdbc:postgresql | org.postgresql.Driver | |
jdbc:fake | com.alibaba.druid.mock.MockDriver | |
jdbc:mock | com.alibaba.druid.mock.MockDriver | |
jdbc:hsqldb | org.hsqldb.jdbcDriver | |
jdbc:db2 | com.ibm.db2.jdbc.app.DB2Driver | DB2’s JDBC Driver is confusing, and this match may not be right |
jdbc:sqlite | org.sqlite.JDBC | |
jdbc:ingres | com.ingres.jdbc.IngresDriver | |
jdbc:h2 | org.h2.Driver | |
jdbc:mckoi | com.mckoi.JDBCDriver | |
jdbc:cloudscape | com.cloudscape.core.JDBCDriver | |
jdbc:informix-sqli | com.informix.jdbc.IfxDriver | |
jdbc:timesten | com.timesten.jdbc.TimesTenDriver | |
jdbc:as400 | com.ibm.as400.access.AS400JDBCDriver | |
jdbc:sapdb | com.sap.dbtech.jdbc.DriverSapDB | |
jdbc:JSQLConnect | com.jnetdirect.jsql.JSQLDriver | |
jdbc:JTurbo | com.newatlanta.jturbo.driver.Driver | |
jdbc:firebirdsql | org.firebirdsql.jdbc.FBDriver | |
jdbc:interbase | interbase.interclient.Driver | |
jdbc:pointbase | com.pointbase.jdbc.jdbcUniversalDriver | |
jdbc:edbc | ca.edbc.jdbc.EdbcDriver | |
jdbc:mimer:multi1 | com.mimer.jdbc.Driver |
Quick start
Join the rely on
Versions after Druid 0.1.18 are published in Maven’s central repository, so you can add dependency to your project’s POP.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 - the boot - starter < / artifactId > < version > 2.1.1 < / version > < / dependency > <! -- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency>Copy the code
The main dependencies used are druid, MySQL and MyBatis. Easy to connect to the database.
Configuration DruidMoniterConfig
Configure a Druid monitoring management background so that you can view monitoring management information on the WEB. The main configuration information includes the user name, user name, password, access permission path, and IP address of the blacklist. You need to configure a Web monitoring filter to filter static files
/** * @ClassName DruidMoniterConfig * @Description: DruidMoniterConfig * @author JavaZhan @public id :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; }}Copy the code
Basic Configuration Information
The configuration file contains the data source URL, database user name, database password, and driver class.
Server. The port = 8888 # mysql spring. The 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/**/*.xmlCopy the code
Start the class
/** * @ClassName DemoMyBatisApplication * @Description: DemoMyBatisApplication * @author JavaZhan @public id :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); }}Copy the code
Monitoring the page
After starting the project, type in your browserhttp://127.0.0.1:8888/druid/, automatically jump to http://127.0.0.1:8888/druid/login.html page, need to input the user name and password information After entering the user name and password, the monitoring page is displayed. The following information is displayed:Data source, SQL monitoring, SQL firewall, Web application, URI monitoring, Session monitoring, Spring monitoring, AND JSON API information. Spring Boot integration Druid data monitoring is complete.
conclusion
Spring Boot integrates Druid data monitoring to facilitate the analysis of database connection data and SQL monitoring.
About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.
Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.