Cloud Foundry spring boot数据源池配置

时间:2017-03-02 02:16:15

标签: postgresql spring-boot pivotal-cloud-foundry

我正在使用spring boot并使用postgres,rabbit mq和在CF上部署应用程序。我们认为我们需要设置连接池,我们发现无论我们做什么配置,在CF上我们最多可以连接4个连接,不确定我们从哪里获得该数字(可能是buildpack或service config)。

为了解决我必须扩展AbstractCloudConfig,这很痛苦,因为它关闭了其他自动配置,所以现在我必须手动配置兔mq连接工厂:(。我已经提出了以下配置,但没有确定这是正确的方式。

Spring boot版本:1.4

请告知。

package com.example;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.config.java.AbstractCloudConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;

/**
 * If we need to modify some some custom service configuration on cloud foundry
 * e.g. setting up of connection pools. If we set normally and expose bean, it
 * will work fine on local machine. But as it will go to Cloud foundry it
 * somehow creates max 4 connections. (Not sure from where this number comes)
 * 
 * Adding this configuration meaning we no longer want to leverage auto
 * configuration. As soon as Spring boot sees this bean in cloud profile it will
 * turn of auto configuration. Expectation is application is going to take care
 * of all configuration. This normally works for most of the applications.
 * 
 * For more information read: https://github.com/dsyer/cloud-middleware-blog
 * https://docs.cloudfoundry.org/buildpacks/java/spring-service-bindings.html
 *
 * Hopefully future release of spring boot will allow us to hijack only
 * configuration that we want to do ourselves and rest will be auto
 * configuration specifically in context with CloudFoundry.
 *
 */
@Configuration
@Profile("cloud")
public class CloudServicesConfig extends AbstractCloudConfig {

    @Value("${vcap.services.postgres.credentials.jdbc_uri}")
    private String postgresUrl;

    @Value("${vcap.services.postgres.credentials.username}")
    private String postgresUsername;

    @Value("${vcap.services.postgres.credentials.password}")
    private String postgresPassword;

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

    @Primary
    @Bean
    public DataSource dataSource() {
        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
        dataSource.setDriverClassName(dataSourceDriverClassName);
        dataSource.setUrl(postgresUrl);
        dataSource.setUsername(postgresUsername);
        dataSource.setPassword(postgresPassword);
        dataSource.setInitialSize(10);
        dataSource.setMaxIdle(5);
        dataSource.setMinIdle(5);
        dataSource.setMaxActive(25);
        return dataSource;
    }

    // You can add rest of services configuration below e.g. rabbit connection
    // factory, redis etc to centralize services configuration for cloud.
    // This example did not use profile but that is what you should use to
    // separate out cloud vs local configuraion to help run on local etc.

}

1 个答案:

答案 0 :(得分:1)

您不需要所有配置来自定义池大小。您应该只需要documentation

中显示的代码
@Bean
public DataSource dataSource() {
    PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
    DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, null);
    return connectionFactory().dataSource(dbConfig);
}