alibaba / druid

阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
https://github.com/alibaba/druid/wiki
Apache License 2.0
27.97k stars 8.58k forks source link

支持 SpringBoot 2.0.0 #1799

Closed beihaifeiwu closed 7 years ago

beihaifeiwu commented 7 years ago

SpringBoot 2.0.0版本 API变动了,这个有支持的计划吗?

lihengming commented 7 years ago

对Druid应该没影响吧?

pink-lucifer commented 7 years ago

Well, based upon my limited experience, this falls kinda in the middle of irrelevant and relevant... At lease, two aspect falls into concern:

  1. in previous version, DruidDataSourceBuilder utilizing RelaxedDataBinder which is plan to be deprecated, however, author have already changed the implementation #1813, after a quick try, this should work well with 2.0.0.M2.
  2. Reactive web module - webflux ... with webflux, we may run web api without servlet api support, which cause the embeded ui (based upon servlet) not working ... but can still adds an dependency on traditional web module to have this supported... After all, reactive web is not well supported in RDB, though mongo, redis having spring-data supported... well, reactive jdbc support component [rxjava-jdbc] is good try though, after a quick glance, we might keep an eye on it... but not sure if ready for production before a well test.

Furthermore, is health indicator endpoint support like the actuator within the reactive web style -- this might be a good plus as spring starter ...

Above are some of my thought, please help review,

thanks & regards.

lihengming commented 7 years ago

@beihaifeiwu Druid Spring Boot Starter 1.1.1兼容 Spring Boot 2.0

lihengming commented 7 years ago

@pink-lucifer thx

beihaifeiwu commented 7 years ago

@lihengming , 还有一个地方,SpringBoot监控DataSource时有一个与数据提供者接口,这个Druid也需要提供一下吧 :)

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.jdbc.metadata.AbstractDataSourcePoolMetadata;

/**
 * Created by liupin on 2017/5/4.
 */
public class DruidDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<DruidDataSource> {

    protected DruidDataSourcePoolMetadata(DruidDataSource dataSource) {
        super(dataSource);
    }

    @Override
    public Integer getActive() {
        return getDataSource().getActiveCount();
    }

    @Override
    public Integer getMax() {
        return getDataSource().getMaxActive();
    }

    @Override
    public Integer getMin() {
        return getDataSource().getMinIdle();
    }

    @Override
    public String getValidationQuery() {
        return getDataSource().getValidationQuery();
    }
}

在Configuration里:

    @Bean
    public DataSourcePoolMetadataProvider druidDataSourcePoolMetadataProvider() {
        return dataSource -> new DruidDataSourcePoolMetadata((DruidDataSource) dataSource);
    }
lihengming commented 7 years ago

@beihaifeiwu

这是结合spring-boot-starter-actuator用的吧?我试了下不加这些代码也可以显示DruidDataSource的信息(/configprops),你试试。

lihengming commented 7 years ago

The following metrics are exposed for each supported DataSource defined in your application:

  • The number of active connections (datasource.xxx.active)
  • The current usage of the connection pool (datasource.xxx.usage).

@beihaifeiwu 我看了下官方文档关于这部分的说明,就是为了调用spring-boot-starter-actuator/metrics时显示active和usage用的,感觉这和Druid 监控提供的信息有点重叠,不知道这个东西重要不,另外请问上面配置的max、min、validationQuery在哪里显示?看了下源码,只看到了active和usage。

DataSourcePublicMetrics.java
...
@Override
public Collection<Metric<?>> metrics() {
    Set<Metric<?>> metrics = new LinkedHashSet<>();
    for (Map.Entry<String, DataSourcePoolMetadata> entry : this.metadataByPrefix
            .entrySet()) {
        String prefix = entry.getKey();
        prefix = (prefix.endsWith(".") ? prefix : prefix + ".");
        DataSourcePoolMetadata metadata = entry.getValue();
        addMetric(metrics, prefix + "active", metadata.getActive());
        addMetric(metrics, prefix + "usage", metadata.getUsage());
    }
    return metrics;
}
beihaifeiwu commented 7 years ago

@lihengming 多谢回复,确实只是使用了两个属性,这部分我并没有深入,这个issue我关闭了:)

lihengming commented 7 years ago

@beihaifeiwu :)我再看看吧,如果人畜无害的话就加上,万一有人用到这玩意呢,毕竟DataSourcePoolMetadataProvidersConfiguration为其他数据源都配置了这个,感谢提醒。

mattmok commented 7 years ago

spring boot 2.0.0.M3 集成druid-spring-boot-starter 1.1.3时报以下异常: Description:

Binding to target [Bindable@4507e03f type = ?, value = 'none', annotations = array[[empty]]] failed:

Reason: Failed to bind properties under 'spring.datasource.druid.connection-init-sqls[0]' to ?

Action:

配置为: @Bean @ConfigurationProperties(prefix = "spring.datasource") @ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class) public DruidDataSource dataSource() { DruidDataSource datasource = DruidDataSourceBuilder.create().build(); return datasource; } spring: datasource: druid: url: jdbc:mysql://127.0.0.1:3306/authority?useUnicode=true&characterEncoding=utf8 username: root password: root driver-class-name: com.mysql.jdbc.Driver

请问如何解决?

chixinzei commented 7 years ago

@mattmok

@Primary //数据源名称 @Bean(name="portalDataSource") //松耦合属性分层注解,注意,跟druid整合时,需要忽略无效注入属性ignoreInvalidFields = true,避免注入报错 @ConfigurationProperties(value = "spring.datasource.druid.local",ignoreInvalidFields = true) public DataSource dataSourcePortal(){ return DruidDataSourceBuilder.create().build(); }

chuntaojun commented 6 years ago

我想问下,如果我用的是spring-boot-start-webflux,可以集成druid吗,