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
database | Support status |
---|---|
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 according to the url prefix, which makes the configuration more convenient and concise.
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 very confusing, and this match is not necessarily correct |
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 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.