Skip to content

DataSource埋点接入配置问题 #159

@xingguang2013

Description

@xingguang2013

DataSource埋点接入,测试发现只有xml方式配置数据源才能生效

在尝试使用DataSource埋点时,发现插件只能处理使用xml配置数据源的方式,如果使用Spring Boot原生的方式,也就是在application.yml文件直接配置某种数据源或时Java Config方式均不能使DataSouce埋点生效。

配置如下

  1. Spring Boot原生方式(埋点不生效):application.yml
spring:
  application:
    name: sofa-sample
  datasource:
      url: jdbc:mysql://localhost:3306/sofa
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: xxxx
      password: xxxx
      type: com.zaxxer.hikari.HikariDataSource
      hikari:
        minimum-idle: 3
        maximum-pool-size: 5
        idle-timeout: 30000
        pool-name: SofaHikariCPDatasource
        max-lifetime: 1800000
        connection-timeout: 30000
        connection-test-query: SELECT 1
  1. Java Config方式(埋点不生效)
@Configuration
public class DatasourceConfig {

    @Value("${spring.datasource.url}")
    private String jdbcUrl;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public DataSource hikariDataSource(){
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

3、Xml方式配置数据源(埋点生效)

application.yml

spring:
  application:
    name: sofa-sample
  datasource:
      url: jdbc:mysql://localhost:3306/sofa
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: xxxx
      password: xxxx

datasouce.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- dataSource pool -->
    <bean id="simpleDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close" primary="true">
        <property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
        <property name="jdbcUrl" value="${spring.datasource.url}"/>
        <property name="username" value="${spring.datasource.username}"/>
        <property name="password" value="${spring.datasource.password}"/>
    </bean>
</beans>

xml 配置

@Configuration
@ImportResource(locations = {"classpath:/datasource.xml"})
public class DatasourceConfig {
    
}

建议

从插件代码看,是com.alipay.sofa.tracer.boot.datasource.processor.DataSourceBeanFactoryPostProcessor在处理数据源的代理,根据BeanDefinition#getBeanClassName的方法来判断数据源类型,可惜Java Config方式或Spring Boot原生方式没有获取到相关信息,导致无法初始化数据源代理。

Environment

  • SOFATracer version: 3.0.2
  • JVM version : 1.8.0_171
  • OS version : Windows 10
  • Maven version: 3.5.4
  • IDE version: IDEA 2018

Activity

khotyn

khotyn commented on Jan 27, 2019

@khotyn
Member

感谢你的建议,我们跟进一下。

added this to the 2.3.2 milestone on Jan 28, 2019
added a commit that references this issue on Jan 28, 2019
f2313e6
QilongZhang

QilongZhang commented on Jan 28, 2019

@QilongZhang
Contributor

@xingguang2013 感谢反馈,已经在 v2.3.2 提 PR #162 支持 @bean 方式 DataSource 埋点,预计本周发布 v2.3.2. 如果有兴趣,可以拉取分支至本地,快速验证该功能。

xingguang2013

xingguang2013 commented on Jan 28, 2019

@xingguang2013
Author

好的,感谢

xingguang2013

xingguang2013 commented on Jan 28, 2019

@xingguang2013
Author

@QilongZhang 我用的是springboot版本 com.alipay.sofa:tracer-sofa-boot-starter:3.0.2,是不是还需要一些时间?

QilongZhang

QilongZhang commented on Jan 28, 2019

@QilongZhang
Contributor

@QilongZhang 我用的是springboot版本 com.alipay.sofa:tracer-sofa-boot-starter:3.0.2,是不是还需要一些时间?

我们会将代码移至 3.0.3,如果没有其他的featrue进入,会尽快发布。

xingguang2013

xingguang2013 commented on Jan 28, 2019

@xingguang2013
Author

@QiLC 感谢

QilongZhang

QilongZhang commented on Feb 1, 2019

@QilongZhang
Contributor

@xingguang2013 SOFATracer 2.3.2 已经发布增强 DataSource 埋点,3.x 版本预计春节后发布。

xingguang2013

xingguang2013 commented on Feb 1, 2019

@xingguang2013
Author

好的👌

xingguang2013

xingguang2013 commented on Mar 2, 2019

@xingguang2013
Author

com.alipay.sofa:tracer-sofa-boot-starter:3.0.3 版本准备什么时候发布?

QilongZhang

QilongZhang commented on Mar 2, 2019

@QilongZhang
Contributor

com.alipay.sofa:tracer-sofa-boot-starter:3.0.3 版本准备什么时候发布?

下周发布,3.0.3 应该只有你这一个需求。

QilongZhang

QilongZhang commented on Mar 2, 2019

@QilongZhang
Contributor

@xingguang2013 PR 已经提交,如果方便,麻烦可以使用 代码分支 在本地验证下~

xingguang2013

xingguang2013 commented on Mar 4, 2019

@xingguang2013
Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @khotyn@xingguang2013@QilongZhang

      Issue actions

        DataSource埋点接入配置问题 · Issue #159 · sofastack/sofa-tracer