add feature-env plugin & add spring cloud gateway staining plugin. (#533)
parent
371d18f8a4
commit
8484fc5f61
12
spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerBeanPostProcessor.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/LoadBalancerInterceptorBeanPostProcessor.java
12
spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerBeanPostProcessor.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/LoadBalancerInterceptorBeanPostProcessor.java
15
spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientBeanPostProcessor.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/ReactiveLoadBalancerClientFilterBeanPostProcessor.java
15
spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientBeanPostProcessor.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/beanprocessor/ReactiveLoadBalancerClientFilterBeanPostProcessor.java
@ -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);
|
||||
}
|
19
spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerBeanPostProcessorTest.java → spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/LoadBalancerInterceptorBeanPostProcessorTest.java
19
spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/PolarisLoadBalancerBeanPostProcessorTest.java → spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/LoadBalancerInterceptorBeanPostProcessorTest.java
19
spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientBeanPostProcessorTest.java → spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/ReactiveLoadBalancerClientFilterBeanPostProcessorTest.java
19
spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientBeanPostProcessorTest.java → spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/beanprocessor/ReactiveLoadBalancerClientFilterBeanPostProcessorTest.java
@ -1,64 +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.
|
||||
*/
|
||||
EQUALS("EQUALS"),
|
||||
/**
|
||||
* case sensitive string not equals.
|
||||
*/
|
||||
NOT_EQUALS("NOT_EQUALS"),
|
||||
/**
|
||||
* 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 EQUALS:
|
||||
return firstExpectedValue != null && StringUtils.equals(actualValue, firstExpectedValue);
|
||||
case NOT_EQUALS:
|
||||
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, EQUALS.value)) {
|
||||
return EQUALS;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, NOT_EQUALS.value)) {
|
||||
return NOT_EQUALS;
|
||||
}
|
||||
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,316 +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
|
||||
*/
|
||||
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 prefix of expression.
|
||||
*/
|
||||
public static final String LABEL_PREFIX = "${";
|
||||
/**
|
||||
* the suffix of expression.
|
||||
*/
|
||||
public static final String LABEL_SUFFIX = "}";
|
||||
|
||||
private ExpressionLabelUtils() {
|
||||
}
|
||||
|
||||
public static boolean isExpressionLabel(String labelKey) {
|
||||
if (StringUtils.isEmpty(labelKey)) {
|
||||
return false;
|
||||
}
|
||||
return StringUtils.startsWith(labelKey, LABEL_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,135 @@
|
||||
/*
|
||||
* 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 prefix of expression.
|
||||
*/
|
||||
public static final String LABEL_PREFIX = "${";
|
||||
|
||||
/**
|
||||
* the suffix of expression.
|
||||
*/
|
||||
public static final String LABEL_SUFFIX = "}";
|
||||
|
||||
private ExpressionLabelUtils() {
|
||||
}
|
||||
|
||||
public static boolean isExpressionLabel(String labelKey) {
|
||||
if (StringUtils.isEmpty(labelKey)) {
|
||||
return false;
|
||||
}
|
||||
return StringUtils.startsWith(labelKey, LABEL_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.EQUALS.getValue()));
|
||||
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "v2", Operation.EQUALS.getValue()));
|
||||
Assert.assertFalse(Operation.match(Collections.singletonList(""), "v2", Operation.EQUALS.getValue()));
|
||||
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "", Operation.EQUALS.getValue()));
|
||||
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), null, Operation.EQUALS.getValue()));
|
||||
Assert.assertFalse(Operation.match(Collections.emptyList(), "v1", Operation.EQUALS.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEqual() {
|
||||
Assert.assertFalse(Operation.match(Collections.singletonList("v1"), "v1", Operation.NOT_EQUALS.getValue()));
|
||||
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), "v2", Operation.NOT_EQUALS.getValue()));
|
||||
Assert.assertTrue(Operation.match(Collections.singletonList(""), "v2", Operation.NOT_EQUALS.getValue()));
|
||||
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), "", Operation.NOT_EQUALS.getValue()));
|
||||
Assert.assertTrue(Operation.match(Collections.singletonList("v1"), null, Operation.NOT_EQUALS.getValue()));
|
||||
Assert.assertTrue(Operation.match(Collections.emptyList(), "v1", Operation.NOT_EQUALS.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,16 @@
|
||||
<?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>base</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>base-backend</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.featureenv.basebackend;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class BackendController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
return appName + "[base]";
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.basebackend;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class BaseBackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BaseBackendApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 10002
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-backend-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,16 @@
|
||||
<?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>base</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>base-front</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.basefront;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class BaseFrontApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BaseFrontApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.featureenv.basefront;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class FrontController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
@Autowired
|
||||
private MiddleService middleService;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
String curName = appName + "[base]";
|
||||
String resp = middleService.rest();
|
||||
|
||||
return curName + " -> " + resp;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.featureenv.basefront;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@FeignClient("featureenv-middle-example")
|
||||
public interface MiddleService {
|
||||
|
||||
@GetMapping("/router/rest")
|
||||
String rest();
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 10000
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-front-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,18 @@
|
||||
<?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>base</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>base-middle</artifactId>
|
||||
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.featureenv.basemiddle;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@FeignClient("featureenv-backend-example")
|
||||
public interface BackendService {
|
||||
|
||||
@GetMapping("/router/rest")
|
||||
String rest();
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.basemiddle;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class BaseMiddleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BaseMiddleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.featureenv.basemiddle;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class MiddleController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
@Autowired
|
||||
private BackendService backendService;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
String curName = appName + "[base]";
|
||||
String resp = backendService.rest();
|
||||
|
||||
return curName + " -> " + resp;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 10001
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-middle-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>polaris-router-featureenv-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>base</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>base-front</module>
|
||||
<module>base-middle</module>
|
||||
<module>base-backend</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,16 @@
|
||||
<?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>feature1</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>feature1-backend</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.featureenv.feature1backend;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class BackendController {
|
||||
|
||||
@Autowired
|
||||
private StaticMetadataManager staticMetadataManager;
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
String featureEnv = staticMetadataManager.getMergedStaticMetadata().get("featureenv");
|
||||
return appName + "[" + featureEnv + "]";
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.feature1backend;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class Feature1BackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Feature1BackendApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 11002
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-backend-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
tencent:
|
||||
metadata:
|
||||
content:
|
||||
featureenv: feature1
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,16 @@
|
||||
<?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>feature1</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>feature1-middle</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.featureenv.feature1middle;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@FeignClient("featureenv-backend-example")
|
||||
public interface BackendService {
|
||||
|
||||
@GetMapping("/router/rest")
|
||||
String rest();
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.feature1middle;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class Feature1MiddleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Feature1MiddleApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.featureenv.feature1middle;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class MiddleController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
@Autowired
|
||||
private BackendService backendService;
|
||||
|
||||
@Autowired
|
||||
private StaticMetadataManager staticMetadataManager;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
String featureEnv = staticMetadataManager.getMergedStaticMetadata().get("featureenv");
|
||||
|
||||
String curName = appName + "[" + featureEnv + "]";
|
||||
String resp = backendService.rest();
|
||||
|
||||
return curName + " -> " + resp;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 11001
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-middle-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
tencent:
|
||||
metadata:
|
||||
content:
|
||||
featureenv: feature1
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,32 @@
|
||||
<?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>polaris-router-featureenv-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>feature1</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>feature1-middle</module>
|
||||
<module>feature1-backend</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,16 @@
|
||||
<?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>feature2</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>feature2-backend</artifactId>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.featureenv.feature2backend;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class BackendController {
|
||||
|
||||
@Autowired
|
||||
private StaticMetadataManager staticMetadataManager;
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
String featureEnv = staticMetadataManager.getMergedStaticMetadata().get("featureenv");
|
||||
return appName + "[" + featureEnv + "]";
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.feature2backend;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class Feature2BackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Feature2BackendApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 12002
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-backend-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
tencent:
|
||||
metadata:
|
||||
content:
|
||||
featureenv: feature2
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,17 @@
|
||||
<?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>feature2</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>feature2-front</artifactId>
|
||||
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.featureenv.feature2front;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class Feature2FrontApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Feature2FrontApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.featureenv.feature2front;
|
||||
|
||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/router")
|
||||
public class FrontController {
|
||||
|
||||
@Autowired
|
||||
private StaticMetadataManager staticMetadataManager;
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String appName;
|
||||
|
||||
@Autowired
|
||||
private MiddleService middleService;
|
||||
|
||||
/**
|
||||
* Get information of callee.
|
||||
* @return information of callee
|
||||
*/
|
||||
@GetMapping("/rest")
|
||||
public String rest() {
|
||||
String featureEnv = staticMetadataManager.getMergedStaticMetadata().get("featureenv");
|
||||
|
||||
String curName = appName + "[" + featureEnv + "]";
|
||||
String resp = middleService.rest();
|
||||
|
||||
return curName + " -> " + resp;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.featureenv.feature2front;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@FeignClient("featureenv-middle-example")
|
||||
public interface MiddleService {
|
||||
|
||||
@GetMapping("/router/rest")
|
||||
String rest();
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 12000
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-front-example
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
tencent:
|
||||
metadata:
|
||||
content:
|
||||
featureenv: feature2
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
@ -0,0 +1,32 @@
|
||||
<?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>polaris-router-featureenv-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>feature2</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>feature2-front</module>
|
||||
<module>feature2-backend</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>polaris-router-featureenv-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>featureenv-gateway</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-featureenv-plugin</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.featureenv.gateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author lepdou 2022-07-20
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class FeatureEnvScgApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(FeatureEnvScgApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 9999
|
||||
spring:
|
||||
application:
|
||||
name: featureenv-gateway
|
||||
cloud:
|
||||
tencent:
|
||||
plugin:
|
||||
scg:
|
||||
staining:
|
||||
enabled: true
|
||||
rule-staining:
|
||||
enabled: true
|
||||
router:
|
||||
feature-env:
|
||||
enabled: true
|
||||
polaris:
|
||||
address: grpc://183.47.111.80:8091
|
||||
namespace: default
|
||||
enabled: true
|
||||
gateway:
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true
|
||||
'predicates[0]':
|
||||
name: Path
|
||||
args:
|
||||
patterns: '''/'' + serviceId + ''/**'''
|
||||
'filters[0]':
|
||||
name: RewritePath
|
||||
args:
|
||||
regexp: '''/'' + serviceId + ''/(?<remaining>.*)'''
|
||||
replacement: '''/$\{remaining}'''
|
||||
'filters[1]':
|
||||
name: Retry
|
||||
args:
|
||||
retries: 3
|
||||
exceptions:
|
||||
'[0]': '''java.net.ConnectException'''
|
||||
'[1]': '''java.io.IOException'''
|
||||
statuses:
|
||||
'[0]': '''BAD_GATEWAY'''
|
||||
'[1]': '''SERVICE_UNAVAILABLE'''
|
||||
series:
|
||||
'[0]': '''CLIENT_ERROR'''
|
||||
methods:
|
||||
'[0]': '''GET'''
|
||||
'[1]': '''POST'''
|
||||
'[2]': '''PUT'''
|
||||
'[3]': '''DELETE'''
|
||||
backoff:
|
||||
firstBackoff: '''100ms'''
|
||||
maxBackoff: '''500ms'''
|
||||
factor: 2
|
||||
basedOnPreviousValue: false
|
||||
routes:
|
||||
- id: featureenv-front-example
|
||||
uri: lb://featureenv-front-example
|
||||
predicates:
|
||||
- Path=/featureenv-front-example/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: info
|
||||
com.tencent.cloud.polaris: debug
|
After Width: | Height: | Size: 55 KiB |
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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-examples</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>polaris-router-featureenv-example</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>base</module>
|
||||
<module>feature1</module>
|
||||
<module>feature2</module>
|
||||
<module>featureenv-gateway</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-featureenv-plugin</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue