Fixes ordering with local and remote sources with profiles.

If the context is associated with a profile, the Option.PROFILE_SPECIFIC is added.

Fixes gh-706
This commit is contained in:
spencergibb
2021-05-05 20:08:24 -04:00
parent 77416811bf
commit d6ab8a64b6
11 changed files with 237 additions and 127 deletions

View File

@@ -16,102 +16,14 @@
package org.springframework.cloud.consul.configdatatests;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author Spencer Gibb
*/
@SpringBootApplication
@RestController
@EnableConfigurationProperties
@Slf4j
public class ConsulConfigDataApplication {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private Environment env;
@Autowired
private RestTemplate restTemplate;
@Autowired
private Registration registration;
@Value("${spring.application.name:testConsulApp}")
private String appName;
public static void main(String[] args) {
SpringApplication.run(ConsulConfigDataApplication.class, args);
}
@RequestMapping("/me")
public ServiceInstance me() {
return this.registration;
}
@RequestMapping("/")
public ServiceInstance lb() {
return this.loadBalancer.choose(this.appName);
}
@RequestMapping("/rest")
public String rest() {
return this.restTemplate.getForObject("http://" + this.appName + "/me", String.class);
}
@RequestMapping("/choose")
public String choose() {
return this.loadBalancer.choose(this.appName).getUri().toString();
}
@RequestMapping("/myenv")
public String env(@RequestParam("prop") String prop) {
return this.env.getProperty(prop, "Not Found");
}
@RequestMapping("/prop")
public String prop() {
return sampleProperties().getProp();
}
@RequestMapping("/instances")
public List<ServiceInstance> instances() {
return this.discoveryClient.getInstances(this.appName);
}
@Bean
public SampleProperties sampleProperties() {
return new SampleProperties();
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2018-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.consul.configdatatests;
import java.util.Map;
import java.util.UUID;
import com.ecwid.consul.v1.ConsulClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.consul.ConsulProperties;
import org.springframework.cloud.consul.config.ConsulConfigProperties;
import org.springframework.cloud.consul.test.ConsulTestcontainers;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest(classes = ConsulConfigDataApplication.class,
properties = { "spring.application.name=" + ConsulConfigDataOrderingIntegrationTests.APP_NAME,
"spring.config.name=orderingtest", "spring.profiles.active=dev",
"management.endpoints.web.exposure.include=*" },
webEnvironment = RANDOM_PORT)
public class ConsulConfigDataOrderingIntegrationTests {
private static final String BASE_PATH = new WebEndpointProperties().getBasePath();
static final String APP_NAME = "testConsulConfigDataOrderingIntegration";
private static final String PREFIX = "_configDataOrderingIntegrationTests_config__";
private static final String ROOT = PREFIX + UUID.randomUUID();
private static final String VALUE = "my value from consul default profile";
private static final String TEST_PROP = "my.prop";
private static final String KEY = ROOT + "/" + APP_NAME + "/" + TEST_PROP;
private static final String VALUE_PROFILE = "my value from consul dev profile";
private static final String KEY_PROFILE = ROOT + "/" + APP_NAME + ",dev/" + TEST_PROP;
@Autowired
private Environment env;
@BeforeAll
public static void initialize() {
ConsulTestcontainers.initializeSystemProperties();
System.setProperty(ConsulConfigProperties.PREFIX + ".prefix", ROOT);
ConsulClient client = ConsulTestcontainers.client();
client.deleteKVValues(PREFIX);
client.setKVValue(KEY, VALUE);
client.setKVValue(KEY_PROFILE, VALUE_PROFILE);
}
@AfterAll
public static void close() {
System.clearProperty(ConsulProperties.PREFIX + ".port");
System.clearProperty(ConsulProperties.PREFIX + ".host");
System.clearProperty(ConsulProperties.PREFIX + ".prefix");
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void contextLoads() {
Integer port = env.getProperty("local.server.port", Integer.class);
ResponseEntity<Map> response = new TestRestTemplate()
.getForEntity("http://localhost:" + port + BASE_PATH + "/env/my.prop", Map.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
Map res = response.getBody();
assertThat(res).containsKey("propertySources");
Map<String, Object> property = (Map<String, Object>) res.get("property");
assertThat(property).containsEntry("value", VALUE_PROFILE);
}
}

View File

@@ -0,0 +1 @@
my.prop=my value from local dev profile