Commit 2784b0a2 authored by Yiming Liu's avatar Yiming Liu

Merge pull request #24 from nobodyiam/refactor-dto-namespace

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