Skip to content

Commit

Permalink
refactor project config
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Dec 19, 2016
1 parent 7e4f6bc commit d68cae0
Show file tree
Hide file tree
Showing 42 changed files with 880 additions and 685 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.ctrip.framework.apollo.adminservice.aop;


import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.entity.NamespaceLock;
import com.ctrip.framework.apollo.biz.service.ItemService;
import com.ctrip.framework.apollo.biz.service.NamespaceLockService;
import com.ctrip.framework.apollo.biz.service.NamespaceService;
import com.ctrip.framework.apollo.biz.utils.ApolloSwitcher;
import com.ctrip.framework.apollo.common.dto.ItemChangeSets;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
Expand Down Expand Up @@ -39,7 +39,7 @@ public class NamespaceLockAspect {
@Autowired
private ItemService itemService;
@Autowired
private ApolloSwitcher apolloSwitcher;
private BizConfig bizConfig;


//create item
Expand Down Expand Up @@ -75,7 +75,7 @@ public void requireLockAdvice(long itemId, String operator) {

void acquireLock(String appId, String clusterName, String namespaceName,
String currentUser) {
if (apolloSwitcher.isNamespaceLockSwitchOff()) {
if (bizConfig.isNamespaceLockSwitchOff()) {
return;
}

Expand All @@ -85,7 +85,7 @@ void acquireLock(String appId, String clusterName, String namespaceName,
}

void acquireLock(long namespaceId, String currentUser) {
if (apolloSwitcher.isNamespaceLockSwitchOff()) {
if (bizConfig.isNamespaceLockSwitchOff()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ctrip.framework.apollo.adminservice.controller;

import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.entity.NamespaceLock;
import com.ctrip.framework.apollo.biz.service.NamespaceLockService;
import com.ctrip.framework.apollo.biz.service.NamespaceService;
import com.ctrip.framework.apollo.biz.utils.ApolloSwitcher;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
Expand All @@ -23,7 +23,7 @@ public class NamespaceLockController {
@Autowired
private NamespaceService namespaceService;
@Autowired
private ApolloSwitcher apolloSwitcher;
private BizConfig bizConfig;

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/lock")
public NamespaceLockDTO getNamespaceLockOwner(@PathVariable String appId, @PathVariable String clusterName,
Expand All @@ -33,7 +33,7 @@ public NamespaceLockDTO getNamespaceLockOwner(@PathVariable String appId, @PathV
throw new BadRequestException("namespace not exist.");
}

if (apolloSwitcher.isNamespaceLockSwitchOff()) {
if (bizConfig.isNamespaceLockSwitchOff()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.ctrip.framework.apollo.adminservice.aop;


import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.entity.NamespaceLock;
import com.ctrip.framework.apollo.biz.service.ItemService;
import com.ctrip.framework.apollo.biz.service.NamespaceLockService;
import com.ctrip.framework.apollo.biz.service.NamespaceService;
import com.ctrip.framework.apollo.biz.utils.ApolloSwitcher;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.ServiceException;

Expand Down Expand Up @@ -40,14 +40,14 @@ public class NamespaceLockTest {
@Mock
private ItemService itemService;
@Mock
private ApolloSwitcher apolloSwitcher;
private BizConfig bizConfig;
@InjectMocks
NamespaceLockAspect namespaceLockAspect;

@Test
public void acquireLockWithNotLockedAndSwitchON() {

when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(true);
when(bizConfig.isNamespaceLockSwitchOff()).thenReturn(true);

namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);

Expand All @@ -57,13 +57,13 @@ public void acquireLockWithNotLockedAndSwitchON() {
@Test
public void acquireLockWithNotLockedAndSwitchOFF() {

when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(bizConfig.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(APP, CLUSTER, NAMESPACE)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(anyLong())).thenReturn(null);

namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);

verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(bizConfig).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(APP, CLUSTER, NAMESPACE);
verify(namespaceLockService).findLock(anyLong());
verify(namespaceLockService).tryLock(any());
Expand All @@ -73,41 +73,41 @@ public void acquireLockWithNotLockedAndSwitchOFF() {
@Test(expected = BadRequestException.class)
public void acquireLockWithAlreadyLockedByOtherGuy() {

when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(bizConfig.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(APP, CLUSTER, NAMESPACE)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(mockNamespaceLock(ANOTHER_USER));

namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);

verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(bizConfig).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(APP, CLUSTER, NAMESPACE);
verify(namespaceLockService).findLock(NAMESPACE_ID);
}

@Test
public void acquireLockWithAlreadyLockedBySelf() {

when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(bizConfig.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(APP, CLUSTER, NAMESPACE)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(mockNamespaceLock(CURRENT_USER));

namespaceLockAspect.acquireLock(APP, CLUSTER, NAMESPACE, CURRENT_USER);

verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(bizConfig).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(APP, CLUSTER, NAMESPACE);
verify(namespaceLockService).findLock(NAMESPACE_ID);
}

@Test
public void acquireLockWithNamespaceIdSwitchOn(){

when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(bizConfig.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(NAMESPACE_ID)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(null);

namespaceLockAspect.acquireLock(NAMESPACE_ID, CURRENT_USER);

verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(bizConfig).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(NAMESPACE_ID);
verify(namespaceLockService).findLock(NAMESPACE_ID);
verify(namespaceLockService).tryLock(any());
Expand All @@ -116,14 +116,14 @@ public void acquireLockWithNamespaceIdSwitchOn(){
@Test(expected = ServiceException.class)
public void testDuplicateLock(){

when(apolloSwitcher.isNamespaceLockSwitchOff()).thenReturn(false);
when(bizConfig.isNamespaceLockSwitchOff()).thenReturn(false);
when(namespaceService.findOne(NAMESPACE_ID)).thenReturn(mockNamespace());
when(namespaceLockService.findLock(NAMESPACE_ID)).thenReturn(null);
when(namespaceLockService.tryLock(any())).thenThrow(DataIntegrityViolationException.class);

namespaceLockAspect.acquireLock(NAMESPACE_ID, CURRENT_USER);

verify(apolloSwitcher).isNamespaceLockSwitchOff();
verify(bizConfig).isNamespaceLockSwitchOff();
verify(namespaceService).findOne(NAMESPACE_ID);
verify(namespaceLockService, times(2)).findLock(NAMESPACE_ID);
verify(namespaceLockService).tryLock(any());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.ctrip.framework.apollo.biz.config;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.ctrip.framework.apollo.biz.service.BizDBPropertySource;
import com.ctrip.framework.apollo.common.config.RefreshableConfig;
import com.ctrip.framework.apollo.common.config.RefreshablePropertySource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Component
public class BizConfig extends RefreshableConfig {

private Gson gson = new Gson();
private static final Type namespaceValueLengthOverrideTypeReference =
new TypeToken<Map<Long, Integer>>() {
}.getType();

@Autowired
private BizDBPropertySource propertySource;

@Override
protected List<RefreshablePropertySource> getRefreshablePropertySources() {
return Collections.singletonList(propertySource);
}

public List<String> eurekaServiceUrls() {
String configuration = getValue("eureka.service.url", "");
if (Strings.isNullOrEmpty(configuration)) {
return Collections.emptyList();
}

return splitter.splitToList(configuration);
}

public int grayReleaseRuleScanInterval() {
return getIntProperty("apollo.gray-release-rule-scan.interval", 60);
}

public int itemKeyLengthLimit() {
return getIntProperty("item.key.length.limit", 128);
}

public int itemValueLengthLimit() {
return getIntProperty("item.value.length.limit", 20000);
}

public Map<Long, Integer> namespaceValueLengthLimitOverride() {
String namespaceValueLengthOverrideString = getValue("namespace.value.length.limit.override");
Map<Long, Integer> namespaceValueLengthOverride = Maps.newHashMap();
if (!Strings.isNullOrEmpty(namespaceValueLengthOverrideString)) {
namespaceValueLengthOverride =
gson.fromJson(namespaceValueLengthOverrideString, namespaceValueLengthOverrideTypeReference);
}

return namespaceValueLengthOverride;
}

public boolean isNamespaceLockSwitchOff() {
return !getBooleanProperty("namespace.lock.switch", false);
}

/**
* ctrip config
**/
public String cloggingUrl() {
return getValue("clogging.server.url");
}

public String cloggingPort() {
return getValue("clogging.server.port");
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ctrip.framework.apollo.biz.customize;

import com.ctrip.framework.apollo.biz.service.ServerConfigService;
import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.common.customize.LoggingCustomizer;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -11,28 +11,18 @@
@Profile("ctrip")
public class BizLoggingCustomizer extends LoggingCustomizer{

private static final String CLOGGING_SERVER_URL_KEY = "clogging.server.url";
private static final String CLOGGING_SERVER_PORT_KEY = "clogging.server.port";

@Autowired
private ServerConfigService serverConfigService;
private BizConfig bizConfig;

private String cloggingUrl;
private String cloggingPort;

@Override
protected String cloggingUrl() {
if (cloggingUrl == null){
cloggingUrl = serverConfigService.getValue(CLOGGING_SERVER_URL_KEY);
}
return cloggingUrl;
return bizConfig.cloggingUrl();
}

@Override
protected String cloggingPort() {
if (cloggingPort == null){
cloggingPort = serverConfigService.getValue(CLOGGING_SERVER_PORT_KEY);
}
return cloggingPort;
return bizConfig.cloggingPort();
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,29 @@
package com.ctrip.framework.apollo.biz.eureka;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;

import com.ctrip.framework.apollo.biz.service.ServerConfigService;
import com.ctrip.framework.apollo.biz.config.BizConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Primary
public class ApolloEurekaClientConfig extends EurekaClientConfigBean {
static final String EUREKA_URL_CONFIG = "eureka.service.url";
private static final Splitter URL_SPLITTER = Splitter.on(",").omitEmptyStrings();

@Autowired
private ServerConfigService serverConfigService;

@Autowired
private Environment environment;
private BizConfig bizConfig;

/**
* Assert only one zone: defaultZone, but multiple environments.
*/
public List<String> getEurekaServerServiceUrls(String myZone) {
//First check if there is any system property override
if (!Strings.isNullOrEmpty(environment.getProperty(EUREKA_URL_CONFIG))) {
return URL_SPLITTER.splitToList(environment.getProperty(EUREKA_URL_CONFIG));
}

//Second check if it is configured in database
String eurekaUrl = serverConfigService.getValue(EUREKA_URL_CONFIG);

if (!Strings.isNullOrEmpty(eurekaUrl)) {
return URL_SPLITTER.splitToList(eurekaUrl);

}

//fallback to default
return super.getEurekaServerServiceUrls(myZone);
List<String> urls = bizConfig.eurekaServiceUrls();
return CollectionUtils.isEmpty(urls) ? super.getEurekaServerServiceUrls(myZone) : urls;
}

@Override
Expand Down
Loading

0 comments on commit d68cae0

Please sign in to comment.