Cloud Foundry的春天启动数据源池配置

问题描述 投票:1回答:1

我使用的春天开机和使用的Postgres,兔MQ和CF.部署应用程序我们计算过,我们需要设置连接池,我们发现,我们做任何配置,在CF我们可以其最大4个连接,而不是从那里我们得到的数字(可能是一些与buildpack或服务配置)确定。

为了解决我不得不延长AbstractCloudConfig,那就是痛苦,因为它关闭其它自动配置所以现在我必须手动配置兔子MQ连接工厂太:(我已经想出了下面的配置,但不知道这是正确的路。

春天引导版本: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.

}
postgresql spring-boot pivotal-cloud-foundry
1个回答
1
投票

你并不需要所有的配置只是为了自定义池的大小。你应该只需要这个代码所示documentation

@Bean
public DataSource dataSource() {
    PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
    DataSourceConfig dbConfig = new DataSourceConfig(poolConfig, null);
    return connectionFactory().dataSource(dbConfig);
}
© www.soinside.com 2019 - 2024. All rights reserved.