Skip to content

Commit

Permalink
Add DynamicUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Narzisss committed Oct 29, 2024
1 parent 7a354b3 commit b8c60ae
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeys;
import com.alipay.sofa.rpc.dynamic.DynamicConfigManager;
import com.alipay.sofa.rpc.dynamic.DynamicConfigManagerFactory;
import com.alipay.sofa.rpc.dynamic.DynamicUrl;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.invoke.Invoker;
import com.alipay.sofa.rpc.listener.ConfigListener;
Expand Down Expand Up @@ -172,13 +173,17 @@ public T refer() {
}

//接口级别动态配置参数
final String dynamicUrl = getOrDefault(DynamicConfigKeys.CENTER_ADDRESS);
if ( StringUtils.isNotBlank(dynamicUrl)) {
//启用接口级别动态配置
final DynamicConfigManager dynamicManager = DynamicConfigManagerFactory.getDynamicManagerWithUrl(
consumerConfig.getAppName(), dynamicUrl);
dynamicManager.addListener(consumerConfig.getInterfaceId(), configListener);
dynamicManager.initServiceConfiguration(consumerConfig.getInterfaceId(), configListener);
Boolean dynamicConfigRefreshEnable = getOrDefault(DynamicConfigKeys.DYNAMIC_REFRESH_ENABLE);
if (dynamicConfigRefreshEnable) {
String configCenterAddress = getOrDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS);
if (StringUtils.isNotBlank(configCenterAddress)) {
DynamicUrl dynamicUrl = new DynamicUrl(configCenterAddress);
//启用接口级别动态配置
final DynamicConfigManager dynamicManager = DynamicConfigManagerFactory.getDynamicManager(
consumerConfig.getAppName(), dynamicUrl.getProtocol());
dynamicManager.addListener(consumerConfig.getInterfaceId(), configListener);
dynamicManager.initServiceConfiguration(consumerConfig.getInterfaceId(), configListener);
}
}
} catch (Exception e) {
if (cluster != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import com.alipay.sofa.common.config.SofaConfigs;
import com.alipay.sofa.rpc.auth.AuthRuleGroup;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.dynamic.*;
import com.alipay.sofa.rpc.dynamic.ConfigChangeType;
import com.alipay.sofa.rpc.dynamic.ConfigChangedEvent;
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeyHelper;
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeys;
import com.alipay.sofa.rpc.dynamic.DynamicConfigManager;
import com.alipay.sofa.rpc.dynamic.DynamicHelper;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.listener.ConfigListener;
import com.ctrip.framework.apollo.Config;
Expand All @@ -29,7 +34,6 @@
import com.ctrip.framework.apollo.model.ConfigChange;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -51,31 +55,26 @@ public class ApolloDynamicConfigManager extends DynamicConfigManager {

private static final String APOLLO_CLUSTER_KEY = "apollo.cluster";

private static final String APOLLO_PROTOCOL_PREFIX = "http://";
private static final String APOLLO_PARAM_APPID_KEY = "appId";

private Config config;
private static final String APOLLO_PARAM_CLUSTER_KEY = "cluster";

private static final String APOLLO_PROTOCOL_PREFIX = "http://";
private final ConcurrentMap<String, ApolloListener> watchListenerMap = new ConcurrentHashMap<>();
private Config config;

protected ApolloDynamicConfigManager(String appName) {
super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.APOLLO_ADDRESS,""));
if (StringUtils.isNotBlank(appName)) {
System.setProperty(APOLLO_APPID_KEY, appName);
}
if (StringUtils.isNotBlank(getAddress())) {
System.setProperty(APOLLO_ADDR_KEY, APOLLO_PROTOCOL_PREFIX + getAddress());
}
config = ConfigService.getAppConfig();
}

protected ApolloDynamicConfigManager(String appName, String remainUrl) {
super(appName, remainUrl);
System.setProperty(APOLLO_APPID_KEY, appName);
System.setProperty(APOLLO_ADDR_KEY, APOLLO_PROTOCOL_PREFIX + getAddress());
Map params = getParams();
if (params != null && params.containsKey("cluster")) {
String clusterValue = (String)params.get("cluster");
System.setProperty(APOLLO_CLUSTER_KEY, clusterValue);
super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS, ""));
if (getDynamicUrl() != null) {
if (StringUtils.isNotBlank(getDynamicUrl().getParam(APOLLO_PARAM_APPID_KEY))) {
System.setProperty(APOLLO_APPID_KEY, getDynamicUrl().getParam(APOLLO_PARAM_APPID_KEY));
}
if (StringUtils.isNotBlank(getDynamicUrl().getAddress())) {
System.setProperty(APOLLO_ADDR_KEY, APOLLO_PROTOCOL_PREFIX + getDynamicUrl().getAddress());
}
if (StringUtils.isNotBlank(getDynamicUrl().getParam(APOLLO_PARAM_CLUSTER_KEY))) {
System.setProperty(APOLLO_CLUSTER_KEY, getDynamicUrl().getParam(APOLLO_PARAM_CLUSTER_KEY));
}
}
config = ConfigService.getAppConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
import com.alipay.sofa.common.config.SofaConfigs;
import com.alipay.sofa.rpc.auth.AuthRuleGroup;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.dynamic.*;
import com.alipay.sofa.rpc.dynamic.ConfigChangeType;
import com.alipay.sofa.rpc.dynamic.ConfigChangedEvent;
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeyHelper;
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeys;
import com.alipay.sofa.rpc.dynamic.DynamicConfigManager;
import com.alipay.sofa.rpc.dynamic.DynamicHelper;
import com.alipay.sofa.rpc.ext.Extension;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
Expand Down Expand Up @@ -59,34 +64,17 @@ public class NacosDynamicConfigManager extends DynamicConfigManager {
private Properties nacosConfig = new Properties();

protected NacosDynamicConfigManager(String appName) {
super(appName, SofaConfigs.getOrDefault(DynamicConfigKeys.NACOS_ADDRESS));
super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS, "nacos://127.0.0.1:8848"));
group = appName;
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, getAddress());
try {
configService = ConfigFactory.createConfigService(nacosConfig);
} catch (NacosException e) {
LOGGER.error("Failed to create ConfigService", e);
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, getDynamicUrl().getAddress());
if (StringUtils.isNotBlank(getDynamicUrl().getParam(PropertyKeyConst.USERNAME))) {
nacosConfig.put(PropertyKeyConst.USERNAME, getDynamicUrl().getParam(PropertyKeyConst.USERNAME));
}
}

