2017-03-02 118 views
1

我正在使用spring启动并使用postgres,rabbit mq和在CF上部署应用程序。我们认为我们需要设置连接池,我们发现无论我们做什么配置,在CF上我们都可以连接它的最大4个连接,但不知道从哪里获得那个数字(可能是带有buildpack或服务配置的东西)。云代工弹簧引导数据源池配置

为了解决这个问题,我不得不扩展AbstractCloudConfig,这很麻烦,因为它关闭了其他自动配置,所以现在我不得不手动配置rabbit 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. 

} 
+0

找到了这些:https://discuss.pivotal.io/hc/en-us/articles/221898227-Connection-pool-warning-message-maxIdle-is-larger-than-maxActive-在PCF部署的Spring-app和https://github.com/spring-cloud/spring-cloud-connectors/blob/master/spring-cloud-spring-service-中设置-max-4-see-in-PCF-deployed-连接器/ src/main/java/org/springframework/cloud/service/relational/DbcpLikePooledDataSourceCreator.java – user3444718

+0

你是在部署fat jar还是.war – ams

+0

jar,因为它是弹簧启动! – user3444718

回答

1

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

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