Add samples for vanilla config client

This commit is contained in:
Dave Syer
2015-04-21 08:42:46 +02:00
parent 674b2e9e42
commit 539513c826
14 changed files with 448 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2014-2015 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
*
* http://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 apps;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author Dave Syer
*
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
start(args);
}
public static ConfigurableApplicationContext start(String... args) {
return new SpringApplicationBuilder(ConfigServer.class)
.showBanner(false)
.profiles("native")
.properties("server.port=8888",
"spring.cloud.config.server.native.searchLocation:file:./src/test/resources/config/")
.run(args);
}
}

View File

@@ -0,0 +1,85 @@
package demo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.Executors;
import org.aopalliance.intercept.MethodInterceptor;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.interceptor.RetryOperationsInterceptor;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import apps.ConfigServer;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = StandaloneClientApplication.class)
// Explicitly enable config client because test classpath has config server on it
@IntegrationTest({ "spring.cloud.config.enabled=true",
"logging.level.org.springframework.retry=TRACE" })
@DirtiesContext
public class StandaloneClientApplicationTests {
@Autowired
private ConfigServicePropertySourceLocator locator;
private static ConfigurableApplicationContext context;
@BeforeClass
public static void delayConfigServer() {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000L);
}
catch (InterruptedException e) {
}
context = ConfigServer.start();
}
});
}
@AfterClass
public static void shutdown() {
if (context!=null) {
context.close();
}
}
@Test
public void contextLoads() throws Exception {
assertTrue("ConfigServicePropertySourceLocator is not a proxy (so no retry)",
AopUtils.isAopProxy(locator));
AnnotationAwareRetryOperationsInterceptor advice = (AnnotationAwareRetryOperationsInterceptor) ((Advised) locator)
.getAdvisors()[0].getAdvice();
@SuppressWarnings("unchecked")
Map<Method, MethodInterceptor> delegates = (Map<Method, MethodInterceptor>) ReflectionTestUtils
.getField(advice, "delegates");
RetryOperationsInterceptor interceptor = (RetryOperationsInterceptor) delegates
.values().iterator().next();
RetryTemplate retryTemplate = (RetryTemplate) ReflectionTestUtils.getField(
interceptor, "retryOperations");
ExponentialBackOffPolicy backoff = (ExponentialBackOffPolicy) ReflectionTestUtils
.getField(retryTemplate, "backOffPolicy");
assertEquals(3000, backoff.getInitialInterval());
}
}