protected NacosDynamicConfigManager(String appName, String remainUrl) {
super(appName, remainUrl);
group = appName;
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, getAddress());
Map params = getParams();
if (params != null) {
if( params.containsKey("username")) {
String username = (String)params.get("username");
nacosConfig.put(PropertyKeyConst.USERNAME, username);
}
if( params.containsKey("password")) {
String password = (String) params.get("password");
nacosConfig.put(PropertyKeyConst.PASSWORD, password);
}
if (StringUtils.isNotBlank(getDynamicUrl().getParam(PropertyKeyConst.PASSWORD))) {
nacosConfig.put(PropertyKeyConst.PASSWORD, getDynamicUrl().getParam(PropertyKeyConst.PASSWORD));
}
try {
configService = ConfigFactory.createConfigService(nacosConfig);

} catch (NacosException e) {
LOGGER.error("Failed to create ConfigService", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
import com.alipay.sofa.rpc.auth.AuthRuleGroup;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.dynamic.*;
import com.alipay.sofa.rpc.dynamic.ConfigChangeType;
import com.alipay.sofa.rpc.dynamic.ConfigChangedEvent;
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeyHelper;
import com.alipay.sofa.rpc.dynamic.DynamicConfigKeys;
import com.alipay.sofa.rpc.dynamic.DynamicConfigManager;
import com.alipay.sofa.rpc.dynamic.DynamicHelper;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.listener.ConfigListener;
import com.alipay.sofa.rpc.log.Logger;
Expand All @@ -35,6 +40,7 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;

import static com.alipay.sofa.common.config.SofaConfigs.getOrDefault;
import static com.alipay.sofa.rpc.common.utils.StringUtils.CONTEXT_SEP;

/**
Expand All @@ -55,55 +61,46 @@ public class ZookeeperDynamicConfigManager extends DynamicConfigManager {
private ConcurrentMap<String, String> configMap = new ConcurrentHashMap<>();

protected ZookeeperDynamicConfigManager(String appName) {
super(appName, SofaConfigs.getOrDefault(DynamicConfigKeys.ZK_ADDRESS));
super(appName, SofaConfigs.getOrCustomDefault(DynamicConfigKeys.CONFIG_CENTER_ADDRESS, "zookeeper://127.0.0.1:2181"));
rootPath = CONTEXT_SEP + DynamicConfigKeys.CONFIG_NODE + CONTEXT_SEP + appName;
zkClient = CuratorFrameworkFactory.builder()
.connectString(getAddress())
.connectString(getDynamicUrl().getAddress())
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.namespace(DynamicConfigKeys.DEFAULT_NAMESPACE)
.build();
zkClient.start();

PathChildrenCache cache = new PathChildrenCache(zkClient, rootPath, true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case CHILD_ADDED:
case CHILD_UPDATED:
String key = event.getData().getPath().substring(rootPath.length() + 1);
String value = new String(event.getData().getData());
configMap.put(key, value);
LOGGER.info("Receive zookeeper event: " + "type=[" + event.getType() + "] key=[" + key + "] value=[" + value + "]");
break;
case CHILD_REMOVED:
key = event.getData().getPath().substring(rootPath.length() + 1);
configMap.remove(key);
LOGGER.info("Receive zookeeper event: " + "type=[" + event.getType() + "] key=[" + key + "]");
break;
default:
break;
if (!getOrDefault(DynamicConfigKeys.DYNAMIC_REFRESH_ENABLE)) {
PathChildrenCache cache = new PathChildrenCache(zkClient, rootPath, true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()) {
case CHILD_ADDED:
case CHILD_UPDATED:
String key = event.getData().getPath().substring(rootPath.length() + 1);
String value = new String(event.getData().getData());
configMap.put(key, value);
LOGGER.info("Receive zookeeper event: " + "type=[" + event.getType() + "] key=[" + key + "] value=[" + value + "]");
break;
case CHILD_REMOVED:
key = event.getData().getPath().substring(rootPath.length() + 1);
configMap.remove(key);
LOGGER.info("Receive zookeeper event: " + "type=[" + event.getType() + "] key=[" + key + "]");
break;
default:
break;
}
}
});
try {
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
} catch (Exception e) {
LOGGER.error("setupPathChildrenCache error", e);
}
});
try {
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
} catch (Exception e) {
LOGGER.error("setupPathChildrenCache error", e);
}
}

protected ZookeeperDynamicConfigManager(String appName, String remainUrl) {
super(appName, remainUrl);
rootPath = CONTEXT_SEP + DynamicConfigKeys.CONFIG_NODE + CONTEXT_SEP + appName;
zkClient = CuratorFrameworkFactory.builder()
.connectString(getAddress())
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.namespace(DynamicConfigKeys.DEFAULT_NAMESPACE)
.build();
zkClient.start();
}

@Override
public void initServiceConfiguration(String service) {
// TODO 暂不支持
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,26 @@
* @version : DynamicConfigKeys.java, v 0.1 2019年04月17日 21:51 bystander Exp $
*/
public class DynamicConfigKeys {
public static final String DYNAMIC_ALIAS = "dynamicAlias";

public static final String CONFIG_NODE = "config";

public static final String DEFAULT_NAMESPACE = "sofa-rpc";

public static ConfigKey<String> CENTER_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.address",
" ",
false,
"The url of the dynamic configuration.",
new String[] { "sofa_rpc_config_center_address" });

public static ConfigKey<String> ZK_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.zookeeper.address",
"127.0.0.1:2181",
false,
"The address of Zookeeper configuration center.",
new String[] { "sofa_rpc_config_center_zookeeper_address" });

public static ConfigKey<String> NACOS_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.nacos.address",
"127.0.0.1:8848",
false,
"The address of Nacos configuration center.",
new String[] { "sofa_rpc_config_center_nacos_address" });
public static ConfigKey<String> APOLLO_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.apollo.address",
"127.0.0.1:8080",
false,
"The address of Apollo configuration center.",
new String[] { "sofa_rpc_config_center_apollo_address" });
public static final String DYNAMIC_ALIAS = "dynamicAlias";

public static final String CONFIG_NODE = "config";

public static final String DEFAULT_NAMESPACE = "sofa-rpc";

public static ConfigKey<String> CONFIG_CENTER_ADDRESS = ConfigKey
.build(
"sofa.rpc.config.center.address",
" ",
false,
"The url of the dynamic configuration.",
new String[] { "sofa_rpc_config_CONFIG_CENTER_ADDRESS" });

public static ConfigKey<Boolean> DYNAMIC_REFRESH_ENABLE = ConfigKey
.build(
"sofa.rpc.config.dynamic.refresh.enable",
false,
false,
"Switch for dynamic configuration refresh.",
new String[] { "sofa_rpc_config_dynamic_refresh_enable" });

}
Loading

0 comments on commit b8c60ae

Please sign in to comment.