簡體   English   中英

Spring Boot具有重復屬性的多個數據源

[英]Spring Boot multiple datasources with repeating properties

我有一個設置我的Spring Web應用程序,其中有兩個數據源,主要數據源和次要數據源。 這兩個數據源主要共享除用戶名,密碼和URL之外的所有配置屬性。 隨着公共屬性列表的增長,我希望對兩個數據源使用公共配置屬性,並僅指定要為輔助數據源和其他數據源覆蓋哪些屬性。 例如,我已經設置了我的主數據源bean,如下所示:

@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

並在輔助數據源中:

@Value("${spring.secondaryDatasource.url}")
private String databaseUrl;

@Value("${spring.datasource.username}")
private String username;

@Value("${spring.datasource.password}")
private String password;

@Value("${spring.datasource.driver-class-name}")
private String driver;

@Bean
public DataSource secondaryDataSource() {
    return DataSourceBuilder
            .create()
            .url(databaseUrl)
            .username(username)
            .password(password)
            .driverClassName(driver)
            .build();
}

我還設置了一個類似於我當前設置的示例項目: https//github.com/Edvinas01/MultipleDatasources

是否可以注入重復屬性,例如驅動程序名稱和其他屬性,同時僅指定要覆蓋的屬性? 像這樣的東西(這不起作用):

@Bean
@ConfigurationProperties(prefix = "spring.datasource") // Inject default properties
public DataSource secondaryDataSource() {
    return DataSourceBuilder
            .create()
            .url(databaseUrl)   // Override url
            .username(username) // Override username
            .password(password) // Override  password
            .build();
}

編輯:

我已將.properties文件替換為.yml配置文件,如下所示:

spring:
  jpa.hibernate.ddl-auto: update
  datasource:
    username: main
    password: main
    url: jdbc:hsqldb:mem:main
    driver-class-name: org.hsqldb.jdbc.JDBCDriver

---

spring:
  profiles: secondary
  datasource:
    username: secondary
    password: secondary
    url: jdbc:hsqldb:mem:secondary

---

spring:
  profiles.active: default,secondary

和數據源bean:

@Bean
@Primary
@Profile("default")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@Profile({"secondary", "default"})
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

我的主數據源(默認)獲取輔助數據源值,但輔助配置文件配置中未指定的驅動程序除外。 輔助數據源獲取正確的屬性。

是否有可能的解決方案,不使用.yml格式的配置或不必為每個配置文件創建多個.applications文件?

Edit2:一些澄清,在我們的設置中,我們有當前的屬性文件:

application.properties (sets the active profile according to the machine and common properties)
application-stating.properties (staging machine)
application-production.properties (production machine)

登台和生產環境都必須使用兩個數據源,因此登台有兩個數據源(主要,次要),生產有兩個數據源(主要,次要)。 在主數據源和輔助數據源之間共享諸如驅動程序等設置。 嘗試將這些公共屬性注入第二個數據源時會出現問題。

我建議將Spring配置文件與YAML配置結合使用(如果可能,但它可以適應屬性文件)。

// Insert in your Spring Configuration Class

   @Profile("!production")
   @ConfigurationProperties(prefix = "spring.datasource")
   @Bean
   public DataSource dataSource() {
       return DataSourceBuilder.create().build();
   }

   @Profile("production")
   @ConfigurationProperties(prefix = "spring.datasource")
   @Bean
   public DataSource secondDataSource() {
      return DataSourceBuilder
        .create()
        .url(databaseUrl)   // Override url
        .username(username) // Override username
        .password(password) // Override  password
        .build();
   }

如果您使用適當的配置文件啟動應用程序,例如第二個(請參閱http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html ),那么您正確的DataSource是加載。 默認配置文件不需要設置spring.profiles.active值。

編輯:

無需創建其他配置文件或文件。 提醒 :您可以通過-Dspring.profiles.active="production"設置默認配置文件。 如果沒有默認配置文件設置,那么它的default ,你不必創建/定義。

好的,回到主題:

假設您希望使用“生產”和“暫存”配置文件,並為每個配置文件激活一個配置。 通過在第一個bean中用@Profile("default", "production")替換@Profile("default") ,你可以像上面那樣經典地完成它。 在第二個bean中,將@Profile("second")替換為@Profile("staging")

另一種方法是通過邏輯運算符來完成。 對於“生產”配置文件,您希望使用用戶名/密碼數據源。 所以這里需要一個@Profile("production")注釋。 對於非生產用途(你的情況staging ),你可以使用@profile(!“生產”)。

很高興閱讀: https//docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Profile.html

EDIT2:

對於具有一個屬性/ YAML文件的解決方案,我將采用以下方法:

# application.yml
# default settings
spring:
  datasource:
    # insert your default settings
---
spring:
   profiles: staging
   datasource:
   # insert staging settings here
---
spring:
   profiles: production
   datasource:
   # insert production settings here

這為每個配置文件保存了其他屬性文件。

最后,我們使用了兩個數據源的可重用屬性。 首先,我創建了一個bean來存儲這些常見屬性:

@Configuration
public class CommonPropertiesConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public PoolProperties poolProperties() {
        return new PoolProperties();
    }
}

在第二個數據源配置中,我自動裝配公共屬性(所有數據源屬性)並按值為此數據源注入特定屬性:

@Value("${spring.secondaryDatasource.username}")
private String username;

@Value("${spring.secondaryDatasource.password}")
private String password;

@Value("${spring.secondaryDatasource.url}")
private String url;

@Autowired
private PoolProperties poolProperties;

@Bean
public DataSource secondaryDataSource() {
    DataSource dataSource = new DataSource(poolProperties);
    dataSource.setPassword(password);
    dataSource.setUsername(username);
    dataSource.setUrl(url);
    return dataSource;
}

現在application.properties看起來像這樣:

# Main/common data source properties.
spring.datasource.username=test
spring.datasource.password=test

spring.datasource.url=jdbc:hsqldb:mem:main
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver

# Override these properties for second datasource.
spring.secondaryDatasource.username=second
spring.secondaryDatasource.password=second
spring.secondaryDatasource.url=jdbc:hsqldb:mem:secondary

logging.level.com.datasources=DEBUG
spring.jpa.hibernate.ddl-auto=update

更改也會反映在github存儲庫中。

暫無
暫無

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

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