簡體   English   中英

Amazon RDS Read Replica 配置來自部署在 PCF 上的 spring 引導應用程序的 Postgres 數據庫?

[英]Amazon RDS Read Replica configuration Postgres database from an spring boot application deployed on PCF?

大家好,目前我們已經將 springboot 代碼部署到在 aws 上運行的 pcf。

我們正在使用 aws 數據庫——我們有 cup 服務和 VCAP_SERVICES,它保存了 db 的參數。

在我們的配置下獲取數據源

 @Bean
    public DataSource dataSource() {
        if (dataSource == null) {
            dataSource = connectionFactory().dataSource();
            configureDataSource(dataSource);
        }
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
            return new JdbcTemplate(dataSource());
        }

    private void configureDataSource(DataSource dataSource) {
        org.apache.tomcat.jdbc.pool.DataSource tomcatDataSource = asTomcatDatasource(dataSource);
        tomcatDataSource.setTestOnBorrow(true);
        tomcatDataSource.setValidationQuery("SELECT 1");
        tomcatDataSource.setValidationInterval(30000);
        tomcatDataSource.setTestWhileIdle(true);
        tomcatDataSource.setTimeBetweenEvictionRunsMillis(60000);
        tomcatDataSource.setRemoveAbandoned(true);
        tomcatDataSource.setRemoveAbandonedTimeout(60);
        tomcatDataSource.setMaxActive(Environment.getAsInt("MAX_ACTIVE_DB_CONNECTIONS", tomcatDataSource.getMaxActive()));
    }

    private org.apache.tomcat.jdbc.pool.DataSource asTomcatDatasource(DataSource dataSource) {
        Objects.requireNonNull(dataSource, "There is no DataSource configured");
        DataSource targetDataSource = ((DelegatingDataSource)dataSource).getTargetDataSource();
        return (org.apache.tomcat.jdbc.pool.DataSource) targetDataSource;
    }

現在,當我們創建了只讀副本后,我需要修改什么配置才能使我們的 spring 啟動應用程序使用只讀副本?

get 調用中的 Just @Transactional(readOnly = true) 就足夠了——它會被自動處理嗎? 還是我需要添加更多配置

@Repository
public class PostgresSomeRepository implements SomeRepository {
  

    @Autowired
    public PostgresSomeRepository(JdbcTemplate jdbcTemplate, RowMapper<Consent> rowMapper) {
        this.jdbcTemplate = jdbcTemplate;
        this.rowMapper = rowMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SomeValue> getSomeGetCall(List<String> userIds, String applicationName, String propositionName, String since, String... types) {
      //Some Logic
        try {
            return jdbcTemplate.query(sql, rowMapper, paramList.toArray());
        } catch (DataAccessException ex) {
            throw new ErrorGettingConsent(ex.getMessage(), ex);
        }
    }  
}

注意:我們沒有添加任何 spring aws jdbc 依賴

假設雲服務名稱是my_db

  1. Map CF 中默認使用的應用程序配置appication-cloud.yml的雲服務(順便說一句,這比使用連接器更好,因為您可以自定義數據源)
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    # my_db
    url: ${vcap.services.my_db.credentials.url} 
    username: ${vcap.services.my_db.credentials.username} 
    password: ${vcap.services.my_db.credentials.password} 
    hikari:
      poolName: Hikari
      auto-commit: false
      data-source-properties:
        cachePrepStmts: true
        prepStmtCacheSize: 250
        prepStmtCacheSqlLimit: 2048
        useServerPrepStmts: true
  jpa:
    generate-ddl: false
    show-sql: true
  1. 將服務放入應用程序manifest.yml
---
applications:
  - name: my-app
    env:
      SPRING_PROFILES_ACTIVE: "cloud" # by default
    services:
      - my_db

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM