mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add sub project druid-spring-boot-starter.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
druid-spring-boot-start
|
||||
=======================
|
||||
Spring boot with druid support。
|
||||
|
||||
### 如何使用
|
||||
在Spring Boot项目的pom.xml中添加以下依赖:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.alibaba.boot</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
```
|
||||
Druid的连接池配置采用Spring Boot标准的数据库连接池配置方式,配置按照标准设置即可。
|
||||
|
||||
```properties
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/druid
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
```
|
||||
|
||||
如果你想设置druid的具体参数,请参考 spring.datasource.druid.* 下的具体配置项。
|
||||
|
||||
|
||||
### Todo
|
||||
|
||||
* stat view servlet调整到management port上,这样可以解决安全问题
|
||||
* stat拦截中未考虑SEO的情况,如果在SEO的情况下,会导致URL数量暴增,如aliexpress的商品详情URL
|
||||
|
||||
### 参考文档
|
||||
|
||||
* Spring Boot多数据源:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html
|
||||
* Druid Wiki: https://github.com/alibaba/druid/wiki
|
||||
@@ -0,0 +1,90 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.alibaba.boot</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>5.1.26-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>tddl-spring-boot-starter</name>
|
||||
<url>http://gitlab.alibaba-inc.com/sping-boot/tddl-spring-boot-starter</url>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>1.4.5.RELEASE</spring-boot.version>
|
||||
<druid.version>1.0.29</druid.version>
|
||||
</properties>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<id>leijuan</id>
|
||||
<name>雷卷</name>
|
||||
<email>jacky.chenlb@alibaba-inc.com</email>
|
||||
<url>https://work.alibaba-inc.com/work/u/7883</url>
|
||||
<roles>
|
||||
<role>开发人员</role>
|
||||
</roles>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:git@gitlab.alibaba-inc.com:spring-boot-incubator/druid-spring-boot-starter.git</connection>
|
||||
<developerConnection>scm:git:git@gitlab.alibaba-inc.com:spring-boot-incubator/druid-spring-boot-starter.git
|
||||
</developerConnection>
|
||||
<url>http://gitlab.alibaba-inc.com/spring-boot-incubator/druid-spring-boot-starter</url>
|
||||
</scm>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<!-- Import dependency management from Spring Boot,not include plugin
|
||||
management as the parent import style -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package org.springframework.boot.autoconfigure.jdbc;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.sql.DataSource;
|
||||
import javax.sql.XADataSource;
|
||||
|
||||
/**
|
||||
* druid auto configuration
|
||||
*
|
||||
* @author leijuan
|
||||
*/
|
||||
@SuppressWarnings("SpringJavaAutowiringInspection")
|
||||
@Configuration
|
||||
@ConditionalOnClass(DruidDataSource.class)
|
||||
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(DataSourceProperties.class)
|
||||
public class DruidAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.datasource.druid")
|
||||
@ConditionalOnMissingBean({DataSource.class, XADataSource.class})
|
||||
public DataSource dataSource(DataSourceProperties properties) {
|
||||
return properties.initializeDataSourceBuilder().type(DruidDataSource.class).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(HttpServlet.class)
|
||||
public ServletRegistrationBean druidStatViewServlet() {
|
||||
return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"groups": [
|
||||
{
|
||||
"name": "spring.datasource.druid",
|
||||
"type": "com.alibaba.druid.pool.DruidDataSource",
|
||||
"sourceType": "org.springframework.boot.autoconfigure.jdbc.DruidAutoConfiguration",
|
||||
"sourceMethod": "dataSource(org.springframework.boot.autoconfigure.jdbc.DataSourceProperties)"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.boot.autoconfigure.jdbc.DruidAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
provides: druid-spring-boot-starter
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.alibaba.boot.druid.demo;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* druid auto configuration test
|
||||
*
|
||||
* @author leijuan
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ImportAutoConfiguration(DruidDemoApplication.class)
|
||||
@TestPropertySource("classpath:application.properties")
|
||||
public class DruidAutoConfigurationTest {
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Test
|
||||
public void testDatabaseOperations() {
|
||||
Assert.assertNotNull(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.alibaba.boot.druid.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* druid demo application
|
||||
*
|
||||
* @author leijuan
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class DruidDemoApplication {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
SpringApplication.run(DruidDemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/druid
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=123456
|
||||
|
||||
Reference in New Issue
Block a user