| 1 | |
package com.ctrip.framework.apollo.biz.service; |
| 2 | |
|
| 3 | |
import com.google.common.base.Strings; |
| 4 | |
|
| 5 | |
import com.ctrip.framework.apollo.biz.entity.ServerConfig; |
| 6 | |
import com.ctrip.framework.apollo.biz.repository.ServerConfigRepository; |
| 7 | |
import com.ctrip.framework.apollo.core.ConfigConsts; |
| 8 | |
import com.ctrip.framework.foundation.Foundation; |
| 9 | |
|
| 10 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 11 | |
import org.springframework.stereotype.Service; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
@Service |
| 17 | 7 | public class ServerConfigService { |
| 18 | |
@Autowired |
| 19 | |
private ServerConfigRepository serverConfigRepository; |
| 20 | |
|
| 21 | |
public String getValue(String key) { |
| 22 | 6 | ServerConfig serverConfig = null; |
| 23 | |
|
| 24 | |
|
| 25 | 6 | if (!Strings.isNullOrEmpty(System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY))) { |
| 26 | 3 | serverConfig = |
| 27 | 3 | serverConfigRepository.findTopByKeyAndCluster(key, System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY)); |
| 28 | |
} |
| 29 | |
|
| 30 | |
|
| 31 | 6 | if (serverConfig == null && !Strings.isNullOrEmpty(getDataCenter())) { |
| 32 | 3 | serverConfig = serverConfigRepository.findTopByKeyAndCluster(key, getDataCenter()); |
| 33 | |
} |
| 34 | |
|
| 35 | |
|
| 36 | 6 | if (serverConfig == null) { |
| 37 | 4 | serverConfig = |
| 38 | 4 | serverConfigRepository.findTopByKeyAndCluster(key, ConfigConsts.CLUSTER_NAME_DEFAULT); |
| 39 | |
} |
| 40 | |
|
| 41 | 6 | return serverConfig == null ? null : serverConfig.getValue(); |
| 42 | |
} |
| 43 | |
|
| 44 | |
public String getValue(String key, String defaultValue) { |
| 45 | 0 | String value = getValue(key); |
| 46 | 0 | return value == null ? defaultValue : value; |
| 47 | |
} |
| 48 | |
|
| 49 | |
String getDataCenter() { |
| 50 | 5 | return Foundation.server().getDataCenter(); |
| 51 | |
} |
| 52 | |
|
| 53 | |
} |