Skip to content

Commit

Permalink
Basicly support dynamic config for extends. (#623)
Browse files Browse the repository at this point in the history
* support dynamic manager
* support layers
* support registe and use
* add type for consumer
* remove type for consumer
* dynamic config
* Support for getting configuration from the configuration center. (#585)
* refactor dynamic config
* support for getting configuration from the configuration center
* fix build method key bug
* fix test case
* use common key
* support dynamic manager
* support layers
* support registe and use
* add type for consumer
* remove type for consumer
* dynamic config
* Support for getting configuration from the configuration center. (#585)
* refactor dynamic config
* support for getting configuration from the configuration center
* resolve conflict
* fix build method key bug
* use common key
  • Loading branch information
zonghaishang committed Jul 1, 2019
1 parent 2d132a0 commit a97c632
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 1 deletion.
Expand Up @@ -32,6 +32,9 @@
import com.alipay.sofa.rpc.core.invoke.SofaResponseCallback;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
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.event.EventBus;
import com.alipay.sofa.rpc.event.ProviderInfoAddEvent;
import com.alipay.sofa.rpc.event.ProviderInfoRemoveEvent;
Expand Down Expand Up @@ -587,6 +590,24 @@ private SofaResponse buildEmptyResponse(SofaRequest request) {
* @return 调用超时
*/
private int resolveTimeout(SofaRequest request, ConsumerConfig consumerConfig, ProviderInfo providerInfo) {
// 动态配置优先
final String dynamicAlias = consumerConfig.getParameter(DynamicConfigKeys.DYNAMIC_ALIAS);
if (StringUtils.isNotBlank(dynamicAlias)) {
String dynamicTimeout = null;
DynamicConfigManager dynamicConfigManager = DynamicConfigManagerFactory.getDynamicManager(
consumerConfig.getAppName(),
dynamicAlias);

if (dynamicConfigManager != null) {
dynamicTimeout = dynamicConfigManager.getConsumerMethodProperty(request.getInterfaceName(),
request.getMethodName(),
"timeout");
}

if (StringUtils.isNotBlank(dynamicTimeout)) {
return Integer.parseInt(dynamicTimeout);
}
}
// 先去调用级别配置
Integer timeout = request.getTimeout();
if (timeout == null) {
Expand Down
60 changes: 60 additions & 0 deletions core/api/src/main/java/com/alipay/sofa/rpc/auth/AuthRule.java
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.auth;

import java.util.List;

/**
*
* @author lepdou
* @version $Id: AuthRule.java, v 0.1 2019年04月11日 下午7:53 lepdou Exp $
*/
public class AuthRule {

private String name;
private int enabled;

private List<AuthRuleItem> ruleItems;

public boolean enable() {
return enabled > 0;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getEnabled() {
return enabled;
}

public void setEnabled(int enabled) {
this.enabled = enabled;
}

public List<AuthRuleItem> getRuleItems() {
return ruleItems;
}

public void setRuleItems(List<AuthRuleItem> ruleItems) {
this.ruleItems = ruleItems;
}
}
69 changes: 69 additions & 0 deletions core/api/src/main/java/com/alipay/sofa/rpc/auth/AuthRuleGroup.java
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.auth;

import java.util.List;

/**
*
* @author lepdou
* @version $Id: AuthRuleGroup.java, v 0.1 2019年04月11日 下午7:53 lepdou Exp $
*/
public class AuthRuleGroup {

private String dataId;
private String type;
private int enabled;

private List<AuthRule> rules;

public boolean enable() {
return enabled > 0;
}

public String getDataId() {
return dataId;
}

public void setDataId(String dataId) {
this.dataId = dataId;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public int getEnabled() {
return enabled;
}

public void setEnabled(int enabled) {
this.enabled = enabled;
}

public List<AuthRule> getRules() {
return rules;
}

public void setRules(List<AuthRule> rules) {
this.rules = rules;
}
}
62 changes: 62 additions & 0 deletions core/api/src/main/java/com/alipay/sofa/rpc/auth/AuthRuleItem.java
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.auth;

/**
*
* @author lepdou
* @version $Id: AuthRuleItem.java, v 0.1 2019年04月11日 下午7:53 lepdou Exp $
*/
public class AuthRuleItem {

private String type;
private String field;
private String operation;
private String value;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

public String getOperation() {
return operation;
}

public void setOperation(String operation) {
this.operation = operation;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
@@ -0,0 +1,153 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.dynamic;

import com.alipay.sofa.rpc.common.utils.StringUtils;

/**
*
* @author lepdou
* @version $Id: DynamicConfigKeyHelper.java, v 0.1 2019年04月16日 下午12:01 lepdou Exp $
*/
public class DynamicConfigKeyHelper {

private static final String KEY_SEPARATOR = ".";

public static final String APP_CONSUMER_CONFIG_KEY_PREFIX = "sofa.consumer";
public static final String APP_PROVIDER_CONFIG_KEY_PREFIX = "sofa.provider";
public static final String SERVICE_CONSUMER_PROPERTY_KEY_PREFIX = "sofa.consumer.service";
public static final String SERVICE_PROVIDER_PROPERTY_KEY_PREFIX = "sofa.provider.service";
public static final String METHOD_CONSUMER_PROPERTY_KEY_PREFIX = "sofa.consumer.method";
public static final String METHOD_PROVIDER_PROPERTY_KEY_PREFIX = "sofa.provider.method";

/**
* The last field of key is actual property key
*/
public static String extractPropertyKey(String key) {
if (StringUtils.isBlank(key)) {
return "";
}
return key.substring(key.lastIndexOf(KEY_SEPARATOR) + 1);
}

/**
* Service property key format : sofa.consumer.service.{serivceName}.{configKey}
*
* For example : sofa.consumer.service.com.alipay.sofa.rpc.test.HelloService.timeout
* service name = com.alipay.sofa.rpc.test.HelloService
* config key = timeout
*
* @param serviceProKey service property key
* @return extracted service name
*/
public static String extractServiceNameFromServiceProKey(String serviceProKey) {
if (!isServiceProKey(serviceProKey)) {
return "";
}

return serviceProKey.substring(SERVICE_CONSUMER_PROPERTY_KEY_PREFIX.length() + 1,
serviceProKey.lastIndexOf(KEY_SEPARATOR));
}

/**
* Method property key format : sofa.consumer.method.{serivceName}.{methodName}.{configKey}
*
* Example : sofa.consumer.method.com.alipay.sofa.rpc.test.HelloService.sayHello.timeout
* service name = com.alipay.sofa.rpc.test.HelloService
* method name = sayHello
* config key = timeout
*
* @param methodProKey method property key
* @return extracted service name
*/
public static String extractServiceNameFromMethodProKey(String methodProKey) {
if (!isMethodProKey(methodProKey)) {
return "";
}

String serviceMethod = methodProKey.substring(METHOD_PROVIDER_PROPERTY_KEY_PREFIX.length() + 1,
methodProKey.lastIndexOf(KEY_SEPARATOR));

return serviceMethod.substring(0, serviceMethod.lastIndexOf(KEY_SEPARATOR));
}

/**
* Method property key format : sofa.consumer.method.{serivceName}.{methodName}.{configKey}
*
* Example : sofa.consumer.method.com.alipay.sofa.rpc.test.HelloService.sayHello.timeout
* service name = com.alipay.sofa.rpc.test.HelloService
* method name = sayHello
* config key = timeout
*
* @param methodProKey method property key
* @return extracted method name
*/
public static String extractMethodNameFromMethodProKey(String methodProKey) {
if (!isMethodProKey(methodProKey)) {
return "";
}

String serviceMethod = methodProKey.substring(METHOD_PROVIDER_PROPERTY_KEY_PREFIX.length() + 1,
methodProKey.lastIndexOf(KEY_SEPARATOR));

return serviceMethod.substring(serviceMethod.lastIndexOf(KEY_SEPARATOR) + 1);
}

/**
* Consumer service property key format : sofa.consumer.service.{serviceName}.{configKey}
*/
public static String buildConsumerServiceProKey(String serviceName, String proKey) {
return SERVICE_CONSUMER_PROPERTY_KEY_PREFIX + KEY_SEPARATOR + serviceName + KEY_SEPARATOR + proKey;
}

/**
* Provider service property key format : sofa.provider.service.{serviceName}.{configKey}
*/
public static String buildProviderServiceProKey(String serviceName, String proKey) {
return SERVICE_PROVIDER_PROPERTY_KEY_PREFIX + KEY_SEPARATOR + serviceName + KEY_SEPARATOR + proKey;
}

/**
* Consumer method property key format : sofa.consumer.service.{serviceName}.{methodName}.{configKey}
*/
public static String buildConsumerMethodProKey(String serviceName, String methodName, String proKey) {
return METHOD_CONSUMER_PROPERTY_KEY_PREFIX + KEY_SEPARATOR + serviceName + KEY_SEPARATOR + methodName +
KEY_SEPARATOR + proKey;
}

/**
* Provider method property key format : sofa.consumer.service.{serviceName}.{methodName}.{configKey}
*/
public static String buildProviderMethodProKey(String serviceName, String methodName, String proKey) {
return METHOD_PROVIDER_PROPERTY_KEY_PREFIX + KEY_SEPARATOR + serviceName + KEY_SEPARATOR + methodName +
KEY_SEPARATOR + proKey;
}

public static boolean isServiceProKey(String key) {
return !StringUtils.isBlank(key) && (key.startsWith(SERVICE_CONSUMER_PROPERTY_KEY_PREFIX) ||
key.startsWith(SERVICE_PROVIDER_PROPERTY_KEY_PREFIX));
}

public static boolean isMethodProKey(String key) {
return !StringUtils.isBlank(key) && (key.startsWith(METHOD_CONSUMER_PROPERTY_KEY_PREFIX) ||
key.startsWith(METHOD_PROVIDER_PROPERTY_KEY_PREFIX));
}

public static boolean isSofaProKey(String key) {
return isServiceProKey(key) || isMethodProKey(key);
}
}

0 comments on commit a97c632

Please sign in to comment.