gpt4 book ai didi

postgresql - Cloud Foundry spring boot 数据源池配置

转载 作者:行者123 更新时间:2023-11-29 13:20:34 24 4
gpt4 key购买 nike

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

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

Spring 启动版本: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.

}

最佳答案

您不需要所有这些配置来自定义池大小。您应该只需要此代码,如 documentation 中所示:

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

关于postgresql - Cloud Foundry spring boot 数据源池配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42545645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com