Add default-label property to Jdbc Repository (#2176)

This commit is contained in:
woshikid
2023-01-06 23:00:06 +08:00
committed by GitHub
parent 83aef4026f
commit c43fdc770f
5 changed files with 36 additions and 2 deletions

View File

@@ -891,6 +891,8 @@ The database needs to have a table called `PROPERTIES` with columns called `APPL
All fields are of type String in Java, so you can make them `VARCHAR` of whatever length you need.
Property values behave in the same way as they would if they came from Spring Boot properties files named `{application}-{profile}.properties`, including all the encryption and decryption, which will be applied as post-processing steps (that is, not in the repository implementation directly).
NOTE: The default label used for JDBC is `master`. You can change that by setting `spring.cloud.config.server.jdbc.defaultLabel`.
==== Redis Backend
Spring Cloud Config Server supports Redis as a backend for configuration properties.

View File

@@ -51,6 +51,8 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
*/
private boolean failOnError = true;
private String defaultLabel = "master";
public boolean isEnabled() {
return enabled;
}
@@ -97,4 +99,12 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
return !this.sql.equals(DEFAULT_SQL) && this.sqlWithoutProfile.equals(DEFAULT_SQL_WITHOUT_PROFILE);
}
public String getDefaultLabel() {
return this.defaultLabel;
}
public void setDefaultLabel(String defaultLabel) {
this.defaultLabel = defaultLabel;
}
}

View File

@@ -69,6 +69,8 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
private boolean configIncomplete;
private String defaultLabel;
@Deprecated
public JdbcEnvironmentRepository(JdbcTemplate jdbc, JdbcEnvironmentProperties properties) {
this(jdbc, properties, new PropertiesResultSetExtractor());
@@ -83,6 +85,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
this.failOnError = properties.isFailOnError();
this.extractor = extractor;
this.configIncomplete = properties.isConfigIncomplete();
this.defaultLabel = properties.getDefaultLabel();
}
public String getSql() {
@@ -97,7 +100,7 @@ public class JdbcEnvironmentRepository implements EnvironmentRepository, Ordered
public Environment findOne(String application, String profile, String label) {
String config = application;
if (StringUtils.isEmpty(label)) {
label = "master";
label = this.defaultLabel;
}
if (!StringUtils.hasText(profile)) {
profile = "default";

View File

@@ -207,6 +207,22 @@ public class JdbcEnvironmentRepositoryTests {
assertThatThrownBy(() -> repository.findOne("foo", "bar", "")).isInstanceOf(DataAccessException.class);
}
@Test
public void testCustomLabel() {
JdbcEnvironmentProperties properties = new JdbcEnvironmentProperties();
properties.setDefaultLabel("main");
Environment env = new JdbcEnvironmentRepository(new JdbcTemplate(this.dataSource), properties,
new JdbcEnvironmentRepository.PropertiesResultSetExtractor()).findOne("foo", "bar", "");
assertThat(env.getName()).isEqualTo("foo");
assertThat(env.getProfiles()).isEqualTo(new String[] { "bar" });
assertThat(env.getLabel()).isEqualTo("main");
assertThat(env.getPropertySources()).isNotEmpty();
assertThat(env.getPropertySources().get(0).getName()).isEqualTo("foo-bar");
assertThat(env.getPropertySources().get(0).getSource().get("a.b.c")).isEqualTo("foo-bar");
assertThat(env.getPropertySources().get(1).getName()).isEqualTo("application-bar");
assertThat(env.getPropertySources().get(1).getSource().get("a.b.c")).isEqualTo("application-bar");
}
@ImportAutoConfiguration(SqlInitializationAutoConfiguration.class)
@Configuration(proxyBeanMethods = false)
protected static class ApplicationConfiguration {

View File

@@ -10,4 +10,7 @@ INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('foo', null, 'master', 'a.b.c', 'foo-null');
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', 'bar', 'master', 'a.b.c', 'application-bar');
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', 'default', 'master', 'a.b.c', 'application-default');
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', null, 'master', 'a.b.c', 'application-null');
INSERT into MY_PROPERTIES(APPLICATION, PROFILE, LABEL, MY_KEY, MY_VALUE) values ('application', null, 'master', 'a.b.c', 'application-null');
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('foo', 'bar', 'main', 'a.b.c', 'foo-bar');
INSERT into PROPERTIES(APPLICATION, PROFILE, LABEL, "KEY", "VALUE") values ('application', 'bar', 'main', 'a.b.c', 'application-bar');