无法在带有 SqlServer 的 PCF 中使用 Spring 云连接器设置 connectionProperties

Cannot set connectionProperties with Spring Cloud Connectors in PCF with SqlServer

我正在我的项目中设置我的数据源,该项目已部署到 PCF,并且存在关于 sqlserver 的性能问题,所以我想添加 属性:sendStringParametersAsUnicode=false解决这个问题,就像本地jdbcUrl设置:url: jdbc:sqlserver://localhost:1433;databaseName=localdatasource;sendStringParametersAsUnicode=false,

但是我尝试了下面的两个代码,似乎connectionConfig不起作用。

代码1参考Spring Cloud Spring Service Connector guide

@Configuration
@Profile({"dev", "sit", "uat", "prod"})
@Slf4j
public class CloudConfig extends  AbstractCloudConfig {
    @Value("${datasourceinfo.min-idle}")
    private int dataSourceMinPoolSize = 20;
    @Value("${datasourceinfo.max-active}")
    private int dataSourceMaxPoolSize = 100;
    @Value("${datasourceinfo.max-wait}")
    private int dataSourceMaxWaitTime = -1;

    @Value("${datasourceinfo.azureDatabase}")
    private String azureDatabase;

    @Bean
    @Primary
    public DataSource dataSource() {
        PooledServiceConnectorConfig.PoolConfig poolConfig = new PooledServiceConnectorConfig.PoolConfig(dataSourceMinPoolSize, dataSourceMaxPoolSize, dataSourceMaxWaitTime);
        DataSourceConfig.ConnectionConfig connConfig = new DataSourceConfig.ConnectionConfig("sendStringParametersAsUnicode=false");
        DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, connConfig);
        log.info("======================init dataSource connProperties {}",dbConfig);
        return connectionFactory().dataSource(azureDatabase, dbConfig);
    }
}

Code2:遵循这里的建议:Spring Cloud Connectors issue

    @Primary
    public DataSource dataSource() {
        PooledServiceConnectorConfig.PoolConfig poolConfig = new PooledServiceConnectorConfig.PoolConfig(dataSourceMinPoolSize, dataSourceMaxPoolSize, dataSourceMaxWaitTime);
        Map<String, Object> connProperties = new HashMap<>();
        connProperties.put("connectionProperties","sendStringParametersAsUnicode=false");
        DataSourceConfig dbConfig = new DataSourceConfig(poolConfig,null,null,connProperties);
        log.info("======================init dataSource connProperties {}",dbConfig);
        return connectionFactory().dataSource(azureDatabase, dbConfig);
    }

日志如下:

2020-05-26T20:13:48.724+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.724+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] HikariPool-1 - configuration:
2020-05-26T20:13:48.727+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.727+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] connectionTestQuery............."SELECT 1"
2020-05-26T20:13:48.727+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.727+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] connectionTimeout...............30000
2020-05-26T20:13:48.728+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.728+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] driverClassName................."com.microsoft.sqlserver.jdbc.SQLServerDriver"
2020-05-26T20:13:48.729+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.729+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] jdbc4ConnectionTest.............false
2020-05-26T20:13:48.729+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.729+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] jdbcUrl.........................**jdbc:sqlserver://myIp:1433;database=devDb;user=u4a1780c36;password=<masked>;Encrypt=true;TrustServerCertificate=false;HostNameInCertificate=*.database.windows.net;loginTimeout=30;**
2020-05-26T20:13:48.729+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.729+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] leakDetectionThreshold..........0
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.729+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] maxLifetime.....................1800000
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.730+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] maximumPoolSize.................100
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.730+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] metricRegistry..................none
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.730+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] metricsTrackerFactory...........com.zaxxer.hikari.metrics.micrometer.MicrometerMetricsTrackerFactory@75add13c
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.730+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] minimumIdle.....................20
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.730+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] password........................<masked>
2020-05-26T20:13:48.730+08:00 [APP/PROC/WEB/0] [OUT] DEBUG . [26-05-2020 20:13:48.730+0800] [main] [springAppName_IS_UNDEFINED,,,] [c.z.h.HikariConfig] poolName........................"HikariPool-1"

从日志中,我发现PoolConfig [minimumIdle=20,maximumPoolSize=100]更新了,但是ConnectionConfig被忽略了,我不知道发生了什么还是我的代码有问题。 谢谢。

您正在使用 HikariCP 连接池库,我认为您尝试使用 HikariCP 和 Spring 云连接器无法实现。

    DataSourceConfig.ConnectionConfig connConfig = new DataSourceConfig.ConnectionConfig("sendStringParametersAsUnicode=false");
    DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, connConfig);

使用此配置时,连接器将尝试在检测到的 DataSource 实现上调用名为 setConnectionProperties 的 setter 方法。 HikariDataSource 没有像其他受支持的连接池库那样的 setConnectionProperties 方法,因此此配置无效。基于 Map 的配置选项也是如此,因为 HikariDataSource 不公开允许设置此类属性的任何其他方法。

为了让它工作,您需要切换到另一个 supported connection pooling libraries

请注意 Spring 云连接器 is in maintenance mode. Please consider switching to Java CFEnv instead. Java CFEnv is more flexible in terms of allowing you to use the JDBC URL directly(根据需要进行修改)或允许使用 Spring 引导属性覆盖连接详细信息。