Merge branch 'main' of https://github.com/cheese8/spring-cloud-tencent
commit
258b741b8c
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.adapter;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.config.enums.RefreshBehavior;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationPropertiesBean;
|
||||||
|
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||||
|
import org.springframework.cloud.context.properties.ConfigurationPropertiesBeans;
|
||||||
|
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.polaris.config.condition.NonDefaultBehaviorCondition.POLARIS_CONFIG_REFRESH_BEHAVIOR;
|
||||||
|
import static com.tencent.cloud.polaris.config.enums.RefreshBehavior.ALL_BEANS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend {@link ConfigurationPropertiesRebinder}.
|
||||||
|
* <p>
|
||||||
|
* Spring team doesn't seem to support single {@link ConfigurationPropertiesBean} refresh.
|
||||||
|
* <p>
|
||||||
|
* SmartConfigurationPropertiesRebinder can refresh specific
|
||||||
|
* {@link ConfigurationPropertiesBean} base on the change keys.
|
||||||
|
* <p>
|
||||||
|
* <strong> NOTE: We still use Spring's default behavior (full refresh) as default
|
||||||
|
* behavior, This feature can be considered an advanced feature, it may not be as stable
|
||||||
|
* as the default behavior. </strong>
|
||||||
|
* <code><a href=https://github.com/alibaba/spring-cloud-alibaba/blob/2.2.x/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/refresh/SmartConfigurationPropertiesRebinder.java>
|
||||||
|
* SmartConfigurationPropertiesRebinder</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class SmartConfigurationPropertiesRebinder extends ConfigurationPropertiesRebinder {
|
||||||
|
|
||||||
|
private static final String BEANS = "beans";
|
||||||
|
|
||||||
|
private Map<String, ConfigurationPropertiesBean> beanMap;
|
||||||
|
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private RefreshBehavior refreshBehavior;
|
||||||
|
|
||||||
|
public SmartConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) {
|
||||||
|
super(beans);
|
||||||
|
fillBeanMap(beans);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private void fillBeanMap(ConfigurationPropertiesBeans beans) {
|
||||||
|
this.beanMap = new HashMap<>();
|
||||||
|
Field field = ReflectionUtils.findField(beans.getClass(), BEANS);
|
||||||
|
if (field != null) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
this.beanMap.putAll((Map<String, ConfigurationPropertiesBean>) Optional
|
||||||
|
.ofNullable(ReflectionUtils.getField(field, beans))
|
||||||
|
.orElse(Collections.emptyMap()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext)
|
||||||
|
throws BeansException {
|
||||||
|
super.setApplicationContext(applicationContext);
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
this.refreshBehavior = this.applicationContext.getEnvironment().getProperty(
|
||||||
|
POLARIS_CONFIG_REFRESH_BEHAVIOR, RefreshBehavior.class,
|
||||||
|
ALL_BEANS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(EnvironmentChangeEvent event) {
|
||||||
|
if (this.applicationContext.equals(event.getSource())
|
||||||
|
// Backwards compatible
|
||||||
|
|| event.getKeys().equals(event.getSource())) {
|
||||||
|
switch (refreshBehavior) {
|
||||||
|
case SPECIFIC_BEAN:
|
||||||
|
rebindSpecificBean(event);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
rebind();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void rebindSpecificBean(EnvironmentChangeEvent event) {
|
||||||
|
Set<String> refreshedSet = new HashSet<>();
|
||||||
|
beanMap.forEach((name, bean) -> event.getKeys().forEach(changeKey -> {
|
||||||
|
String prefix = AnnotationUtils.getValue(bean.getAnnotation()).toString();
|
||||||
|
// prevent multiple refresh one ConfigurationPropertiesBean.
|
||||||
|
if (changeKey.startsWith(prefix) && refreshedSet.add(name)) {
|
||||||
|
rebind(name);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.condition;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Conditional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* custom annotation.
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-13
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Documented
|
||||||
|
@Conditional(NonDefaultBehaviorCondition.class)
|
||||||
|
public @interface ConditionalOnNonDefaultBehavior {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.condition;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.config.enums.RefreshBehavior;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||||
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extend SpringBootCondition.
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-13
|
||||||
|
*/
|
||||||
|
public class NonDefaultBehaviorCondition extends SpringBootCondition {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* refresh behavior config.
|
||||||
|
*/
|
||||||
|
public static final String POLARIS_CONFIG_REFRESH_BEHAVIOR = "spring.cloud.polaris.config.refresh-behavior";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* refresh behavior config default value.
|
||||||
|
*/
|
||||||
|
private static final RefreshBehavior DEFAULT_REFRESH_BEHAVIOR = RefreshBehavior.ALL_BEANS;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||||
|
AnnotatedTypeMetadata metadata) {
|
||||||
|
RefreshBehavior behavior = context.getEnvironment().getProperty(
|
||||||
|
POLARIS_CONFIG_REFRESH_BEHAVIOR, RefreshBehavior.class,
|
||||||
|
DEFAULT_REFRESH_BEHAVIOR);
|
||||||
|
if (DEFAULT_REFRESH_BEHAVIOR == behavior) {
|
||||||
|
return ConditionOutcome.noMatch("no matched");
|
||||||
|
}
|
||||||
|
return ConditionOutcome.match("matched");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.enums;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationPropertiesBean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh behavior.
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-13
|
||||||
|
*/
|
||||||
|
public enum RefreshBehavior {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh all {@link ConfigurationPropertiesBean}s.
|
||||||
|
*/
|
||||||
|
ALL_BEANS,
|
||||||
|
/**
|
||||||
|
* Refresh specific {@link ConfigurationPropertiesBean} base on change key.
|
||||||
|
*/
|
||||||
|
SPECIFIC_BEAN,
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
package com.tencent.cloud.polaris.config.spring.annotation;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.PriorityOrdered;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get spring bean properties and methods.
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrdered {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
Class clazz = bean.getClass();
|
||||||
|
for (Field field : findAllField(clazz)) {
|
||||||
|
processField(bean, beanName, field);
|
||||||
|
}
|
||||||
|
for (Method method : findAllMethod(clazz)) {
|
||||||
|
processMethod(bean, beanName, method);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* subclass should implement this method to process field.
|
||||||
|
* @param bean bean
|
||||||
|
* @param beanName beanName
|
||||||
|
* @param field field
|
||||||
|
*/
|
||||||
|
protected abstract void processField(Object bean, String beanName, Field field);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* subclass should implement this method to process method.
|
||||||
|
* @param bean bean
|
||||||
|
* @param beanName beanName
|
||||||
|
* @param method method
|
||||||
|
*/
|
||||||
|
protected abstract void processMethod(Object bean, String beanName, Method method);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
//make it as late as possible
|
||||||
|
return Ordered.LOWEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Field> findAllField(Class clazz) {
|
||||||
|
final List<Field> res = new LinkedList<>();
|
||||||
|
ReflectionUtils.doWithFields(clazz, field -> res.add(field));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Method> findAllMethod(Class clazz) {
|
||||||
|
final List<Method> res = new LinkedList<>();
|
||||||
|
ReflectionUtils.doWithMethods(clazz, method -> res.add(method));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,172 @@
|
|||||||
|
package com.tencent.cloud.polaris.config.spring.annotation;
|
||||||
|
|
||||||
|
import java.beans.PropertyDescriptor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Member;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.common.collect.LinkedListMultimap;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
|
||||||
|
import com.tencent.cloud.polaris.config.spring.property.SpringValue;
|
||||||
|
import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinition;
|
||||||
|
import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinitionProcessor;
|
||||||
|
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring value processor of field or method which has @Value and xml config placeholders.
|
||||||
|
* <br/>
|
||||||
|
* <br/>
|
||||||
|
* This source file was originally from:
|
||||||
|
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java>
|
||||||
|
* SpringValueProcessor</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class SpringValueProcessor extends PolarisProcessor implements BeanFactoryPostProcessor, BeanFactoryAware {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(SpringValueProcessor.class);
|
||||||
|
|
||||||
|
private final PolarisConfigProperties polarisConfigProperties;
|
||||||
|
private final PlaceholderHelper placeholderHelper;
|
||||||
|
private final SpringValueRegistry springValueRegistry;
|
||||||
|
|
||||||
|
private BeanFactory beanFactory;
|
||||||
|
private Multimap<String, SpringValueDefinition> beanName2SpringValueDefinitions;
|
||||||
|
|
||||||
|
public SpringValueProcessor(PlaceholderHelper placeholderHelper,
|
||||||
|
SpringValueRegistry springValueRegistry,
|
||||||
|
PolarisConfigProperties polarisConfigProperties) {
|
||||||
|
this.placeholderHelper = placeholderHelper;
|
||||||
|
this.polarisConfigProperties = polarisConfigProperties;
|
||||||
|
this.springValueRegistry = springValueRegistry;
|
||||||
|
beanName2SpringValueDefinitions = LinkedListMultimap.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
||||||
|
throws BeansException {
|
||||||
|
if (polarisConfigProperties.isAutoRefresh() && beanFactory instanceof BeanDefinitionRegistry) {
|
||||||
|
beanName2SpringValueDefinitions = SpringValueDefinitionProcessor
|
||||||
|
.getBeanName2SpringValueDefinitions((BeanDefinitionRegistry) beanFactory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
if (polarisConfigProperties.isAutoRefresh()) {
|
||||||
|
super.postProcessBeforeInitialization(bean, beanName);
|
||||||
|
processBeanPropertyValues(bean, beanName);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void processField(Object bean, String beanName, Field field) {
|
||||||
|
// register @Value on field
|
||||||
|
Value value = field.getAnnotation(Value.class);
|
||||||
|
if (value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doRegister(bean, beanName, field, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void processMethod(Object bean, String beanName, Method method) {
|
||||||
|
//register @Value on method
|
||||||
|
Value value = method.getAnnotation(Value.class);
|
||||||
|
if (value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//skip Configuration bean methods
|
||||||
|
if (method.getAnnotation(Bean.class) != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (method.getParameterTypes().length != 1) {
|
||||||
|
LOGGER.error("Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters",
|
||||||
|
bean.getClass().getName(), method.getName(), method.getParameterTypes().length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doRegister(bean, beanName, method, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doRegister(Object bean, String beanName, Member member, Value value) {
|
||||||
|
Set<String> keys = placeholderHelper.extractPlaceholderKeys(value.value());
|
||||||
|
if (keys.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String key : keys) {
|
||||||
|
SpringValue springValue;
|
||||||
|
if (member instanceof Field) {
|
||||||
|
Field field = (Field) member;
|
||||||
|
springValue = new SpringValue(key, value.value(), bean, beanName, field, false);
|
||||||
|
}
|
||||||
|
else if (member instanceof Method) {
|
||||||
|
Method method = (Method) member;
|
||||||
|
springValue = new SpringValue(key, value.value(), bean, beanName, method, false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LOGGER.error("Polaris @Value annotation currently only support to be used on methods and fields, "
|
||||||
|
+ "but is used on {}", member.getClass());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
springValueRegistry.register(beanFactory, key, springValue);
|
||||||
|
LOGGER.info("Monitoring {}", springValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processBeanPropertyValues(Object bean, String beanName) {
|
||||||
|
Collection<SpringValueDefinition> propertySpringValues = beanName2SpringValueDefinitions
|
||||||
|
.get(beanName);
|
||||||
|
if (propertySpringValues == null || propertySpringValues.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SpringValueDefinition definition : propertySpringValues) {
|
||||||
|
try {
|
||||||
|
PropertyDescriptor pd = BeanUtils
|
||||||
|
.getPropertyDescriptor(bean.getClass(), definition.getPropertyName());
|
||||||
|
Method method = pd.getWriteMethod();
|
||||||
|
if (method == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
SpringValue springValue = new SpringValue(definition.getKey(), definition.getPlaceholder(),
|
||||||
|
bean, beanName, method, false);
|
||||||
|
springValueRegistry.register(beanFactory, definition.getKey(), springValue);
|
||||||
|
LOGGER.debug("Monitoring {}", springValue);
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
|
LOGGER.error("Failed to enable auto update feature for {}.{}", bean.getClass(),
|
||||||
|
definition.getPropertyName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear
|
||||||
|
beanName2SpringValueDefinitions.removeAll(beanName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,195 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.spring.property;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.beans.factory.config.BeanExpressionContext;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
|
import org.springframework.beans.factory.config.Scope;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Placeholder helper functions.
|
||||||
|
* <br/>
|
||||||
|
* <br/>
|
||||||
|
* This source file was originally from:
|
||||||
|
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/PlaceholderHelper.java>
|
||||||
|
* PlaceholderHelper</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class PlaceholderHelper {
|
||||||
|
|
||||||
|
private static final String PLACEHOLDER_PREFIX = "${";
|
||||||
|
private static final String PLACEHOLDER_SUFFIX = "}";
|
||||||
|
private static final String VALUE_SEPARATOR = ":";
|
||||||
|
private static final String SIMPLE_PLACEHOLDER_PREFIX = "{";
|
||||||
|
private static final String EXPRESSION_PREFIX = "#{";
|
||||||
|
private static final String EXPRESSION_SUFFIX = "}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve placeholder property values, e.g.
|
||||||
|
* @param beanFactory beanFactory
|
||||||
|
* @param beanName beanName
|
||||||
|
* @param placeholder placeholder
|
||||||
|
* @return "${somePropertyValue}" -> "the actual property value"
|
||||||
|
*/
|
||||||
|
public Object resolvePropertyValue(ConfigurableBeanFactory beanFactory, String beanName, String placeholder) {
|
||||||
|
// resolve string value
|
||||||
|
String strVal = beanFactory.resolveEmbeddedValue(placeholder);
|
||||||
|
|
||||||
|
BeanDefinition bd = (beanFactory.containsBean(beanName) ? beanFactory
|
||||||
|
.getMergedBeanDefinition(beanName) : null);
|
||||||
|
|
||||||
|
// resolve expressions like "#{systemProperties.myProp}"
|
||||||
|
return evaluateBeanDefinitionString(beanFactory, strVal, bd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object evaluateBeanDefinitionString(ConfigurableBeanFactory beanFactory, String value,
|
||||||
|
BeanDefinition beanDefinition) {
|
||||||
|
if (beanFactory.getBeanExpressionResolver() == null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
Scope scope = (beanDefinition != null ? beanFactory
|
||||||
|
.getRegisteredScope(beanDefinition.getScope()) : null);
|
||||||
|
return beanFactory.getBeanExpressionResolver()
|
||||||
|
.evaluate(value, new BeanExpressionContext(beanFactory, scope));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param propertyString propertyString
|
||||||
|
* @return
|
||||||
|
* Extract keys from placeholder, e.g.
|
||||||
|
* <li>${some.key} => "some.key"</li>
|
||||||
|
* <li>${some.key:${some.other.key:100}} => "some.key", "some.other.key"</li>
|
||||||
|
* <li>${${some.key}} => "some.key"</li>
|
||||||
|
* <li>${${some.key:other.key}} => "some.key"</li>
|
||||||
|
* <li>${${some.key}:${another.key}} => "some.key", "another.key"</li>
|
||||||
|
* <li>#{new java.text.SimpleDateFormat('${some.key}').parse('${another.key}')} => "some.key", "another.key"</li>
|
||||||
|
*/
|
||||||
|
public Set<String> extractPlaceholderKeys(String propertyString) {
|
||||||
|
Set<String> placeholderKeys = Sets.newHashSet();
|
||||||
|
|
||||||
|
if (Strings.isNullOrEmpty(propertyString) || (!isNormalizedPlaceholder(propertyString) && !isExpressionWithPlaceholder(propertyString))) {
|
||||||
|
return placeholderKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stack<String> stack = new Stack<>();
|
||||||
|
stack.push(propertyString);
|
||||||
|
|
||||||
|
while (!stack.isEmpty()) {
|
||||||
|
String strVal = stack.pop();
|
||||||
|
int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
|
||||||
|
if (startIndex == -1) {
|
||||||
|
placeholderKeys.add(strVal);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int endIndex = findPlaceholderEndIndex(strVal, startIndex);
|
||||||
|
if (endIndex == -1) {
|
||||||
|
// invalid placeholder?
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String placeholderCandidate = strVal.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
|
||||||
|
|
||||||
|
// ${some.key:other.key}
|
||||||
|
if (placeholderCandidate.startsWith(PLACEHOLDER_PREFIX)) {
|
||||||
|
stack.push(placeholderCandidate);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// some.key:${some.other.key:100}
|
||||||
|
int separatorIndex = placeholderCandidate.indexOf(VALUE_SEPARATOR);
|
||||||
|
|
||||||
|
if (separatorIndex == -1) {
|
||||||
|
stack.push(placeholderCandidate);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
stack.push(placeholderCandidate.substring(0, separatorIndex));
|
||||||
|
String defaultValuePart =
|
||||||
|
normalizeToPlaceholder(placeholderCandidate.substring(separatorIndex + VALUE_SEPARATOR.length()));
|
||||||
|
if (!Strings.isNullOrEmpty(defaultValuePart)) {
|
||||||
|
stack.push(defaultValuePart);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// has remaining part, e.g. ${a}.${b}
|
||||||
|
if (endIndex + PLACEHOLDER_SUFFIX.length() < strVal.length() - 1) {
|
||||||
|
String remainingPart = normalizeToPlaceholder(strVal.substring(endIndex + PLACEHOLDER_SUFFIX.length()));
|
||||||
|
if (!Strings.isNullOrEmpty(remainingPart)) {
|
||||||
|
stack.push(remainingPart);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return placeholderKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNormalizedPlaceholder(String propertyString) {
|
||||||
|
return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isExpressionWithPlaceholder(String propertyString) {
|
||||||
|
return propertyString.startsWith(EXPRESSION_PREFIX) && propertyString.contains(EXPRESSION_SUFFIX)
|
||||||
|
&& propertyString.contains(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalizeToPlaceholder(String strVal) {
|
||||||
|
int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX);
|
||||||
|
if (startIndex == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int endIndex = strVal.lastIndexOf(PLACEHOLDER_SUFFIX);
|
||||||
|
if (endIndex == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strVal.substring(startIndex, endIndex + PLACEHOLDER_SUFFIX.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
|
||||||
|
int index = startIndex + PLACEHOLDER_PREFIX.length();
|
||||||
|
int withinNestedPlaceholder = 0;
|
||||||
|
while (index < buf.length()) {
|
||||||
|
if (StringUtils.substringMatch(buf, index, PLACEHOLDER_SUFFIX)) {
|
||||||
|
if (withinNestedPlaceholder > 0) {
|
||||||
|
withinNestedPlaceholder--;
|
||||||
|
index = index + PLACEHOLDER_SUFFIX.length();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (StringUtils.substringMatch(buf, index, SIMPLE_PLACEHOLDER_PREFIX)) {
|
||||||
|
withinNestedPlaceholder++;
|
||||||
|
index = index + SIMPLE_PLACEHOLDER_PREFIX.length();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,156 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.spring.property;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring @Value method info.
|
||||||
|
* <br/>
|
||||||
|
* <br/>
|
||||||
|
* This source file was originally from:
|
||||||
|
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValue.java>
|
||||||
|
* SpringValue</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class SpringValue {
|
||||||
|
|
||||||
|
private MethodParameter methodParameter;
|
||||||
|
private Field field;
|
||||||
|
private final WeakReference<Object> beanRef;
|
||||||
|
private final String beanName;
|
||||||
|
private final String key;
|
||||||
|
private final String placeholder;
|
||||||
|
private final Class<?> targetType;
|
||||||
|
private Type genericType;
|
||||||
|
private final boolean isJson;
|
||||||
|
|
||||||
|
public SpringValue(String key, String placeholder, Object bean, String beanName, Field field, boolean isJson) {
|
||||||
|
this.beanRef = new WeakReference<>(bean);
|
||||||
|
this.beanName = beanName;
|
||||||
|
this.field = field;
|
||||||
|
this.key = key;
|
||||||
|
this.placeholder = placeholder;
|
||||||
|
this.targetType = field.getType();
|
||||||
|
this.isJson = isJson;
|
||||||
|
if (isJson) {
|
||||||
|
this.genericType = field.getGenericType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpringValue(String key, String placeholder, Object bean, String beanName, Method method, boolean isJson) {
|
||||||
|
this.beanRef = new WeakReference<>(bean);
|
||||||
|
this.beanName = beanName;
|
||||||
|
this.methodParameter = new MethodParameter(method, 0);
|
||||||
|
this.key = key;
|
||||||
|
this.placeholder = placeholder;
|
||||||
|
Class<?>[] paramTps = method.getParameterTypes();
|
||||||
|
this.targetType = paramTps[0];
|
||||||
|
this.isJson = isJson;
|
||||||
|
if (isJson) {
|
||||||
|
this.genericType = method.getGenericParameterTypes()[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(Object newVal) throws IllegalAccessException, InvocationTargetException {
|
||||||
|
if (isField()) {
|
||||||
|
injectField(newVal);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
injectMethod(newVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void injectField(Object newVal) throws IllegalAccessException {
|
||||||
|
Object bean = beanRef.get();
|
||||||
|
if (bean == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean accessible = field.isAccessible();
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(bean, newVal);
|
||||||
|
field.setAccessible(accessible);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void injectMethod(Object newVal)
|
||||||
|
throws InvocationTargetException, IllegalAccessException {
|
||||||
|
Object bean = beanRef.get();
|
||||||
|
if (bean == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
methodParameter.getMethod().invoke(bean, newVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBeanName() {
|
||||||
|
return beanName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> getTargetType() {
|
||||||
|
return targetType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlaceholder() {
|
||||||
|
return this.placeholder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MethodParameter getMethodParameter() {
|
||||||
|
return methodParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isField() {
|
||||||
|
return this.field != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Field getField() {
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type getGenericType() {
|
||||||
|
return genericType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isJson() {
|
||||||
|
return isJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isTargetBeanValid() {
|
||||||
|
return beanRef.get() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
Object bean = beanRef.get();
|
||||||
|
if (bean == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (isField()) {
|
||||||
|
return String
|
||||||
|
.format("key: %s, beanName: %s, field: %s.%s", key, beanName, bean.getClass()
|
||||||
|
.getName(), field.getName());
|
||||||
|
}
|
||||||
|
return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(),
|
||||||
|
methodParameter.getMethod().getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.spring.property;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring value.
|
||||||
|
* <br/>
|
||||||
|
* <br/>
|
||||||
|
* This source file was originally from:
|
||||||
|
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueDefinition.java>
|
||||||
|
* SpringValueDefinition</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SpringValueDefinition {
|
||||||
|
|
||||||
|
private final String key;
|
||||||
|
private final String placeholder;
|
||||||
|
private final String propertyName;
|
||||||
|
|
||||||
|
public SpringValueDefinition(String key, String placeholder, String propertyName) {
|
||||||
|
this.key = key;
|
||||||
|
this.placeholder = placeholder;
|
||||||
|
this.propertyName = propertyName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlaceholder() {
|
||||||
|
return placeholder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPropertyName() {
|
||||||
|
return propertyName;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,125 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.spring.property;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.google.common.collect.LinkedListMultimap;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
|
import org.springframework.beans.PropertyValue;
|
||||||
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
import org.springframework.beans.factory.config.TypedStringValue;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To process xml config placeholders, e.g.
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* <bean class="com.demo.bean.XmlBean">
|
||||||
|
* <property name="timeout" value="${timeout:200}"/>
|
||||||
|
* <property name="batch" value="${batch:100}"/>
|
||||||
|
* </bean>
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* This source file was originally from:
|
||||||
|
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueDefinitionProcessor.java>
|
||||||
|
* SpringValueDefinitionProcessor</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPostProcessor {
|
||||||
|
private static final Map<BeanDefinitionRegistry, Multimap<String, SpringValueDefinition>> beanName2SpringValueDefinitions =
|
||||||
|
Maps.newConcurrentMap();
|
||||||
|
private static final Set<BeanDefinitionRegistry> PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet();
|
||||||
|
|
||||||
|
private final PlaceholderHelper placeholderHelper;
|
||||||
|
|
||||||
|
private PolarisConfigProperties polarisConfigProperties;
|
||||||
|
|
||||||
|
public SpringValueDefinitionProcessor(PlaceholderHelper placeholderHelper, PolarisConfigProperties polarisConfigProperties) {
|
||||||
|
this.polarisConfigProperties = polarisConfigProperties;
|
||||||
|
this.placeholderHelper = placeholderHelper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
|
||||||
|
if (polarisConfigProperties.isAutoRefresh()) {
|
||||||
|
processPropertyValues(registry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Multimap<String, SpringValueDefinition> getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) {
|
||||||
|
Multimap<String, SpringValueDefinition> springValueDefinitions = beanName2SpringValueDefinitions.get(registry);
|
||||||
|
if (springValueDefinitions == null) {
|
||||||
|
springValueDefinitions = LinkedListMultimap.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
return springValueDefinitions;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processPropertyValues(BeanDefinitionRegistry beanRegistry) {
|
||||||
|
if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) {
|
||||||
|
// already initialized
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!beanName2SpringValueDefinitions.containsKey(beanRegistry)) {
|
||||||
|
beanName2SpringValueDefinitions.put(beanRegistry, LinkedListMultimap.create());
|
||||||
|
}
|
||||||
|
|
||||||
|
Multimap<String, SpringValueDefinition> springValueDefinitions = beanName2SpringValueDefinitions.get(beanRegistry);
|
||||||
|
|
||||||
|
String[] beanNames = beanRegistry.getBeanDefinitionNames();
|
||||||
|
for (String beanName : beanNames) {
|
||||||
|
BeanDefinition beanDefinition = beanRegistry.getBeanDefinition(beanName);
|
||||||
|
MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();
|
||||||
|
List<PropertyValue> propertyValues = mutablePropertyValues.getPropertyValueList();
|
||||||
|
for (PropertyValue propertyValue : propertyValues) {
|
||||||
|
Object value = propertyValue.getValue();
|
||||||
|
if (!(value instanceof TypedStringValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String placeholder = ((TypedStringValue) value).getValue();
|
||||||
|
Set<String> keys = placeholderHelper.extractPlaceholderKeys(placeholder);
|
||||||
|
|
||||||
|
if (keys.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String key : keys) {
|
||||||
|
springValueDefinitions.put(beanName, new SpringValueDefinition(key, placeholder, propertyValue.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.spring.property;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import com.google.common.collect.LinkedListMultimap;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
import com.google.common.collect.Multimaps;
|
||||||
|
import com.tencent.polaris.client.util.NamedThreadFactory;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spring value auto registry.
|
||||||
|
* <br/>
|
||||||
|
* <br/>
|
||||||
|
* This source file was originally from:
|
||||||
|
* <code><a href=https://github.com/apolloconfig/apollo/blob/master/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java>
|
||||||
|
* SpringValueRegistry</a></code>
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class SpringValueRegistry {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SpringValueRegistry.class);
|
||||||
|
|
||||||
|
private static final long CLEAN_INTERVAL_IN_SECONDS = 5;
|
||||||
|
private final Map<BeanFactory, Multimap<String, SpringValue>> registry = Maps.newConcurrentMap();
|
||||||
|
private final AtomicBoolean initialized = new AtomicBoolean(false);
|
||||||
|
private final Object LOCK = new Object();
|
||||||
|
|
||||||
|
public void register(BeanFactory beanFactory, String key, SpringValue springValue) {
|
||||||
|
if (!registry.containsKey(beanFactory)) {
|
||||||
|
synchronized (LOCK) {
|
||||||
|
if (!registry.containsKey(beanFactory)) {
|
||||||
|
registry.put(beanFactory, Multimaps.synchronizedListMultimap(LinkedListMultimap.create()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
registry.get(beanFactory).put(key, springValue);
|
||||||
|
|
||||||
|
// lazy initialize
|
||||||
|
if (initialized.compareAndSet(false, true)) {
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<SpringValue> get(BeanFactory beanFactory, String key) {
|
||||||
|
Multimap<String, SpringValue> beanFactorySpringValues = registry.get(beanFactory);
|
||||||
|
if (beanFactorySpringValues == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return beanFactorySpringValues.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize() {
|
||||||
|
Executors.newSingleThreadScheduledExecutor(
|
||||||
|
new NamedThreadFactory("polaris-spring-value-registry")).scheduleAtFixedRate(
|
||||||
|
() -> {
|
||||||
|
try {
|
||||||
|
scanAndClean();
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
|
logger.error(ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}, CLEAN_INTERVAL_IN_SECONDS, CLEAN_INTERVAL_IN_SECONDS, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scanAndClean() {
|
||||||
|
Iterator<Multimap<String, SpringValue>> iterator = registry.values().iterator();
|
||||||
|
while (!Thread.currentThread().isInterrupted() && iterator.hasNext()) {
|
||||||
|
Multimap<String, SpringValue> springValues = iterator.next();
|
||||||
|
Iterator<Map.Entry<String, SpringValue>> springValueIterator = springValues.entries().iterator();
|
||||||
|
while (springValueIterator.hasNext()) {
|
||||||
|
Map.Entry<String, SpringValue> springValue = springValueIterator.next();
|
||||||
|
if (!springValue.getValue().isTargetBeanValid()) {
|
||||||
|
// clear unused spring values
|
||||||
|
springValueIterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.tencent.cloud.polaris.config.adapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock config kv file for test.
|
||||||
|
*
|
||||||
|
* @author weihubeats 2022-7-10
|
||||||
|
*/
|
||||||
|
public class MockedConfigChange {
|
||||||
|
|
||||||
|
private String k1;
|
||||||
|
|
||||||
|
String getK1() {
|
||||||
|
return k1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setK1(String k1) {
|
||||||
|
this.k1 = k1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.router.interceptor;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
|
||||||
|
import com.tencent.polaris.plugins.router.metadata.MetadataRouter;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router request interceptor for metadata router.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class MetadataRouterRequestInterceptor implements RouterRequestInterceptor {
|
||||||
|
private static final String LABEL_KEY_METADATA_ROUTER_KEYS = "system-metadata-router-keys";
|
||||||
|
|
||||||
|
private final PolarisMetadataRouterProperties polarisMetadataRouterProperties;
|
||||||
|
|
||||||
|
public MetadataRouterRequestInterceptor(PolarisMetadataRouterProperties polarisMetadataRouterProperties) {
|
||||||
|
this.polarisMetadataRouterProperties = polarisMetadataRouterProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext) {
|
||||||
|
if (!polarisMetadataRouterProperties.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. get metadata router label keys
|
||||||
|
Set<String> metadataRouterKeys = routerContext.getLabelAsSet(LABEL_KEY_METADATA_ROUTER_KEYS);
|
||||||
|
// 2. get metadata router labels
|
||||||
|
Map<String, String> metadataRouterLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS,
|
||||||
|
metadataRouterKeys);
|
||||||
|
// 3. set metadata router labels to request
|
||||||
|
request.addRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, metadataRouterLabels);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.router.interceptor;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
|
||||||
|
import com.tencent.polaris.plugins.router.nearby.NearbyRouter;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router request interceptor for nearby router.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class NearbyRouterRequestInterceptor implements RouterRequestInterceptor {
|
||||||
|
|
||||||
|
private final PolarisNearByRouterProperties polarisNearByRouterProperties;
|
||||||
|
|
||||||
|
public NearbyRouterRequestInterceptor(PolarisNearByRouterProperties polarisNearByRouterProperties) {
|
||||||
|
this.polarisNearByRouterProperties = polarisNearByRouterProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext) {
|
||||||
|
if (!polarisNearByRouterProperties.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> nearbyRouterMetadata = new HashMap<>();
|
||||||
|
nearbyRouterMetadata.put(NearbyRouter.ROUTER_ENABLED, "true");
|
||||||
|
|
||||||
|
request.addRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY, nearbyRouterMetadata);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.router.interceptor;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
|
||||||
|
import com.tencent.polaris.plugins.router.rule.RuleBasedRouter;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router request interceptor for rule based router.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class RuleBasedRouterRequestInterceptor implements RouterRequestInterceptor {
|
||||||
|
|
||||||
|
private final PolarisRuleBasedRouterProperties polarisRuleBasedRouterProperties;
|
||||||
|
|
||||||
|
public RuleBasedRouterRequestInterceptor(PolarisRuleBasedRouterProperties polarisRuleBasedRouterProperties) {
|
||||||
|
this.polarisRuleBasedRouterProperties = polarisRuleBasedRouterProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext) {
|
||||||
|
boolean ruleBasedRouterEnabled = polarisRuleBasedRouterProperties.isEnabled();
|
||||||
|
|
||||||
|
// set dynamic switch for rule based router
|
||||||
|
Map<String, String> metadata = new HashMap<>();
|
||||||
|
metadata.put(RuleBasedRouter.ROUTER_ENABLED, String.valueOf(ruleBasedRouterEnabled));
|
||||||
|
request.addRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED, metadata);
|
||||||
|
|
||||||
|
// The label information that the rule based routing depends on
|
||||||
|
// is placed in the metadata of the source service for transmission.
|
||||||
|
// Later, can consider putting it in routerMetadata like other routers.
|
||||||
|
if (ruleBasedRouterEnabled) {
|
||||||
|
Map<String, String> ruleRouterLabels = routerContext.getLabels(PolarisRouterContext.ROUTER_LABELS);
|
||||||
|
request.getSourceService().setMetadata(ruleRouterLabels);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.router.spi;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interceptor for router request. Router plugin can modify request by interceptor.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public interface RouterRequestInterceptor {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* processing request.
|
||||||
|
* @param request the router request.
|
||||||
|
* @param routerContext the router context.
|
||||||
|
*/
|
||||||
|
void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext);
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.router.spi;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interceptor for router response. Router plugin can modify router response by interceptor.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public interface RouterResponseInterceptor {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* processing router response.
|
||||||
|
*
|
||||||
|
* @param response the router response.
|
||||||
|
* @param routerContext the router context.
|
||||||
|
*/
|
||||||
|
void apply(ProcessRoutersResponse response, PolarisRouterContext routerContext);
|
||||||
|
}
|
@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.common.metadata.filter.gateway;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
||||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
|
|
||||||
import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Scg output first filter used for setting peer info in context.
|
|
||||||
*
|
|
||||||
* @author Haotian Zhang
|
|
||||||
*/
|
|
||||||
public class MetadataFirstScgFilter implements GlobalFilter, Ordered {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Order of MetadataFirstScgFilter.
|
|
||||||
*/
|
|
||||||
public static final int METADATA_FIRST_FILTER_ORDER = ROUTE_TO_URL_FILTER_ORDER + 1;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOrder() {
|
|
||||||
return METADATA_FIRST_FILTER_ORDER;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
||||||
// get metadata of current thread
|
|
||||||
MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
|
|
||||||
if (metadataContext == null) {
|
|
||||||
metadataContext = MetadataContextHolder.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
exchange.getAttributes().put(MetadataConstant.HeaderName.METADATA_CONTEXT, metadataContext);
|
|
||||||
|
|
||||||
return chain.filter(exchange);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.rule;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Condition expression.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class Condition {
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
private String operation;
|
||||||
|
private List<String> values;
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getValues() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValues(List<String> values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOperation() {
|
||||||
|
return operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperation(String operation) {
|
||||||
|
this.operation = operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Condition{" +
|
||||||
|
"key='" + key + '\'' +
|
||||||
|
", values='" + values + '\'' +
|
||||||
|
", operation='" + operation + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.rule;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The util for condition expression.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public final class ConditionUtils {
|
||||||
|
|
||||||
|
private ConditionUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean match(Map<String, String> actualValues, List<Condition> conditions) {
|
||||||
|
boolean allMatched = true;
|
||||||
|
for (Condition condition : conditions) {
|
||||||
|
List<String> expectedValues = condition.getValues();
|
||||||
|
String operation = condition.getOperation();
|
||||||
|
String key = condition.getKey();
|
||||||
|
String actualValue = actualValues.get(key);
|
||||||
|
|
||||||
|
if (!Operation.match(expectedValues, actualValue, operation)) {
|
||||||
|
allMatched = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return allMatched;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/value pair.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class KVPair {
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "KVPair{" +
|
||||||
|
"key='" + key + '\'' +
|
||||||
|
", value='" + value + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.rule;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The util for key/value pair.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public final class KVPairUtils {
|
||||||
|
|
||||||
|
private KVPairUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> toMap(List<KVPair> labels) {
|
||||||
|
if (CollectionUtils.isEmpty(labels)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
labels.forEach(label -> {
|
||||||
|
result.put(label.getKey(), label.getValue());
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.rule;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The condition operation.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public enum Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* case sensitive string equals.
|
||||||
|
*/
|
||||||
|
EQUAL("EQUAL"),
|
||||||
|
/**
|
||||||
|
* case sensitive string not equals.
|
||||||
|
*/
|
||||||
|
NOT_EQUAL("NOT_EQUAL"),
|
||||||
|
/**
|
||||||
|
* whether element in collection.
|
||||||
|
*/
|
||||||
|
IN("IN"),
|
||||||
|
/**
|
||||||
|
* whether element not in collection.
|
||||||
|
*/
|
||||||
|
NOT_IN("NOT_IN"),
|
||||||
|
/**
|
||||||
|
* regex operation.
|
||||||
|
*/
|
||||||
|
REGEX("REGEX"),
|
||||||
|
/**
|
||||||
|
* whether element is blank.
|
||||||
|
*/
|
||||||
|
BLANK("BLANK"),
|
||||||
|
/**
|
||||||
|
* whether element is not blank.
|
||||||
|
*/
|
||||||
|
NOT_BLANK("NOT_BLANK");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
Operation(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean match(List<String> expectedValues, String actualValue, String rawOperation) {
|
||||||
|
String firstExpectedValue = null;
|
||||||
|
if (!CollectionUtils.isEmpty(expectedValues)) {
|
||||||
|
firstExpectedValue = expectedValues.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (getOperation(rawOperation)) {
|
||||||
|
case EQUAL:
|
||||||
|
return firstExpectedValue != null && StringUtils.equals(actualValue, firstExpectedValue);
|
||||||
|
case NOT_EQUAL:
|
||||||
|
return firstExpectedValue == null || !StringUtils.equals(actualValue, firstExpectedValue);
|
||||||
|
case BLANK:
|
||||||
|
return StringUtils.isBlank(actualValue);
|
||||||
|
case NOT_BLANK:
|
||||||
|
return !StringUtils.isBlank(actualValue);
|
||||||
|
case IN:
|
||||||
|
if (CollectionUtils.isEmpty(expectedValues)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return expectedValues.contains(actualValue);
|
||||||
|
case NOT_IN:
|
||||||
|
if (CollectionUtils.isEmpty(expectedValues)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return !expectedValues.contains(actualValue);
|
||||||
|
case REGEX:
|
||||||
|
if (firstExpectedValue == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Pattern r = Pattern.compile(firstExpectedValue);
|
||||||
|
return r.matcher(actualValue).matches();
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Operation getOperation(String operation) {
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, EQUAL.value)) {
|
||||||
|
return EQUAL;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, NOT_EQUAL.value)) {
|
||||||
|
return NOT_EQUAL;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, IN.value)) {
|
||||||
|
return IN;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, NOT_IN.value)) {
|
||||||
|
return NOT_IN;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, REGEX.value)) {
|
||||||
|
return REGEX;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, BLANK.value)) {
|
||||||
|
return BLANK;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(operation, NOT_BLANK.value)) {
|
||||||
|
return NOT_BLANK;
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unsupported operation. operation = " + operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
@ -1,319 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.common.util;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpCookie;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpRequest;
|
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the utils for parse label expression.
|
|
||||||
*
|
|
||||||
* @author lepdou 2022-05-13
|
|
||||||
* @author cheese8 2022-06-20
|
|
||||||
*/
|
|
||||||
public final class ExpressionLabelUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the expression prefix of header label.
|
|
||||||
*/
|
|
||||||
public static final String LABEL_HEADER_PREFIX = "${http.header.";
|
|
||||||
/**
|
|
||||||
* the length of expression header label prefix.
|
|
||||||
*/
|
|
||||||
public static final int LABEL_HEADER_PREFIX_LEN = LABEL_HEADER_PREFIX.length();
|
|
||||||
/**
|
|
||||||
* the expression prefix of query.
|
|
||||||
*/
|
|
||||||
public static final String LABEL_QUERY_PREFIX = "${http.query.";
|
|
||||||
/**
|
|
||||||
* the length of expression query label prefix.
|
|
||||||
*/
|
|
||||||
public static final int LABEL_QUERY_PREFIX_LEN = LABEL_QUERY_PREFIX.length();
|
|
||||||
/**
|
|
||||||
* the expression prefix of cookie.
|
|
||||||
*/
|
|
||||||
public static final String LABEL_COOKIE_PREFIX = "${http.cookie.";
|
|
||||||
/**
|
|
||||||
* the length of expression cookie label prefix.
|
|
||||||
*/
|
|
||||||
public static final int LABEL_COOKIE_PREFIX_LEN = LABEL_COOKIE_PREFIX.length();
|
|
||||||
/**
|
|
||||||
* the expression of method.
|
|
||||||
*/
|
|
||||||
public static final String LABEL_METHOD = "${http.method}";
|
|
||||||
/**
|
|
||||||
* the expression of uri.
|
|
||||||
*/
|
|
||||||
public static final String LABEL_URI = "${http.uri}";
|
|
||||||
/**
|
|
||||||
* the suffix of expression.
|
|
||||||
*/
|
|
||||||
public static final String LABEL_SUFFIX = "}";
|
|
||||||
private ExpressionLabelUtils() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isExpressionLabel(String labelKey) {
|
|
||||||
if (StringUtils.isEmpty(labelKey)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (StringUtils.equalsIgnoreCase(LABEL_METHOD, labelKey) ||
|
|
||||||
StringUtils.startsWithIgnoreCase(LABEL_URI, labelKey)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return (StringUtils.startsWithIgnoreCase(labelKey, LABEL_HEADER_PREFIX) ||
|
|
||||||
StringUtils.startsWithIgnoreCase(labelKey, LABEL_QUERY_PREFIX) ||
|
|
||||||
StringUtils.startsWithIgnoreCase(labelKey, LABEL_COOKIE_PREFIX))
|
|
||||||
&& StringUtils.endsWith(labelKey, LABEL_SUFFIX);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<String, String> resolve(HttpServletRequest request, Set<String> labelKeys) {
|
|
||||||
if (CollectionUtils.isEmpty(labelKeys)) {
|
|
||||||
return Collections.emptyMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> labels = new HashMap<>();
|
|
||||||
|
|
||||||
for (String labelKey : labelKeys) {
|
|
||||||
if (!isExpressionLabel(labelKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_HEADER_PREFIX)) {
|
|
||||||
String headerKey = parseHeaderKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(headerKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, request.getHeader(headerKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_QUERY_PREFIX)) {
|
|
||||||
String queryKey = parseQueryKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(queryKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getQueryValue(request.getQueryString(), queryKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_COOKIE_PREFIX)) {
|
|
||||||
String cookieKey = parseCookieKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(cookieKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getCookieValue(request.getCookies(), cookieKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.equalsIgnoreCase(LABEL_METHOD, labelKey)) {
|
|
||||||
labels.put(labelKey, request.getMethod());
|
|
||||||
}
|
|
||||||
else if (StringUtils.equalsIgnoreCase(LABEL_URI, labelKey)) {
|
|
||||||
labels.put(labelKey, request.getRequestURI());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return labels;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<String, String> resolve(ServerWebExchange exchange, Set<String> labelKeys) {
|
|
||||||
if (CollectionUtils.isEmpty(labelKeys)) {
|
|
||||||
return Collections.emptyMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> labels = new HashMap<>();
|
|
||||||
|
|
||||||
for (String labelKey : labelKeys) {
|
|
||||||
if (!isExpressionLabel(labelKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_HEADER_PREFIX)) {
|
|
||||||
String headerKey = parseHeaderKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(headerKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getHeaderValue(exchange.getRequest(), headerKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_QUERY_PREFIX)) {
|
|
||||||
String queryKey = parseQueryKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(queryKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getQueryValue(exchange.getRequest(), queryKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_COOKIE_PREFIX)) {
|
|
||||||
String cookieKey = parseCookieKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(cookieKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getCookieValue(exchange.getRequest(), cookieKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.equalsIgnoreCase(LABEL_METHOD, labelKey)) {
|
|
||||||
labels.put(labelKey, exchange.getRequest().getMethodValue());
|
|
||||||
}
|
|
||||||
else if (StringUtils.equalsIgnoreCase(LABEL_URI, labelKey)) {
|
|
||||||
labels.put(labelKey, exchange.getRequest().getURI().getPath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return labels;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<String, String> resolve(HttpRequest request, Set<String> labelKeys) {
|
|
||||||
if (CollectionUtils.isEmpty(labelKeys)) {
|
|
||||||
return Collections.emptyMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> labels = new HashMap<>();
|
|
||||||
|
|
||||||
for (String labelKey : labelKeys) {
|
|
||||||
if (!isExpressionLabel(labelKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_HEADER_PREFIX)) {
|
|
||||||
String headerKey = parseHeaderKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(headerKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getHeaderValue(request, headerKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.startsWithIgnoreCase(labelKey, LABEL_QUERY_PREFIX)) {
|
|
||||||
String queryKey = parseQueryKey(labelKey);
|
|
||||||
if (StringUtils.isBlank(queryKey)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
labels.put(labelKey, getQueryValue(request, queryKey));
|
|
||||||
}
|
|
||||||
else if (StringUtils.equalsIgnoreCase(LABEL_METHOD, labelKey)) {
|
|
||||||
labels.put(labelKey, request.getMethodValue());
|
|
||||||
}
|
|
||||||
else if (StringUtils.equalsIgnoreCase(LABEL_URI, labelKey)) {
|
|
||||||
labels.put(labelKey, request.getURI().getPath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return labels;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parseHeaderKey(String expression) {
|
|
||||||
return expression.substring(LABEL_HEADER_PREFIX_LEN, expression.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parseQueryKey(String expression) {
|
|
||||||
return expression.substring(LABEL_QUERY_PREFIX_LEN, expression.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String parseCookieKey(String expression) {
|
|
||||||
return expression.substring(LABEL_COOKIE_PREFIX_LEN, expression.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getQueryValue(String queryString, String queryKey) {
|
|
||||||
if (StringUtils.isBlank(queryString)) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
String[] queries = StringUtils.split(queryString, "&");
|
|
||||||
if (queries == null || queries.length == 0) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
for (String query : queries) {
|
|
||||||
String[] queryKV = StringUtils.split(query, "=");
|
|
||||||
if (queryKV != null && queryKV.length == 2 && StringUtils.equals(queryKV[0], queryKey)) {
|
|
||||||
return queryKV[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getCookieValue(Cookie[] cookies, String key) {
|
|
||||||
if (cookies == null || cookies.length == 0) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
for (Cookie cookie : cookies) {
|
|
||||||
if (StringUtils.equals(cookie.getName(), key)) {
|
|
||||||
return cookie.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getHeaderValue(ServerHttpRequest request, String key) {
|
|
||||||
String value = request.getHeaders().getFirst(key);
|
|
||||||
if (value == null) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getQueryValue(ServerHttpRequest request, String key) {
|
|
||||||
MultiValueMap<String, String> queries = request.getQueryParams();
|
|
||||||
if (CollectionUtils.isEmpty(queries)) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
String value = queries.getFirst(key);
|
|
||||||
if (value == null) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getCookieValue(ServerHttpRequest request, String key) {
|
|
||||||
HttpCookie cookie = request.getCookies().getFirst(key);
|
|
||||||
if (cookie == null) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
return cookie.getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getHeaderValue(HttpRequest request, String key) {
|
|
||||||
HttpHeaders headers = request.getHeaders();
|
|
||||||
return headers.getFirst(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getQueryValue(HttpRequest request, String key) {
|
|
||||||
String query = request.getURI().getQuery();
|
|
||||||
return getQueryValue(query, key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFirstValue(Map<String, Collection<String>> valueMaps, String key) {
|
|
||||||
if (CollectionUtils.isEmpty(valueMaps)) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
Collection<String> values = valueMaps.get(key);
|
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(values)) {
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String value : values) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return StringUtils.EMPTY;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,136 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.util.expresstion;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the utils for parse label expression.
|
||||||
|
*
|
||||||
|
* @author lepdou 2022-05-13
|
||||||
|
* @author cheese8 2022-06-20
|
||||||
|
*/
|
||||||
|
public final class ExpressionLabelUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the expression prefix of header label.
|
||||||
|
*/
|
||||||
|
public static final String LABEL_HEADER_PREFIX = "${http.header.";
|
||||||
|
/**
|
||||||
|
* the length of expression header label prefix.
|
||||||
|
*/
|
||||||
|
public static final int LABEL_HEADER_PREFIX_LEN = LABEL_HEADER_PREFIX.length();
|
||||||
|
/**
|
||||||
|
* the expression prefix of query.
|
||||||
|
*/
|
||||||
|
public static final String LABEL_QUERY_PREFIX = "${http.query.";
|
||||||
|
/**
|
||||||
|
* the length of expression query label prefix.
|
||||||
|
*/
|
||||||
|
public static final int LABEL_QUERY_PREFIX_LEN = LABEL_QUERY_PREFIX.length();
|
||||||
|
/**
|
||||||
|
* the expression prefix of cookie.
|
||||||
|
*/
|
||||||
|
public static final String LABEL_COOKIE_PREFIX = "${http.cookie.";
|
||||||
|
/**
|
||||||
|
* the length of expression cookie label prefix.
|
||||||
|
*/
|
||||||
|
public static final int LABEL_COOKIE_PREFIX_LEN = LABEL_COOKIE_PREFIX.length();
|
||||||
|
/**
|
||||||
|
* the expression of method.
|
||||||
|
*/
|
||||||
|
public static final String LABEL_METHOD = "${http.method}";
|
||||||
|
/**
|
||||||
|
* the expression of uri.
|
||||||
|
*/
|
||||||
|
public static final String LABEL_URI = "${http.uri}";
|
||||||
|
/**
|
||||||
|
* the suffix of expression.
|
||||||
|
*/
|
||||||
|
public static final String LABEL_SUFFIX = "}";
|
||||||
|
|
||||||
|
private ExpressionLabelUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isExpressionLabel(String labelKey) {
|
||||||
|
if (StringUtils.isEmpty(labelKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (StringUtils.equalsIgnoreCase(LABEL_METHOD, labelKey) ||
|
||||||
|
StringUtils.startsWithIgnoreCase(LABEL_URI, labelKey)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return (StringUtils.startsWithIgnoreCase(labelKey, LABEL_HEADER_PREFIX) ||
|
||||||
|
StringUtils.startsWithIgnoreCase(labelKey, LABEL_QUERY_PREFIX) ||
|
||||||
|
StringUtils.startsWithIgnoreCase(labelKey, LABEL_COOKIE_PREFIX))
|
||||||
|
&& StringUtils.endsWith(labelKey, LABEL_SUFFIX);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parseHeaderKey(String expression) {
|
||||||
|
return expression.substring(LABEL_HEADER_PREFIX_LEN, expression.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parseQueryKey(String expression) {
|
||||||
|
return expression.substring(LABEL_QUERY_PREFIX_LEN, expression.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parseCookieKey(String expression) {
|
||||||
|
return expression.substring(LABEL_COOKIE_PREFIX_LEN, expression.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getQueryValue(String queryString, String queryKey) {
|
||||||
|
if (StringUtils.isBlank(queryString)) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
String[] queries = StringUtils.split(queryString, "&");
|
||||||
|
if (queries == null || queries.length == 0) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
for (String query : queries) {
|
||||||
|
String[] queryKV = StringUtils.split(query, "=");
|
||||||
|
if (queryKV != null && queryKV.length == 2 && StringUtils.equals(queryKV[0], queryKey)) {
|
||||||
|
return queryKV[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getFirstValue(Map<String, Collection<String>> valueMaps, String key) {
|
||||||
|
if (CollectionUtils.isEmpty(valueMaps)) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<String> values = valueMaps.get(key);
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(values)) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String value : values) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.util.expresstion;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse labels from HttpServletRequest.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public final class ServletExpressionLabelUtils {
|
||||||
|
|
||||||
|
private ServletExpressionLabelUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> resolve(HttpServletRequest request, Set<String> labelKeys) {
|
||||||
|
if (CollectionUtils.isEmpty(labelKeys)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
|
||||||
|
for (String labelKey : labelKeys) {
|
||||||
|
if (!ExpressionLabelUtils.isExpressionLabel(labelKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_HEADER_PREFIX)) {
|
||||||
|
String headerKey = ExpressionLabelUtils.parseHeaderKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(headerKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, request.getHeader(headerKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_QUERY_PREFIX)) {
|
||||||
|
String queryKey = ExpressionLabelUtils.parseQueryKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(queryKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, ExpressionLabelUtils.getQueryValue(request.getQueryString(), queryKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_COOKIE_PREFIX)) {
|
||||||
|
String cookieKey = ExpressionLabelUtils.parseCookieKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(cookieKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, getCookieValue(request.getCookies(), cookieKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.equalsIgnoreCase(ExpressionLabelUtils.LABEL_METHOD, labelKey)) {
|
||||||
|
labels.put(labelKey, request.getMethod());
|
||||||
|
}
|
||||||
|
else if (StringUtils.equalsIgnoreCase(ExpressionLabelUtils.LABEL_URI, labelKey)) {
|
||||||
|
labels.put(labelKey, request.getRequestURI());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCookieValue(Cookie[] cookies, String key) {
|
||||||
|
if (cookies == null || cookies.length == 0) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
for (Cookie cookie : cookies) {
|
||||||
|
if (StringUtils.equals(cookie.getName(), key)) {
|
||||||
|
return cookie.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,161 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.util.expresstion;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpCookie;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse labels from ServerWebExchange and HttpRequest.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public final class SpringWebExpressionLabelUtils {
|
||||||
|
|
||||||
|
private SpringWebExpressionLabelUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> resolve(ServerWebExchange exchange, Set<String> labelKeys) {
|
||||||
|
if (CollectionUtils.isEmpty(labelKeys)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
|
||||||
|
for (String labelKey : labelKeys) {
|
||||||
|
if (!ExpressionLabelUtils.isExpressionLabel(labelKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_HEADER_PREFIX)) {
|
||||||
|
String headerKey = ExpressionLabelUtils.parseHeaderKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(headerKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, getHeaderValue(exchange.getRequest(), headerKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_QUERY_PREFIX)) {
|
||||||
|
String queryKey = ExpressionLabelUtils.parseQueryKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(queryKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, getQueryValue(exchange.getRequest(), queryKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_COOKIE_PREFIX)) {
|
||||||
|
String cookieKey = ExpressionLabelUtils.parseCookieKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(cookieKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, getCookieValue(exchange.getRequest(), cookieKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.equalsIgnoreCase(ExpressionLabelUtils.LABEL_METHOD, labelKey)) {
|
||||||
|
labels.put(labelKey, exchange.getRequest().getMethodValue());
|
||||||
|
}
|
||||||
|
else if (StringUtils.equalsIgnoreCase(ExpressionLabelUtils.LABEL_URI, labelKey)) {
|
||||||
|
labels.put(labelKey, exchange.getRequest().getURI().getPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> resolve(HttpRequest request, Set<String> labelKeys) {
|
||||||
|
if (CollectionUtils.isEmpty(labelKeys)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
|
||||||
|
for (String labelKey : labelKeys) {
|
||||||
|
if (!ExpressionLabelUtils.isExpressionLabel(labelKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_HEADER_PREFIX)) {
|
||||||
|
String headerKey = ExpressionLabelUtils.parseHeaderKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(headerKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, getHeaderValue(request, headerKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.startsWithIgnoreCase(labelKey, ExpressionLabelUtils.LABEL_QUERY_PREFIX)) {
|
||||||
|
String queryKey = ExpressionLabelUtils.parseQueryKey(labelKey);
|
||||||
|
if (StringUtils.isBlank(queryKey)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
labels.put(labelKey, getQueryValue(request, queryKey));
|
||||||
|
}
|
||||||
|
else if (StringUtils.equalsIgnoreCase(ExpressionLabelUtils.LABEL_METHOD, labelKey)) {
|
||||||
|
labels.put(labelKey, request.getMethodValue());
|
||||||
|
}
|
||||||
|
else if (StringUtils.equalsIgnoreCase(ExpressionLabelUtils.LABEL_URI, labelKey)) {
|
||||||
|
labels.put(labelKey, request.getURI().getPath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getHeaderValue(ServerHttpRequest request, String key) {
|
||||||
|
String value = request.getHeaders().getFirst(key);
|
||||||
|
if (value == null) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getQueryValue(ServerHttpRequest request, String key) {
|
||||||
|
MultiValueMap<String, String> queries = request.getQueryParams();
|
||||||
|
if (CollectionUtils.isEmpty(queries)) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
String value = queries.getFirst(key);
|
||||||
|
if (value == null) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCookieValue(ServerHttpRequest request, String key) {
|
||||||
|
HttpCookie cookie = request.getCookies().getFirst(key);
|
||||||
|
if (cookie == null) {
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
return cookie.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getHeaderValue(HttpRequest request, String key) {
|
||||||
|
HttpHeaders headers = request.getHeaders();
|
||||||
|
return headers.getFirst(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getQueryValue(HttpRequest request, String key) {
|
||||||
|
String query = request.getURI().getQuery();
|
||||||
|
return ExpressionLabelUtils.getQueryValue(query, key);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.common.rule;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link Operation}.
|
||||||
|
* @author lepdou 2022-07-12
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class OperationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEqual() {
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), "v1", Operation.EQUAL.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "v2", Operation.EQUAL.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList(""), "v2", Operation.EQUAL.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "", Operation.EQUAL.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), null, Operation.EQUAL.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.emptyList(), "v1", Operation.EQUAL.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotEqual() {
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "v1", Operation.NOT_EQUAL.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), "v2", Operation.NOT_EQUAL.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList(""), "v2", Operation.NOT_EQUAL.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), "", Operation.NOT_EQUAL.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), null, Operation.NOT_EQUAL.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.emptyList(), "v1", Operation.NOT_EQUAL.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIn() {
|
||||||
|
Assert.assertTrue(Operation.match(Arrays.asList("v1", "v2", "v3"), "v1", Operation.IN.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Arrays.asList("v1", "v2", "v3"), "v2", Operation.IN.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Arrays.asList("v1", "v2", "v3"), "v4", Operation.IN.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Arrays.asList("v1", "v2", "v3"), "", Operation.IN.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Arrays.asList("v1", "v2", "v3"), null, Operation.IN.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.emptyList(), null, Operation.IN.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotIn() {
|
||||||
|
Assert.assertFalse(Operation.match(Arrays.asList("v1", "v2", "v3"), "v1", Operation.NOT_IN.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Arrays.asList("v1", "v2", "v3"), "v2", Operation.NOT_IN.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Arrays.asList("v1", "v2", "v3"), "v4", Operation.NOT_IN.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Arrays.asList("v1", "v2", "v3"), "", Operation.NOT_IN.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Arrays.asList("v1", "v2", "v3"), null, Operation.NOT_IN.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.emptyList(), null, Operation.NOT_IN.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmpty() {
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), null, Operation.BLANK.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), "", Operation.BLANK.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.emptyList(), null, Operation.BLANK.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotEmpty() {
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), null, Operation.NOT_BLANK.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "", Operation.NOT_BLANK.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.emptyList(), null, Operation.NOT_BLANK.getValue()));
|
||||||
|
Assert.assertTrue(Operation.match(Collections.emptyList(), "v1", Operation.NOT_BLANK.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegex() {
|
||||||
|
Assert.assertTrue(Operation.match(Collections.singletonList("v[1~10]"), "v1", Operation.REGEX.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v[1~10]"), "v12", Operation.REGEX.getValue()));
|
||||||
|
Assert.assertFalse(Operation.match(Collections.singletonList("v[1~10]*"), "v12", Operation.REGEX.getValue()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-tencent</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>Spring Cloud Starter Tencent Solution</name>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>spring-cloud-tencent-featureenv-plugin</module>
|
||||||
|
<module>spring-cloud-tencent-gateway-plugin</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-cloud-tencent-featureenv-plugin</artifactId>
|
||||||
|
<name>Spring Cloud Tencent Feature Environment Plugin</name>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.featureenv;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto configuration for feature env.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.tencent.plugin.router.feature-env.enabled", matchIfMissing = true)
|
||||||
|
public class FeatureEnvAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FeatureEnvProperties featureEnvProperties() {
|
||||||
|
return new FeatureEnvProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FeatureEnvRouterRequestInterceptor featureEnvRouterRequestInterceptor() {
|
||||||
|
return new FeatureEnvRouterRequestInterceptor();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.featureenv;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The properties for feature env.
|
||||||
|
* @author lepdou 2022-07-12
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.cloud.tencent.plugin.router.feature-env")
|
||||||
|
public class FeatureEnvProperties {
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.featureenv;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor;
|
||||||
|
import com.tencent.polaris.api.rpc.MetadataFailoverType;
|
||||||
|
import com.tencent.polaris.plugins.router.metadata.MetadataRouter;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build metadata router context for feature env scene.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class FeatureEnvRouterRequestInterceptor implements RouterRequestInterceptor {
|
||||||
|
|
||||||
|
private static final String LABEL_KEY_FEATURE_ENV_ROUTER_KEY = "system-feature-env-router-label";
|
||||||
|
private static final String DEFAULT_FEATURE_ENV_ROUTER_LABEL = "env";
|
||||||
|
private static final String NOT_EXISTED_ENV = "NOT_EXISTED_ENV";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext) {
|
||||||
|
//1. get feature env router label key
|
||||||
|
String envLabelKey = routerContext.getLabel(LABEL_KEY_FEATURE_ENV_ROUTER_KEY);
|
||||||
|
if (StringUtils.isBlank(envLabelKey)) {
|
||||||
|
envLabelKey = DEFAULT_FEATURE_ENV_ROUTER_LABEL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//2. get feature env router label value
|
||||||
|
String envLabelValue = routerContext.getLabel(envLabelKey);
|
||||||
|
if (envLabelValue == null) {
|
||||||
|
// router to base env when not matched feature env
|
||||||
|
envLabelValue = NOT_EXISTED_ENV;
|
||||||
|
}
|
||||||
|
|
||||||
|
//3. set env metadata to router request
|
||||||
|
Map<String, String> envMetadata = new HashMap<>();
|
||||||
|
envMetadata.put(envLabelKey, envLabelValue);
|
||||||
|
|
||||||
|
request.addRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, envMetadata);
|
||||||
|
|
||||||
|
//4. set failover type to others
|
||||||
|
request.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.router.feature-env.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": true,
|
||||||
|
"description": "the switch for feature env plugin."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.tencent.cloud.plugin.featureenv.FeatureEnvAutoConfiguration
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.featureenv;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||||
|
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.plugins.router.metadata.MetadataRouter;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link FeatureEnvRouterRequestInterceptor}.
|
||||||
|
* @author lepdou 2022-07-12
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class FeatureEnvRouterRequestInterceptorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultRouterKey() {
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
labels.put("env", "blue");
|
||||||
|
PolarisRouterContext routerContext = new PolarisRouterContext();
|
||||||
|
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
|
||||||
|
|
||||||
|
ProcessRoutersRequest request = new ProcessRoutersRequest();
|
||||||
|
ServiceInstances serviceInstances = new DefaultServiceInstances(Mockito.mock(ServiceKey.class), new ArrayList<>());
|
||||||
|
request.setDstInstances(serviceInstances);
|
||||||
|
|
||||||
|
FeatureEnvRouterRequestInterceptor interceptor = new FeatureEnvRouterRequestInterceptor();
|
||||||
|
|
||||||
|
interceptor.apply(request, routerContext);
|
||||||
|
|
||||||
|
Map<String, String> metadataRouterLabels = request.getRouterMetadata().get(MetadataRouter.ROUTER_TYPE_METADATA);
|
||||||
|
Assert.assertEquals(1, metadataRouterLabels.size());
|
||||||
|
Assert.assertEquals("blue", metadataRouterLabels.get("env"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSpecifyRouterKey() {
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
labels.put("system-feature-env-router-label", "specify-env");
|
||||||
|
labels.put("specify-env", "blue");
|
||||||
|
PolarisRouterContext routerContext = new PolarisRouterContext();
|
||||||
|
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
|
||||||
|
|
||||||
|
ProcessRoutersRequest request = new ProcessRoutersRequest();
|
||||||
|
ServiceInstances serviceInstances = new DefaultServiceInstances(Mockito.mock(ServiceKey.class), new ArrayList<>());
|
||||||
|
request.setDstInstances(serviceInstances);
|
||||||
|
|
||||||
|
FeatureEnvRouterRequestInterceptor interceptor = new FeatureEnvRouterRequestInterceptor();
|
||||||
|
|
||||||
|
interceptor.apply(request, routerContext);
|
||||||
|
|
||||||
|
Map<String, String> metadataRouterLabels = request.getRouterMetadata().get(MetadataRouter.ROUTER_TYPE_METADATA);
|
||||||
|
Assert.assertEquals(1, metadataRouterLabels.size());
|
||||||
|
Assert.assertEquals("blue", metadataRouterLabels.get("specify-env"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotExistedEnvLabel() {
|
||||||
|
Map<String, String> labels = new HashMap<>();
|
||||||
|
labels.put("system-feature-env-router-label", "specify-env");
|
||||||
|
PolarisRouterContext routerContext = new PolarisRouterContext();
|
||||||
|
routerContext.putLabels(PolarisRouterContext.ROUTER_LABELS, labels);
|
||||||
|
|
||||||
|
ProcessRoutersRequest request = new ProcessRoutersRequest();
|
||||||
|
ServiceInstances serviceInstances = new DefaultServiceInstances(Mockito.mock(ServiceKey.class), new ArrayList<>());
|
||||||
|
request.setDstInstances(serviceInstances);
|
||||||
|
|
||||||
|
FeatureEnvRouterRequestInterceptor interceptor = new FeatureEnvRouterRequestInterceptor();
|
||||||
|
|
||||||
|
interceptor.apply(request, routerContext);
|
||||||
|
|
||||||
|
Map<String, String> metadataRouterLabels = request.getRouterMetadata().get(MetadataRouter.ROUTER_TYPE_METADATA);
|
||||||
|
Assert.assertEquals(1, metadataRouterLabels.size());
|
||||||
|
Assert.assertEquals("NOT_EXISTED_ENV", metadataRouterLabels.get("specify-env"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId>
|
||||||
|
<name>Spring Cloud Tencent Gateway Plugin</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-gateway-server</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.StainingProperties;
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.TrafficStainer;
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.TrafficStainingGatewayFilter;
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.rule.RuleStainingExecutor;
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.rule.RuleStainingProperties;
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.rule.RuleTrafficStainer;
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.rule.StainingRuleManager;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto configuration for spring cloud gateway plugins.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.tencent.plugin.scg.enabled", matchIfMissing = true)
|
||||||
|
public class SCGPluginsAutoConfiguration {
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty("spring.cloud.tencent.plugin.scg.staining.enabled")
|
||||||
|
public static class StainingPluginConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public StainingProperties stainingProperties() {
|
||||||
|
return new StainingProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.tencent.plugin.scg.staining.rule-staining.enabled", matchIfMissing = true)
|
||||||
|
public static class RuleStainingPluginConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RuleStainingProperties ruleStainingProperties() {
|
||||||
|
return new RuleStainingProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public StainingRuleManager stainingRuleManager(RuleStainingProperties stainingProperties, ConfigFileService configFileService) {
|
||||||
|
return new StainingRuleManager(stainingProperties, configFileService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TrafficStainingGatewayFilter trafficStainingGatewayFilter(List<TrafficStainer> trafficStainer) {
|
||||||
|
return new TrafficStainingGatewayFilter(trafficStainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RuleStainingExecutor ruleStainingExecutor() {
|
||||||
|
return new RuleStainingExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RuleTrafficStainer ruleTrafficStainer(StainingRuleManager stainingRuleManager,
|
||||||
|
RuleStainingExecutor ruleStainingExecutor) {
|
||||||
|
return new RuleTrafficStainer(stainingRuleManager, ruleStainingExecutor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The properties for traffic staining.
|
||||||
|
* @author lepdou 2022-07-07
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.cloud.tencent.plugin.scg.staining")
|
||||||
|
public class StainingProperties {
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Staining according to request parameters. for example, when the request parameter uid=0, staining env=blue.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public interface TrafficStainer extends Ordered {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get stained labels from request.
|
||||||
|
* @param exchange the request.
|
||||||
|
* @return stained labels.
|
||||||
|
*/
|
||||||
|
Map<String, String> apply(ServerWebExchange exchange);
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Staining the request, and the stained labels will be passed to the link through transitive metadata.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class TrafficStainingGatewayFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
|
private final List<TrafficStainer> trafficStainers;
|
||||||
|
|
||||||
|
public TrafficStainingGatewayFilter(List<TrafficStainer> trafficStainers) {
|
||||||
|
if (!CollectionUtils.isEmpty(trafficStainers)) {
|
||||||
|
trafficStainers.sort(Comparator.comparingInt(Ordered::getOrder));
|
||||||
|
}
|
||||||
|
this.trafficStainers = trafficStainers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
if (CollectionUtils.isEmpty(trafficStainers)) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. get stained labels from request
|
||||||
|
Map<String, String> stainedLabels = getStainedLabels(exchange);
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(stainedLabels)) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. put stained labels to metadata context
|
||||||
|
ServerHttpRequest request = exchange.getRequest().mutate().headers((httpHeaders) -> {
|
||||||
|
MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
|
||||||
|
if (metadataContext == null) {
|
||||||
|
metadataContext = MetadataContextHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> oldTransitiveMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||||
|
|
||||||
|
// append new transitive metadata
|
||||||
|
Map<String, String> newTransitiveMetadata = new HashMap<>(oldTransitiveMetadata);
|
||||||
|
newTransitiveMetadata.putAll(stainedLabels);
|
||||||
|
|
||||||
|
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE, newTransitiveMetadata);
|
||||||
|
}).build();
|
||||||
|
|
||||||
|
return chain.filter(exchange.mutate().request(request).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> getStainedLabels(ServerWebExchange exchange) {
|
||||||
|
Map<String, String> stainedLabels = new HashMap<>();
|
||||||
|
int size = trafficStainers.size();
|
||||||
|
for (int i = size - 1; i >= 0; i--) {
|
||||||
|
TrafficStainer stainer = trafficStainers.get(i);
|
||||||
|
Map<String, String> labels = stainer.apply(exchange);
|
||||||
|
if (!CollectionUtils.isEmpty(labels)) {
|
||||||
|
stainedLabels.putAll(labels);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stainedLabels;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return ROUTE_TO_URL_FILTER_ORDER + 1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.rule.Condition;
|
||||||
|
import com.tencent.cloud.common.rule.ConditionUtils;
|
||||||
|
import com.tencent.cloud.common.rule.KVPairUtils;
|
||||||
|
import com.tencent.cloud.common.util.expresstion.SpringWebExpressionLabelUtils;
|
||||||
|
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve labels from request by staining rule.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
public class RuleStainingExecutor {
|
||||||
|
|
||||||
|
Map<String, String> execute(ServerWebExchange exchange, StainingRule stainingRule) {
|
||||||
|
if (stainingRule == null) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<StainingRule.Rule> rules = stainingRule.getRules();
|
||||||
|
if (CollectionUtils.isEmpty(rules)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> parsedLabels = new HashMap<>();
|
||||||
|
|
||||||
|
for (StainingRule.Rule rule : rules) {
|
||||||
|
List<Condition> conditions = rule.getConditions();
|
||||||
|
|
||||||
|
Set<String> keys = new HashSet<>();
|
||||||
|
conditions.forEach(condition -> keys.add(condition.getKey()));
|
||||||
|
Map<String, String> actualValues = SpringWebExpressionLabelUtils.resolve(exchange, keys);
|
||||||
|
|
||||||
|
if (!ConditionUtils.match(actualValues, conditions)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedLabels.putAll(KVPairUtils.toMap(rule.getLabels()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsedLabels;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The properties for rule staining.
|
||||||
|
* @author lepdou 2022-07-11
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("spring.cloud.tencent.plugin.scg.staining.rule-staining")
|
||||||
|
public class RuleStainingProperties {
|
||||||
|
|
||||||
|
@Value("${spring.cloud.tencent.plugin.scg.staining.rule-staining.namespace:${spring.cloud.tencent.namespace:default}}")
|
||||||
|
private String namespace;
|
||||||
|
|
||||||
|
@Value("${spring.cloud.tencent.plugin.scg.staining.rule-staining.group:${spring.application.name:spring-cloud-gateway}}")
|
||||||
|
private String group;
|
||||||
|
|
||||||
|
@Value("${spring.cloud.tencent.plugin.scg.staining.rule-staining.fileName:rule/staining.json}")
|
||||||
|
private String fileName;
|
||||||
|
|
||||||
|
private boolean enabled = true;
|
||||||
|
|
||||||
|
public String getNamespace() {
|
||||||
|
return namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNamespace(String namespace) {
|
||||||
|
this.namespace = namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGroup() {
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGroup(String group) {
|
||||||
|
this.group = group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFileName() {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFileName(String fileName) {
|
||||||
|
this.fileName = fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.plugin.gateway.staining.TrafficStainer;
|
||||||
|
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Staining the request by staining rules.
|
||||||
|
* @author lepdou 2022-07-06
|
||||||
|
*/
|
||||||
|
public class RuleTrafficStainer implements TrafficStainer {
|
||||||
|
|
||||||
|
private final StainingRuleManager stainingRuleManager;
|
||||||
|
private final RuleStainingExecutor ruleStainingExecutor;
|
||||||
|
|
||||||
|
public RuleTrafficStainer(StainingRuleManager stainingRuleManager, RuleStainingExecutor ruleStainingExecutor) {
|
||||||
|
this.stainingRuleManager = stainingRuleManager;
|
||||||
|
this.ruleStainingExecutor = ruleStainingExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> apply(ServerWebExchange exchange) {
|
||||||
|
StainingRule stainingRule = stainingRuleManager.getStainingRule();
|
||||||
|
|
||||||
|
if (stainingRule == null) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ruleStainingExecutor.execute(exchange, stainingRule);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.rule.Condition;
|
||||||
|
import com.tencent.cloud.common.rule.KVPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The rules for staining.
|
||||||
|
* @author lepdou 2022-07-07
|
||||||
|
*/
|
||||||
|
public class StainingRule {
|
||||||
|
|
||||||
|
private List<Rule> rules;
|
||||||
|
|
||||||
|
public List<Rule> getRules() {
|
||||||
|
return rules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRules(List<Rule> rules) {
|
||||||
|
this.rules = rules;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "StainingRule{" +
|
||||||
|
"rules=" + rules +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Rule {
|
||||||
|
private List<Condition> conditions;
|
||||||
|
private List<KVPair> labels;
|
||||||
|
|
||||||
|
public List<Condition> getConditions() {
|
||||||
|
return conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConditions(List<Condition> conditions) {
|
||||||
|
this.conditions = conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<KVPair> getLabels() {
|
||||||
|
return labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLabels(List<KVPair> labels) {
|
||||||
|
this.labels = labels;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Rule{" +
|
||||||
|
"conditions=" + conditions +
|
||||||
|
", labels=" + labels +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.util.JacksonUtils;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFile;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch staining rule from polaris, and deserialize to {@link StainingRule}.
|
||||||
|
* @author lepdou 2022-07-07
|
||||||
|
*/
|
||||||
|
public class StainingRuleManager {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(StainingRuleManager.class);
|
||||||
|
|
||||||
|
private final RuleStainingProperties stainingProperties;
|
||||||
|
private final ConfigFileService configFileService;
|
||||||
|
|
||||||
|
private StainingRule stainingRule;
|
||||||
|
|
||||||
|
public StainingRuleManager(RuleStainingProperties stainingProperties, ConfigFileService configFileService) {
|
||||||
|
this.stainingProperties = stainingProperties;
|
||||||
|
this.configFileService = configFileService;
|
||||||
|
|
||||||
|
initStainingRule();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initStainingRule() {
|
||||||
|
ConfigFile rulesFile = configFileService.getConfigFile(stainingProperties.getNamespace(), stainingProperties.getGroup(),
|
||||||
|
stainingProperties.getFileName());
|
||||||
|
|
||||||
|
rulesFile.addChangeListener(event -> {
|
||||||
|
LOGGER.info("[SCT] update scg staining rules. {}", event);
|
||||||
|
deserialize(event.getNewValue());
|
||||||
|
});
|
||||||
|
|
||||||
|
String ruleJson = rulesFile.getContent();
|
||||||
|
LOGGER.info("[SCT] init scg staining rules. {}", ruleJson);
|
||||||
|
|
||||||
|
deserialize(ruleJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deserialize(String ruleJsonStr) {
|
||||||
|
if (StringUtils.isBlank(ruleJsonStr)) {
|
||||||
|
stainingRule = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
stainingRule = JacksonUtils.deserialize(ruleJsonStr, StainingRule.class);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOGGER.error("[SCT] deserialize staining rule error.", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public StainingRule getStainingRule() {
|
||||||
|
return stainingRule;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.scg.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": true,
|
||||||
|
"description": "the switch for spring cloud gateway plugin."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.scg.staining.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": true,
|
||||||
|
"description": "the switch for spring cloud gateway staining plugin."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.scg.staining.rule-staining.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"defaultValue": true,
|
||||||
|
"description": "the switch for spring cloud gateway rule staining plugin."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.scg.staining.rule-staining.namespace",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "default",
|
||||||
|
"description": "The namespace used to config staining rules."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.scg.staining.rule-staining.group",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "${spring.application.name}",
|
||||||
|
"description": "The group used to config staining rules."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.tencent.plugin.scg.staining.rule-staining.fileName",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "rule/staining.json",
|
||||||
|
"description": "The file name used to config staining rules."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.tencent.cloud.plugin.gateway.SCGPluginsAutoConfiguration
|
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link TrafficStainingGatewayFilter}.
|
||||||
|
* @author lepdou 2022-07-12
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class TrafficStainerGatewayFilterTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private GatewayFilterChain chain;
|
||||||
|
@Mock
|
||||||
|
private ServerWebExchange exchange;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoneTrafficStainingImplement() {
|
||||||
|
TrafficStainingGatewayFilter filter = new TrafficStainingGatewayFilter(null);
|
||||||
|
|
||||||
|
when(chain.filter(exchange)).thenReturn(Mono.empty());
|
||||||
|
|
||||||
|
filter.filter(exchange, chain);
|
||||||
|
|
||||||
|
verify(chain).filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiStaining() {
|
||||||
|
TrafficStainer trafficStainer1 = Mockito.mock(TrafficStainer.class);
|
||||||
|
TrafficStainer trafficStainer2 = Mockito.mock(TrafficStainer.class);
|
||||||
|
|
||||||
|
when(trafficStainer1.getOrder()).thenReturn(1);
|
||||||
|
when(trafficStainer2.getOrder()).thenReturn(2);
|
||||||
|
|
||||||
|
Map<String, String> labels1 = new HashMap<>();
|
||||||
|
labels1.put("k1", "v1");
|
||||||
|
labels1.put("k2", "v2");
|
||||||
|
when(trafficStainer1.apply(exchange)).thenReturn(labels1);
|
||||||
|
|
||||||
|
Map<String, String> labels2 = new HashMap<>();
|
||||||
|
labels2.put("k1", "v11");
|
||||||
|
labels2.put("k3", "v3");
|
||||||
|
when(trafficStainer2.apply(exchange)).thenReturn(labels2);
|
||||||
|
|
||||||
|
TrafficStainingGatewayFilter filter = new TrafficStainingGatewayFilter(Arrays.asList(trafficStainer1, trafficStainer2));
|
||||||
|
Map<String, String> result = filter.getStainedLabels(exchange);
|
||||||
|
|
||||||
|
Assert.assertFalse(CollectionUtils.isEmpty(result));
|
||||||
|
Assert.assertEquals("v1", result.get("k1"));
|
||||||
|
Assert.assertEquals("v2", result.get("k2"));
|
||||||
|
Assert.assertEquals("v3", result.get("k3"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,181 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.rule.Condition;
|
||||||
|
import com.tencent.cloud.common.rule.KVPair;
|
||||||
|
import com.tencent.cloud.common.rule.Operation;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||||
|
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link RuleStainingExecutor}.
|
||||||
|
* @author lepdou 2022-07-12
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class RuleStainingExecutorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMatchCondition() {
|
||||||
|
Condition condition1 = new Condition();
|
||||||
|
condition1.setKey("${http.header.uid}");
|
||||||
|
condition1.setOperation(Operation.EQUAL.toString());
|
||||||
|
condition1.setValues(Collections.singletonList("1000"));
|
||||||
|
|
||||||
|
Condition condition2 = new Condition();
|
||||||
|
condition2.setKey("${http.query.source}");
|
||||||
|
condition2.setOperation(Operation.IN.toString());
|
||||||
|
condition2.setValues(Collections.singletonList("wx"));
|
||||||
|
|
||||||
|
StainingRule.Rule rule = new StainingRule.Rule();
|
||||||
|
rule.setConditions(Arrays.asList(condition1, condition2));
|
||||||
|
|
||||||
|
KVPair kvPair = new KVPair();
|
||||||
|
kvPair.setKey("env");
|
||||||
|
kvPair.setValue("blue");
|
||||||
|
rule.setLabels(Collections.singletonList(kvPair));
|
||||||
|
|
||||||
|
StainingRule stainingRule = new StainingRule();
|
||||||
|
stainingRule.setRules(Collections.singletonList(rule));
|
||||||
|
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get("/users")
|
||||||
|
.queryParam("source", "wx")
|
||||||
|
.header("uid", "1000").build();
|
||||||
|
MockServerWebExchange exchange = new MockServerWebExchange.Builder(request).build();
|
||||||
|
|
||||||
|
RuleStainingExecutor executor = new RuleStainingExecutor();
|
||||||
|
|
||||||
|
Map<String, String> stainedLabels = executor.execute(exchange, stainingRule);
|
||||||
|
|
||||||
|
Assert.assertNotNull(stainedLabels);
|
||||||
|
Assert.assertEquals(1, stainedLabels.size());
|
||||||
|
Assert.assertEquals("blue", stainedLabels.get("env"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNotMatchCondition() {
|
||||||
|
Condition condition1 = new Condition();
|
||||||
|
condition1.setKey("${http.header.uid}");
|
||||||
|
condition1.setOperation(Operation.EQUAL.toString());
|
||||||
|
condition1.setValues(Collections.singletonList("1000"));
|
||||||
|
|
||||||
|
Condition condition2 = new Condition();
|
||||||
|
condition2.setKey("${http.query.source}");
|
||||||
|
condition2.setOperation(Operation.IN.toString());
|
||||||
|
condition2.setValues(Collections.singletonList("wx"));
|
||||||
|
|
||||||
|
StainingRule.Rule rule = new StainingRule.Rule();
|
||||||
|
rule.setConditions(Arrays.asList(condition1, condition2));
|
||||||
|
|
||||||
|
KVPair kvPair = new KVPair();
|
||||||
|
kvPair.setKey("env");
|
||||||
|
kvPair.setValue("blue");
|
||||||
|
rule.setLabels(Collections.singletonList(kvPair));
|
||||||
|
|
||||||
|
StainingRule stainingRule = new StainingRule();
|
||||||
|
stainingRule.setRules(Collections.singletonList(rule));
|
||||||
|
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get("/users")
|
||||||
|
.queryParam("source", "wx")
|
||||||
|
.header("uid", "10001").build();
|
||||||
|
MockServerWebExchange exchange = new MockServerWebExchange.Builder(request).build();
|
||||||
|
|
||||||
|
RuleStainingExecutor executor = new RuleStainingExecutor();
|
||||||
|
|
||||||
|
Map<String, String> stainedLabels = executor.execute(exchange, stainingRule);
|
||||||
|
|
||||||
|
Assert.assertNotNull(stainedLabels);
|
||||||
|
Assert.assertEquals(0, stainedLabels.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMatchTwoRulesAndNotMatchOneRule() {
|
||||||
|
Condition condition1 = new Condition();
|
||||||
|
condition1.setKey("${http.header.uid}");
|
||||||
|
condition1.setOperation(Operation.EQUAL.toString());
|
||||||
|
condition1.setValues(Collections.singletonList("1000"));
|
||||||
|
|
||||||
|
Condition condition2 = new Condition();
|
||||||
|
condition2.setKey("${http.query.source}");
|
||||||
|
condition2.setOperation(Operation.IN.toString());
|
||||||
|
condition2.setValues(Collections.singletonList("wx"));
|
||||||
|
|
||||||
|
// rule1 matched
|
||||||
|
StainingRule.Rule rule1 = new StainingRule.Rule();
|
||||||
|
rule1.setConditions(Arrays.asList(condition1, condition2));
|
||||||
|
|
||||||
|
KVPair kvPair = new KVPair();
|
||||||
|
kvPair.setKey("env");
|
||||||
|
kvPair.setValue("blue");
|
||||||
|
rule1.setLabels(Collections.singletonList(kvPair));
|
||||||
|
|
||||||
|
// rule2 matched
|
||||||
|
StainingRule.Rule rule2 = new StainingRule.Rule();
|
||||||
|
rule2.setConditions(Collections.singletonList(condition1));
|
||||||
|
|
||||||
|
KVPair kvPair2 = new KVPair();
|
||||||
|
kvPair2.setKey("label1");
|
||||||
|
kvPair2.setValue("value1");
|
||||||
|
KVPair kvPair3 = new KVPair();
|
||||||
|
kvPair3.setKey("label2");
|
||||||
|
kvPair3.setValue("value2");
|
||||||
|
rule2.setLabels(Arrays.asList(kvPair2, kvPair3));
|
||||||
|
|
||||||
|
// rule3 not matched
|
||||||
|
Condition condition3 = new Condition();
|
||||||
|
condition3.setKey("${http.query.type}");
|
||||||
|
condition3.setOperation(Operation.IN.toString());
|
||||||
|
condition3.setValues(Collections.singletonList("wx"));
|
||||||
|
|
||||||
|
StainingRule.Rule rule3 = new StainingRule.Rule();
|
||||||
|
rule3.setConditions(Collections.singletonList(condition3));
|
||||||
|
|
||||||
|
KVPair kvPair4 = new KVPair();
|
||||||
|
kvPair4.setKey("label3");
|
||||||
|
kvPair4.setValue("value3");
|
||||||
|
rule3.setLabels(Collections.singletonList(kvPair4));
|
||||||
|
|
||||||
|
StainingRule stainingRule = new StainingRule();
|
||||||
|
stainingRule.setRules(Arrays.asList(rule1, rule2, rule3));
|
||||||
|
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get("/users")
|
||||||
|
.queryParam("source", "wx")
|
||||||
|
.header("uid", "1000").build();
|
||||||
|
MockServerWebExchange exchange = new MockServerWebExchange.Builder(request).build();
|
||||||
|
|
||||||
|
RuleStainingExecutor executor = new RuleStainingExecutor();
|
||||||
|
|
||||||
|
Map<String, String> stainedLabels = executor.execute(exchange, stainingRule);
|
||||||
|
|
||||||
|
Assert.assertNotNull(stainedLabels);
|
||||||
|
Assert.assertEquals(3, stainedLabels.size());
|
||||||
|
Assert.assertEquals("blue", stainedLabels.get("env"));
|
||||||
|
Assert.assertEquals("value1", stainedLabels.get("label1"));
|
||||||
|
Assert.assertEquals("value2", stainedLabels.get("label2"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.plugin.gateway.staining.rule;
|
||||||
|
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFile;
|
||||||
|
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link StainingRuleManager}.
|
||||||
|
* @author lepdou 2022-07-12
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class StainingRuleManagerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ConfigFileService configFileService;
|
||||||
|
|
||||||
|
private final String testNamespace = "testNamespace";
|
||||||
|
private final String testGroup = "testGroup";
|
||||||
|
private final String testFileName = "rule.json";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNormalRule() {
|
||||||
|
RuleStainingProperties ruleStainingProperties = new RuleStainingProperties();
|
||||||
|
ruleStainingProperties.setNamespace(testNamespace);
|
||||||
|
ruleStainingProperties.setGroup(testGroup);
|
||||||
|
ruleStainingProperties.setFileName(testFileName);
|
||||||
|
|
||||||
|
ConfigFile configFile = Mockito.mock(ConfigFile.class);
|
||||||
|
when(configFile.getContent()).thenReturn("{\n"
|
||||||
|
+ " \"rules\":[\n"
|
||||||
|
+ " {\n"
|
||||||
|
+ " \"conditions\":[\n"
|
||||||
|
+ " {\n"
|
||||||
|
+ " \"key\":\"${http.query.uid}\",\n"
|
||||||
|
+ " \"values\":[\"1000\"],\n"
|
||||||
|
+ " \"operation\":\"EQUAL\"\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ " ],\n"
|
||||||
|
+ " \"labels\":[\n"
|
||||||
|
+ " {\n"
|
||||||
|
+ " \"key\":\"env\",\n"
|
||||||
|
+ " \"value\":\"blue\"\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ " ]\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ " ]\n"
|
||||||
|
+ "}");
|
||||||
|
when(configFileService.getConfigFile(testNamespace, testGroup, testFileName)).thenReturn(configFile);
|
||||||
|
|
||||||
|
StainingRuleManager stainingRuleManager = new StainingRuleManager(ruleStainingProperties, configFileService);
|
||||||
|
|
||||||
|
StainingRule stainingRule = stainingRuleManager.getStainingRule();
|
||||||
|
|
||||||
|
Assert.assertNotNull(stainingRule);
|
||||||
|
Assert.assertEquals(1, stainingRule.getRules().size());
|
||||||
|
StainingRule.Rule rule = stainingRule.getRules().get(0);
|
||||||
|
Assert.assertEquals(1, rule.getConditions().size());
|
||||||
|
Assert.assertEquals(1, rule.getLabels().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void testWrongRule() {
|
||||||
|
RuleStainingProperties ruleStainingProperties = new RuleStainingProperties();
|
||||||
|
ruleStainingProperties.setNamespace(testNamespace);
|
||||||
|
ruleStainingProperties.setGroup(testGroup);
|
||||||
|
ruleStainingProperties.setFileName(testFileName);
|
||||||
|
|
||||||
|
ConfigFile configFile = Mockito.mock(ConfigFile.class);
|
||||||
|
when(configFile.getContent()).thenReturn("{\n"
|
||||||
|
+ " \"rules\":[\n"
|
||||||
|
+ " {\n"
|
||||||
|
+ " \"conditionsxxxx\":[\n"
|
||||||
|
+ " {\n"
|
||||||
|
+ " \"key\":\"${http.query.uid}\",\n"
|
||||||
|
+ " \"values\":[\"1000\"],\n"
|
||||||
|
+ " \"operation\":\"EQUAL\"\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ " ],\n"
|
||||||
|
+ " \"labels\":[\n"
|
||||||
|
+ " {\n"
|
||||||
|
+ " \"key\":\"env\",\n"
|
||||||
|
+ " \"value\":\"blue\"\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ " ]\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ " ]\n"
|
||||||
|
+ "}");
|
||||||
|
when(configFileService.getConfigFile(testNamespace, testGroup, testFileName)).thenReturn(configFile);
|
||||||
|
|
||||||
|
new StainingRuleManager(ruleStainingProperties, configFileService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyRule() {
|
||||||
|
RuleStainingProperties ruleStainingProperties = new RuleStainingProperties();
|
||||||
|
ruleStainingProperties.setNamespace(testNamespace);
|
||||||
|
ruleStainingProperties.setGroup(testGroup);
|
||||||
|
ruleStainingProperties.setFileName(testFileName);
|
||||||
|
|
||||||
|
ConfigFile configFile = Mockito.mock(ConfigFile.class);
|
||||||
|
when(configFile.getContent()).thenReturn(null);
|
||||||
|
when(configFileService.getConfigFile(testNamespace, testGroup, testFileName)).thenReturn(configFile);
|
||||||
|
|
||||||
|
StainingRuleManager stainingRuleManager = new StainingRuleManager(ruleStainingProperties, configFileService);
|
||||||
|
Assert.assertNull(stainingRuleManager.getStainingRule());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue