Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: config support profile and multiple values (#3552) #3553

Open
wants to merge 2 commits into
base: 2021.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* {@link ConfigDataResource}.
*
* @author freeman
* @author yuxin.wang
* @since 2021.0.1.0
*/
public class NacosConfigDataLocationResolver
Expand Down Expand Up @@ -152,28 +153,50 @@ public List<NacosConfigDataResource> resolveProfileSpecific(

registerConfigManager(properties, bootstrapContext);

return loadConfigDataResources(location, profiles, properties);
return loadConfigDataResources(location.split(), profiles, properties);
}

private List<NacosConfigDataResource> loadConfigDataResources(
ConfigDataLocation location, Profiles profiles,
ConfigDataLocation[] configDataLocations, Profiles profiles,
NacosConfigProperties properties) {
List<NacosConfigDataResource> result = new ArrayList<>();
for (ConfigDataLocation configDataLocation: configDataLocations) {
result.add(loadConfigDataResource(configDataLocation, profiles, null, properties));
for (String profile: profiles) {
result.add(loadConfigDataResource(getProfileConfigDataLocation(configDataLocation, profile),
profiles, profile, properties));
}
}
return result;
}

ConfigDataLocation getProfileConfigDataLocation(ConfigDataLocation configDataLocation, String profile) {
StringBuilder profileResourceLocation = new StringBuilder(
configDataLocation.isOptional() ? ConfigDataLocation.OPTIONAL_PREFIX : "");
String resourceLocation = configDataLocation.getValue();
int paramIndex = resourceLocation.indexOf('?') < 0 ? resourceLocation.length() : resourceLocation.indexOf('?');
int extensionIndex = resourceLocation.lastIndexOf('.', paramIndex);
profileResourceLocation.append(resourceLocation, 0, extensionIndex > 0 ? extensionIndex : paramIndex);
profileResourceLocation.append('-').append(profile);
profileResourceLocation.append(resourceLocation.substring(extensionIndex > 0 ? extensionIndex : paramIndex));
return ConfigDataLocation.of(profileResourceLocation.toString());
}

private NacosConfigDataResource loadConfigDataResource(
ConfigDataLocation location, Profiles profiles, String profile,
NacosConfigProperties properties) {
URI uri = getUri(location, properties);

if (StringUtils.isBlank(dataIdFor(uri))) {
throw new IllegalArgumentException("dataId must be specified");
}

NacosConfigDataResource resource = new NacosConfigDataResource(properties,
location.isOptional(), profiles, log,
return new NacosConfigDataResource(properties,
location.isOptional(), profiles, profile, log,
new NacosItemConfig().setGroup(groupFor(uri, properties))
.setDataId(dataIdFor(uri)).setSuffix(suffixFor(uri, properties))
.setRefreshEnabled(refreshEnabledFor(uri, properties))
.setPreference(preferenceFor(uri)));
result.add(resource);

return result;
}

private String preferenceFor(URI uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

/**
* @author freeman
* @author yuxin.wang
* @since 2021.0.1.0
*/
public class NacosConfigDataResource extends ConfigDataResource {
Expand All @@ -36,17 +37,20 @@ public class NacosConfigDataResource extends ConfigDataResource {

private final boolean optional;

private final String profile;

private final Profiles profiles;

private final Log log;

private final NacosItemConfig config;

public NacosConfigDataResource(NacosConfigProperties properties, boolean optional,
Profiles profiles, Log log, NacosItemConfig config) {
Profiles profiles, String profile, Log log, NacosItemConfig config) {
this.properties = properties;
this.optional = optional;
this.profiles = profiles;
this.profile = profile;
this.log = log;
this.config = config;
}
Expand All @@ -63,6 +67,10 @@ public String getProfiles() {
return StringUtils.collectionToCommaDelimitedString(getAcceptedProfiles());
}

public String getProfile() {
return profile;
}

List<String> getAcceptedProfiles() {
return this.profiles.getAccepted();
}
Expand Down Expand Up @@ -97,7 +105,7 @@ public int hashCode() {
@Override
public String toString() {
return "NacosConfigDataResource{" + "properties=" + properties + ", optional="
+ optional + ", profiles=" + profiles + ", config=" + config + '}';
+ optional + ", profiles=" + profiles + ", profile=" + profile + ", config=" + config + '}';
}

public static class NacosItemConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* NacosConfigDataLocationResolver Tester.
*
* @author freeman
* @author yuxin.wang
*/
public class NacosConfigDataLocationResolverTest {

Expand Down Expand Up @@ -198,6 +199,7 @@ void testCommonPropertiesHasLowerPriority() {
private List<NacosConfigDataResource> testUri(String locationUri,
String... activeProfiles) {
Profiles profiles = mock(Profiles.class);
when(profiles.iterator()).thenReturn(Collections.emptyIterator());
when(profiles.getActive()).thenReturn(Arrays.asList(activeProfiles));
return this.resolver.resolveProfileSpecific(context,
ConfigDataLocation.of(locationUri), profiles);
Expand All @@ -209,8 +211,10 @@ void whenNoneInBootstrapContext_thenCreateNewConfigClientProperties() {
.thenReturn(false);
when(bootstrapContext.get(eq(NacosConfigProperties.class)))
.thenReturn(new NacosConfigProperties());
Profiles profiles = mock(Profiles.class);
when(profiles.iterator()).thenReturn(Collections.emptyIterator());
List<NacosConfigDataResource> resources = this.resolver.resolveProfileSpecific(
context, ConfigDataLocation.of("nacos:test.yml"), mock(Profiles.class));
context, ConfigDataLocation.of("nacos:test.yml"), profiles);
assertThat(resources).hasSize(1);
verify(bootstrapContext, times(0)).get(eq(NacosConfigProperties.class));
NacosConfigDataResource resource = resources.get(0);
Expand All @@ -224,6 +228,7 @@ private NacosConfigDataResource testResolveProfileSpecific() {

private NacosConfigDataResource testResolveProfileSpecific(String activeProfile) {
Profiles profiles = mock(Profiles.class);
when(profiles.iterator()).thenReturn(Collections.emptyIterator());
if (activeProfile != null) {
when(profiles.getActive())
.thenReturn(Collections.singletonList(activeProfile));
Expand All @@ -237,4 +242,31 @@ private NacosConfigDataResource testResolveProfileSpecific(String activeProfile)
return resources.get(0);
}

@Test
void testGetProfileConfigDataLocationIsOK() {
String[] locations = new String[]{
"nacos:nacos.yaml",
"optional:nacos:nacos.yaml",
"optional:nacos:nacos.yaml?group=DEFAULT_GROUP",
"optional:nacos:nacos.yaml?group=DEFAULT.GROUP",
"optional:nacos:nacos",
"optional:nacos:nacos?group=DEFAULT_GROUP",
"optional:nacos:nacos?group=DEFAULT.GROUP"
};
String[][] profileLocations = new String[][]{
new String[]{"nacos:nacos-profile.yaml", "false"},
new String[]{"nacos:nacos-profile.yaml", "true"},
new String[]{"nacos:nacos-profile.yaml?group=DEFAULT_GROUP", "true"},
new String[]{"nacos:nacos-profile.yaml?group=DEFAULT.GROUP", "true"},
new String[]{"nacos:nacos-profile", "true"},
new String[]{"nacos:nacos-profile?group=DEFAULT_GROUP", "true"},
new String[]{"nacos:nacos-profile?group=DEFAULT.GROUP", "true"},
};
for (int i = 0; i < locations.length; i++) {
ConfigDataLocation configDataLocation = ConfigDataLocation.of(locations[i]);
ConfigDataLocation profileConfigDataLocation = this.resolver.getProfileConfigDataLocation(configDataLocation, "profile");
assertThat(profileConfigDataLocation.getValue()).isEqualTo(profileLocations[i][0]);
assertThat(profileConfigDataLocation.isOptional()).isEqualTo(Boolean.parseBoolean(profileLocations[i][1]));
}
}
}