【问题标题】:Spring Boot and Spring Cloud AWS Data source pool configurationSpring Boot 和 Spring Cloud AWS 数据源池配置
【发布时间】:2017-11-13 14:19:27
【问题描述】:

我在 EC2 实例上有一个应用程序连接到 RDS (MySQL),8 小时后数据库连接从 MySQL 关闭,当应用程序尝试读/写数据时,我得到以下异常

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.dao.DataAccessResourceFailureException: could not extract ResultSet;
nested exception is org.hibernate.exception.JDBCConnectionException: could not extract ResultSet] with root cause
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

还有这个例外:

Request processing failed; nested exception is
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction;
nested exception is javax.persistence.PersistenceException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionEx
ception: No operations allowed after connection closed.] with root cause

java.net.SocketException: Connection timed out (Write failed)

这会在应用程序运行 8 小时后发生。 我的配置文件(YAML):

management:
  security:
    enabled: false
spring:
  profiles: prod
  datasource:
    tomcat:
      max-active: 20
      max-idle: 10
      min-idle: 5
      initial-size: 5
      test-while-idle: true
      test-on-borrow: true
      test-on-return: true
      validation-query: select 2 from dual
      validation-interval: 3600
      time-between-eviction-runs-millis: 5000
  jpa:
    database: MYSQL
    generate-ddl: false
    show-sql: true
    properties:
      globally_quoted_identifiers: true 
    hibernate:
      ddl-auto: none
cloud:
  aws:
    stack:
      auto: false
    region:
      static: *****
    credentials:
      instanceProfile: true
    rds:
      dev-db:
        databaseName: dev-db
        username: ******
        password: ******

我正在使用:

  • Java 1.8
  • Spring boot 1.5.4.RELEASE(JAR 部署)
  • Spring Cloud AWS JDBC 和 AWS 自动配置,1.1.3.RELEASE

我的 POM 是:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-aws-autoconfigure</artifactId>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <repositories>
        <repository>
            <id>io.spring.repo.maven.release</id>
            <url>http://repo.spring.io/release/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

我的问题是:

如何在 Spring Boot for AWS (Amazon) 中配置数据源池? 在 EC2 上部署应用程序后,我记录了 DataSource 配置,但未配置为 test-while-idle 和其他配置,这里是来自 EC2 的日志:

Data source Class Impl: class org.apache.tomcat.jdbc.pool.DataSource
 TimeBetweenEvictionRunsMillis: 5000
 ValidationInterval: 3000
 isTestOnBorrow: false
 isTestOnBorrow: false
 isTestOnBorrow: false

I checked this Page 但找不到从属性文件(在我的情况下为 yaml)配置池的方法...

【问题讨论】:

    标签: spring-boot jdbc-pool spring-cloud-aws


    【解决方案1】:

    我找到了以下解决方法,但最好通过相同的 spring AWS-JDBC 自动配置属性来支持它。我添加了以下内容 (from spring cloud aws)

    @Configuration
    @EnableRdsInstance(dbInstanceIdentifier = "test",password = "secret")
    public class ApplicationConfiguration {
    
    @Bean
    public RdsInstanceConfigurer instanceConfigurer() {
        return new RdsInstanceConfigurer() {
            @Override
            public DataSourceFactory getDataSourceFactory() {
                TomcatJdbcDataSourceFactory dataSourceFactory = new TomcatJdbcDataSourceFactory();
                dataSourceFactory.setInitialSize(10);
                dataSourceFactory.setValidationQuery("SELECT 1 FROM DUAL");
                dataSourceFactory.setValidationInterval(10000);
                dataSourceFactory.setTimeBetweenEvictionRunsMillis(20000);
                return dataSourceFactory;
            }
        };
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 2015-01-26
      • 2021-03-16
      • 2018-03-22
      • 1970-01-01
      • 2018-06-14
      • 2018-10-16
      相关资源
      最近更新 更多