提交 dcdd4b0d 编写于 作者: J Jason Song

Use dto namespace instead of model and updated the demo page

上级 47478b47
package com.ctrip.apollo.biz.service;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
/**
* Config Service
......
......@@ -5,7 +5,7 @@ import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
......
......@@ -4,7 +4,7 @@ import com.ctrip.apollo.biz.entity.ReleaseSnapShot;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.repository.ReleaseSnapShotRepository;
import com.ctrip.apollo.biz.repository.VersionRepository;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
......
......@@ -19,6 +19,8 @@ import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MutablePropertySources;
import java.util.concurrent.atomic.AtomicReference;
/**
* Client side config manager
*
......@@ -26,12 +28,17 @@ import org.springframework.core.env.MutablePropertySources;
*/
public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ApplicationContextAware {
public static final String APOLLO_PROPERTY_SOURCE_NAME = "ApolloConfigProperties";
private static AtomicReference<ApolloConfigManager> singletonProtector = new AtomicReference<ApolloConfigManager>();
private ConfigLoader configLoader;
private ConfigurableApplicationContext applicationContext;
private CompositePropertySource currentPropertySource;
public ApolloConfigManager() {
if(!singletonProtector.compareAndSet(null, this)) {
throw new IllegalStateException("There should be only one ApolloConfigManager instance!");
}
this.configLoader = ConfigLoaderFactory.getInstance().getRemoteConfigLoader();
}
......@@ -53,7 +60,7 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registerDependentBeans(registry);
preparePropertySource();
initializePropertySource();
}
/**
......@@ -94,14 +101,19 @@ public class ApolloConfigManager implements BeanDefinitionRegistryPostProcessor,
* First try to load from remote
* If loading from remote failed, then fall back to local cached properties
*/
void preparePropertySource() {
void initializePropertySource() {
currentPropertySource = loadPropertySource();
MutablePropertySources currentPropertySources = applicationContext.getEnvironment().getPropertySources();
if (currentPropertySources.contains(APOLLO_PROPERTY_SOURCE_NAME)) {
currentPropertySources.remove(APOLLO_PROPERTY_SOURCE_NAME);
if (currentPropertySources.contains(currentPropertySource.getName())) {
currentPropertySources.remove(currentPropertySource.getName());
}
currentPropertySources.addFirst(currentPropertySource);
}
CompositePropertySource composite = new CompositePropertySource(APOLLO_PROPERTY_SOURCE_NAME);
composite.addPropertySource(configLoader.loadPropertySource());
currentPropertySources.addFirst(composite);
CompositePropertySource loadPropertySource() {
CompositePropertySource compositePropertySource = new CompositePropertySource(APOLLO_PROPERTY_SOURCE_NAME);
compositePropertySource.addPropertySource(configLoader.loadPropertySource());
return compositePropertySource;
}
}
package com.ctrip.apollo.client.loader.impl;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
import com.ctrip.apollo.client.loader.ConfigLoader;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.CompositePropertySource;
......@@ -21,11 +16,10 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.client.loader.ConfigLoader;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
/**
* Load config from remote config server
......
package com.ctrip.apollo.client;
import com.ctrip.apollo.client.loader.ConfigLoader;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
......@@ -16,6 +18,8 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
......@@ -48,6 +52,13 @@ public class ApolloConfigManagerTest {
ReflectionTestUtils.setField(apolloConfigManager, "configLoader", configLoader);
}
@After
public void tearDown() throws Exception {
AtomicReference<ApolloConfigManager> singletonProtector =
(AtomicReference<ApolloConfigManager>)ReflectionTestUtils.getField(ApolloConfigManager.class, "singletonProtector");
singletonProtector.set(null);
}
@Test(expected = RuntimeException.class)
public void testInvalidApplicationContext() {
ApplicationContext someInvalidApplication = mock(ApplicationContext.class);
......@@ -61,7 +72,7 @@ public class ApolloConfigManagerTest {
when(configLoader.loadPropertySource()).thenReturn(somePropertySource);
apolloConfigManager.preparePropertySource();
apolloConfigManager.initializePropertySource();
verify(configLoader, times(1)).loadPropertySource();
verify(mutablePropertySources, times(1)).addFirst(captor.capture());
......@@ -74,7 +85,7 @@ public class ApolloConfigManagerTest {
@Test
public void testPostProcessBeanDefinitionRegistry() {
doNothing().when(apolloConfigManager).preparePropertySource();
doNothing().when(apolloConfigManager).initializePropertySource();
apolloConfigManager.postProcessBeanDefinitionRegistry(beanDefinitionRegistry);
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.client.loader.impl;
import com.ctrip.apollo.client.model.ApolloRegistry;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Before;
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.configserver.controller;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.configserver.controller;
import com.ctrip.apollo.biz.entity.Version;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.core.model.ApolloConfig;
import com.ctrip.apollo.core.dto.ApolloConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......
package com.ctrip.apollo.core.model;
package com.ctrip.apollo.core.dto;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
......
......@@ -35,13 +35,12 @@ public class DemoController {
@RequestMapping(value = "/config/{configName:.*}", method = RequestMethod.GET)
public Config queryConfig(@PathVariable String configName) {
String value;
if (configName.equals("foo")) {
value = demoService.getFoo();
} else {
value = env.getProperty(configName, "undefined");
}
return new Config(configName, value);
return new Config(configName, env.getProperty(configName, "undefined"));
}
@RequestMapping(value = "/injected/config", method = RequestMethod.GET)
public Config queryInjectedConfig() {
return new Config("apollo.foo", demoService.getFoo());
}
@RequestMapping(value = "/client/registries", method = RequestMethod.GET)
......
......@@ -12,6 +12,7 @@
this.registries = {};
this.configQuery = {};
//this.refreshResult = NONE;
this.injectedConfigValue = '';
var self = this;
......@@ -35,6 +36,16 @@
});
};
this.queryInjectedConfig = function () {
$http.get("demo/injected/config")
.success(function(data) {
self.injectedConfigValue = data.value;
})
.error(function(data, status) {
toastr.error((data && data.msg) || 'Load injected config failed');
});
};
//this.refreshConfig = function() {
// $http.post("refresh")
// .success(function(data) {
......
......@@ -14,14 +14,15 @@
<tr ng-repeat="item in demoCtrl.registries">
<td>{{item.appId}}</td>
<td>{{item.version}}</td>
</tr.>
</tr>
</tbody>
</table>
</div>
<div id="load-config-wrapper">
<h3>Load Config:</h3>
<form name="loadConfigForm" class="form-horizontal" novalidate ng-submit="demoCtrl.queryConfig()">
<form name="loadConfigForm" class="form-horizontal" novalidate
ng-submit="demoCtrl.queryConfig()">
<div class="form-group"
ng-class="{ 'has-error' : loadConfigForm.configName.$invalid && loadConfigForm.configName.$dirty}">
<div id="input-config-wrapper" class="clearfix">
......@@ -50,12 +51,31 @@
</form>
</div>
<div>
<h3>Load Injected Config:</h3>
<form name="loadInjectedConfigForm" class="form-horizontal" novalidate
ng-submit="demoCtrl.queryInjectedConfig()">
<div class="form-group">
<div class="clearfix">
<label class="col-sm-2 control-label">apollo.foo</label>
<div class="col-sm-3">
<input type="text" name="injectedConfigValue" class="form-control"
ng-model="demoCtrl.injectedConfigValue" readonly/>
</div>
<input type="submit" class="btn btn-primary col-sm-1" value="query"/>
</div>
</div>
</form>
</div>
<!--<div id="refresh-config-wrapper">-->
<!--<h3>Refresh Config:</h3>-->
<!--<button type="button" class="btn btn-primary" ng-click="demoCtrl.refreshConfig()">Refresh Config</button>-->
<!--<div id="refresh-result">-->
<!--<strong>Changed Properties:</strong> {{demoCtrl.refreshResult}}-->
<!--</div>-->
<!--<h3>Refresh Config:</h3>-->
<!--<button type="button" class="btn btn-primary" ng-click="demoCtrl.refreshConfig()">Refresh Config</button>-->
<!--<div id="refresh-result">-->
<!--<strong>Changed Properties:</strong> {{demoCtrl.refreshResult}}-->
<!--</div>-->
<!--</div>-->
</div>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册