parent
1a3408661e
commit
4d960946cd
@ -1,44 +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.polaris.loadbalancer.reactive;
|
|
||||||
//
|
|
||||||
//import com.tencent.cloud.common.constant.HeaderConstant;
|
|
||||||
//import com.tencent.polaris.api.core.ConsumerAPI;
|
|
||||||
//
|
|
||||||
//import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
//import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerClientRequestTransformer;
|
|
||||||
//import org.springframework.http.HttpHeaders;
|
|
||||||
//import org.springframework.web.reactive.function.client.ClientRequest;
|
|
||||||
//
|
|
||||||
//public class PolarisLoadBalancerClientRequestTransformer implements LoadBalancerClientRequestTransformer {
|
|
||||||
//
|
|
||||||
// private final ConsumerAPI consumerAPI;
|
|
||||||
//
|
|
||||||
// public PolarisLoadBalancerClientRequestTransformer(ConsumerAPI consumerAPI) {
|
|
||||||
// this.consumerAPI = consumerAPI;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public ClientRequest transformRequest(ClientRequest request, ServiceInstance instance) {
|
|
||||||
// if (instance != null) {
|
|
||||||
// HttpHeaders headers = request.headers();
|
|
||||||
// headers.add(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID, instance.getServiceId());
|
|
||||||
// }
|
|
||||||
// return request;
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,140 +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.rpc.enhancement.feign.plugin.reporter;
|
|
||||||
//
|
|
||||||
//import java.io.UnsupportedEncodingException;
|
|
||||||
//import java.net.URI;
|
|
||||||
//import java.net.URLDecoder;
|
|
||||||
//import java.util.Collection;
|
|
||||||
//import java.util.Objects;
|
|
||||||
//import java.util.function.Consumer;
|
|
||||||
//
|
|
||||||
//import com.tencent.cloud.common.constant.RouterConstant;
|
|
||||||
//import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
//import com.tencent.cloud.common.util.RequestLabelUtils;
|
|
||||||
//import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
|
|
||||||
//import com.tencent.polaris.api.plugin.circuitbreaker.entity.InstanceResource;
|
|
||||||
//import com.tencent.polaris.api.plugin.circuitbreaker.entity.Resource;
|
|
||||||
//import com.tencent.polaris.api.pojo.RetStatus;
|
|
||||||
//import com.tencent.polaris.api.pojo.ServiceKey;
|
|
||||||
//import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
//import com.tencent.polaris.api.utils.CollectionUtils;
|
|
||||||
//import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
//import feign.Request;
|
|
||||||
//import feign.RequestTemplate;
|
|
||||||
//import feign.Response;
|
|
||||||
//import org.apache.commons.lang.StringUtils;
|
|
||||||
//import org.slf4j.Logger;
|
|
||||||
//import org.slf4j.LoggerFactory;
|
|
||||||
//
|
|
||||||
//import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * Util for polaris reporter.
|
|
||||||
// *
|
|
||||||
// * @author Haotian Zhang
|
|
||||||
// */
|
|
||||||
//public final class ReporterUtils {
|
|
||||||
//
|
|
||||||
// private static final Logger LOGGER = LoggerFactory.getLogger(ReporterUtils.class);
|
|
||||||
//
|
|
||||||
// private ReporterUtils() {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static ResourceStat createInstanceResourceStat(final Request request,
|
|
||||||
// final Response response, long delay, RetStatus retStatus) {
|
|
||||||
// return createInstanceResourceStat(request.requestTemplate().feignTarget().name(), URI.create(request.url()), response.status(), delay, retStatus);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static ResourceStat createInstanceResourceStat(final URI uri,
|
|
||||||
// final int status, long delay, RetStatus retStatus) {
|
|
||||||
// return createInstanceResourceStat(null, uri, status, delay, retStatus);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private static ResourceStat createInstanceResourceStat(final String serviceName,
|
|
||||||
// final URI uri, final int status, long delay, RetStatus retStatus) {
|
|
||||||
// ServiceKey calleeServiceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
|
|
||||||
// ServiceKey callerServiceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE);
|
|
||||||
// Resource resource = new InstanceResource(calleeServiceKey, uri.getHost(), getRequestPort(uri), callerServiceKey, getRequestProtocol(uri));
|
|
||||||
// return new ResourceStat(resource, status, delay, retStatus);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public static ServiceCallResult createServiceCallResult(final SDKContext context, final URI uri,
|
|
||||||
// final int status, long delay, RetStatus retStatus, final Consumer<ServiceCallResult> consumer) {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// public static ServiceCallResult createServiceCallResult(final SDKContext context, final Request request,
|
|
||||||
// final Response response, long delay, RetStatus retStatus, final Consumer<ServiceCallResult> consumer) {
|
|
||||||
// ServiceCallResult resultRequest = new ServiceCallResult();
|
|
||||||
//
|
|
||||||
// resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
|
||||||
// RequestTemplate requestTemplate = request.requestTemplate();
|
|
||||||
// String serviceName = requestTemplate.feignTarget().name();
|
|
||||||
// resultRequest.setService(serviceName);
|
|
||||||
// Collection<String> labels = requestTemplate.headers().get(RouterConstant.ROUTER_LABEL_HEADER);
|
|
||||||
// if (CollectionUtils.isNotEmpty(labels) && labels.iterator().hasNext()) {
|
|
||||||
// String label = labels.iterator().next();
|
|
||||||
// try {
|
|
||||||
// label = URLDecoder.decode(label, UTF_8);
|
|
||||||
// }
|
|
||||||
// catch (UnsupportedEncodingException e) {
|
|
||||||
// LOGGER.error("unsupported charset exception " + UTF_8, e);
|
|
||||||
// }
|
|
||||||
// resultRequest.setLabels(RequestLabelUtils.convertLabel(label));
|
|
||||||
// }
|
|
||||||
// URI uri = URI.create(request.url());
|
|
||||||
// resultRequest.setMethod(uri.getPath());
|
|
||||||
// resultRequest.setRetCode(response.status());
|
|
||||||
// resultRequest.setRetStatus(retStatus);
|
|
||||||
// resultRequest.setDelay(delay);
|
|
||||||
// resultRequest.setProtocol(getRequestProtocol(uri));
|
|
||||||
// String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
|
|
||||||
// String sourceService = MetadataContext.LOCAL_SERVICE;
|
|
||||||
// if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
|
|
||||||
// resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
|
|
||||||
// }
|
|
||||||
// if (Objects.nonNull(context)) {
|
|
||||||
// resultRequest.setCallerIp(context.getConfig().getGlobal().getAPI().getBindIP());
|
|
||||||
// }
|
|
||||||
// resultRequest.setHost(uri.getHost());
|
|
||||||
// resultRequest.setPort(getRequestPort(uri));
|
|
||||||
// consumer.accept(resultRequest);
|
|
||||||
// return resultRequest;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private static String getRequestProtocol(URI uri) {
|
|
||||||
// String scheme = uri.getScheme();
|
|
||||||
// if (StringUtils.isBlank(scheme)) {
|
|
||||||
// scheme = "http";
|
|
||||||
// }
|
|
||||||
// return scheme;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private static int getRequestPort(URI uri) {
|
|
||||||
// // -1 means access directly by url, and use http default port number 80
|
|
||||||
// return uri.getPort() == -1 ? 80 : uri.getPort();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private static String convertLabel(String label) {
|
|
||||||
// label = label.replaceAll("\"|\\{|\\}", "")
|
|
||||||
// .replaceAll(",", "|");
|
|
||||||
// return label;
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,240 +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.rpc.enhancement.resttemplate;
|
|
||||||
//
|
|
||||||
//import java.io.IOException;
|
|
||||||
//import java.io.UnsupportedEncodingException;
|
|
||||||
//import java.net.URI;
|
|
||||||
//import java.net.URLDecoder;
|
|
||||||
//import java.util.List;
|
|
||||||
//import java.util.Map;
|
|
||||||
//import java.util.Objects;
|
|
||||||
//
|
|
||||||
//import com.tencent.cloud.common.constant.RouterConstant;
|
|
||||||
//import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
//import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
//import com.tencent.cloud.common.util.RequestLabelUtils;
|
|
||||||
//import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
|
||||||
//import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
|
||||||
//import com.tencent.cloud.rpc.enhancement.feign.plugin.reporter.ReporterUtils;
|
|
||||||
//import com.tencent.polaris.api.core.ConsumerAPI;
|
|
||||||
//import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
|
|
||||||
//import com.tencent.polaris.api.pojo.RetStatus;
|
|
||||||
//import com.tencent.polaris.api.pojo.ServiceKey;
|
|
||||||
//import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
//import com.tencent.polaris.api.utils.CollectionUtils;
|
|
||||||
//import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
|
|
||||||
//import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
//import org.apache.commons.lang.StringUtils;
|
|
||||||
//import org.slf4j.Logger;
|
|
||||||
//import org.slf4j.LoggerFactory;
|
|
||||||
//
|
|
||||||
//import org.springframework.beans.BeansException;
|
|
||||||
//import org.springframework.context.ApplicationContext;
|
|
||||||
//import org.springframework.context.ApplicationContextAware;
|
|
||||||
//import org.springframework.http.HttpMethod;
|
|
||||||
//import org.springframework.http.client.ClientHttpResponse;
|
|
||||||
//import org.springframework.lang.NonNull;
|
|
||||||
//import org.springframework.web.client.DefaultResponseErrorHandler;
|
|
||||||
//import org.springframework.web.client.ResponseErrorHandler;
|
|
||||||
//
|
|
||||||
//import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * Extend ResponseErrorHandler to get request information.
|
|
||||||
// *
|
|
||||||
// * @author wh 2022/6/21
|
|
||||||
// */
|
|
||||||
//public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter implements ResponseErrorHandler, ApplicationContextAware {
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Polaris-CircuitBreaker-Fallback header flag.
|
|
||||||
// */
|
|
||||||
// public static final String POLARIS_CIRCUIT_BREAKER_FALLBACK_HEADER = "X-SCT-Polaris-CircuitBreaker-Fallback";
|
|
||||||
// /**
|
|
||||||
// * response has error header flag, since EnhancedRestTemplateReporter#hasError always return true.
|
|
||||||
// */
|
|
||||||
// public static final String HEADER_HAS_ERROR = "X-SCT-Has-Error";
|
|
||||||
// private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedRestTemplateReporter.class);
|
|
||||||
// private final ConsumerAPI consumerAPI;
|
|
||||||
// private final SDKContext context;
|
|
||||||
// private final CircuitBreakAPI circuitBreakAPI;
|
|
||||||
// private ResponseErrorHandler delegateHandler;
|
|
||||||
//
|
|
||||||
// public EnhancedRestTemplateReporter(RpcEnhancementReporterProperties properties, SDKContext context, ConsumerAPI consumerAPI, CircuitBreakAPI circuitBreakAPI) {
|
|
||||||
// super(properties);
|
|
||||||
// this.context = context;
|
|
||||||
// this.consumerAPI = consumerAPI;
|
|
||||||
// this.circuitBreakAPI = circuitBreakAPI;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
// String[] handlerBeanNames = applicationContext.getBeanNamesForType(ResponseErrorHandler.class);
|
|
||||||
// if (handlerBeanNames.length == 1) {
|
|
||||||
// if (this.delegateHandler == null) {
|
|
||||||
// this.delegateHandler = new DefaultResponseErrorHandler();
|
|
||||||
// }
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // inject user custom ResponseErrorHandler
|
|
||||||
// for (String beanName : handlerBeanNames) {
|
|
||||||
// // ignore self
|
|
||||||
// if (StringUtils.equalsIgnoreCase("enhancedRestTemplateReporter", beanName)) {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// this.delegateHandler = (ResponseErrorHandler) applicationContext.getBean(beanName);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public boolean hasError(@NonNull ClientHttpResponse response) throws IOException {
|
|
||||||
// if (delegateHandler != null) {
|
|
||||||
// // Preserve the delegated handler result
|
|
||||||
// boolean hasError = delegateHandler.hasError(response);
|
|
||||||
// response.getHeaders().add(HEADER_HAS_ERROR, String.valueOf(hasError));
|
|
||||||
// }
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void handleError(@NonNull ClientHttpResponse response) throws IOException {
|
|
||||||
// if (realHasError(response)) {
|
|
||||||
// delegateHandler.handleError(response);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// clear(response);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull ClientHttpResponse response) throws IOException {
|
|
||||||
// // report result to polaris
|
|
||||||
// if (reportProperties.isEnabled()) {
|
|
||||||
// reportResult(url, response);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // invoke delegate handler
|
|
||||||
// invokeDelegateHandler(url, method, response);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void reportResult(URI url, ClientHttpResponse response) {
|
|
||||||
// if (Boolean.parseBoolean(response.getHeaders().getFirst(POLARIS_CIRCUIT_BREAKER_FALLBACK_HEADER))) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// try {
|
|
||||||
// Map<String, String> loadBalancerContext = MetadataContextHolder.get().getLoadbalancerMetadata();
|
|
||||||
// String targetHost = loadBalancerContext.get("host");
|
|
||||||
// String targetPort = loadBalancerContext.get("port");
|
|
||||||
// String startMillis = loadBalancerContext.get("startMillis");
|
|
||||||
// long delay = System.currentTimeMillis() - Long.parseLong(startMillis);
|
|
||||||
//
|
|
||||||
// if (StringUtils.isBlank(targetHost) || StringUtils.isBlank(targetPort)) {
|
|
||||||
// LOGGER.warn("Can not get target host or port from metadata context. host = {}, port = {}", targetHost, targetPort);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// ServiceCallResult resultRequest = createServiceCallResult(url, response);
|
|
||||||
//
|
|
||||||
// resultRequest.setHost(targetHost);
|
|
||||||
// resultRequest.setPort(Integer.parseInt(targetPort));
|
|
||||||
// resultRequest.setDelay(delay);
|
|
||||||
//
|
|
||||||
// // checking response http status code
|
|
||||||
// if (apply(response.getStatusCode())) {
|
|
||||||
// resultRequest.setRetStatus(RetStatus.RetFail);
|
|
||||||
// }
|
|
||||||
// resultRequest.setRetStatus(getRetStatusFromRequest(response.getHeaders(), resultRequest.getRetStatus()));
|
|
||||||
// resultRequest.setRuleName(getActiveRuleNameFromRequest(response.getHeaders()));
|
|
||||||
// if (Objects.nonNull(context)) {
|
|
||||||
// resultRequest.setCallerIp(context.getConfig().getGlobal().getAPI().getBindIP());
|
|
||||||
// }
|
|
||||||
// List<String> labels = response.getHeaders().get(RouterConstant.ROUTER_LABEL_HEADER);
|
|
||||||
// if (CollectionUtils.isNotEmpty(labels)) {
|
|
||||||
// String label = labels.get(0);
|
|
||||||
// try {
|
|
||||||
// label = URLDecoder.decode(label, UTF_8);
|
|
||||||
// }
|
|
||||||
// catch (UnsupportedEncodingException e) {
|
|
||||||
// LOGGER.error("unsupported charset exception " + UTF_8, e);
|
|
||||||
// }
|
|
||||||
// resultRequest.setLabels(RequestLabelUtils.convertLabel(label));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // processing report with consumerAPI .
|
|
||||||
// LOGGER.debug("Will report result of {}. Request=[{}]. Response=[{}]. Delay=[{}]ms.", resultRequest.getRetStatus()
|
|
||||||
// .name(), url, response.getStatusCode().value(), delay);
|
|
||||||
// consumerAPI.updateServiceCallResult(resultRequest);
|
|
||||||
// ResourceStat resourceStat = ReporterUtils.createInstanceResourceStat(url, response.getRawStatusCode(), delay, resultRequest.getRetStatus());
|
|
||||||
// circuitBreakAPI.report(resourceStat);
|
|
||||||
// }
|
|
||||||
// catch (Exception e) {
|
|
||||||
// LOGGER.error("RestTemplate response reporter execute failed of {} url {}", response, url, e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void invokeDelegateHandler(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
|
|
||||||
// if (realHasError(response)) {
|
|
||||||
// delegateHandler.handleError(url, method, response);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// clear(response);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private Boolean realHasError(ClientHttpResponse response) {
|
|
||||||
// if (delegateHandler == null) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// String hasErrorHeader = response.getHeaders().getFirst(HEADER_HAS_ERROR);
|
|
||||||
// if (StringUtils.isBlank(hasErrorHeader)) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return Boolean.parseBoolean(hasErrorHeader);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void clear(ClientHttpResponse response) {
|
|
||||||
// response.getHeaders().remove(HEADER_HAS_ERROR);
|
|
||||||
// response.getHeaders().remove(POLARIS_CIRCUIT_BREAKER_FALLBACK_HEADER);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private ServiceCallResult createServiceCallResult(URI uri, ClientHttpResponse response) throws IOException {
|
|
||||||
// ServiceCallResult resultRequest = new ServiceCallResult();
|
|
||||||
// String serviceName = uri.getHost();
|
|
||||||
// resultRequest.setService(serviceName);
|
|
||||||
// resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
|
||||||
// resultRequest.setMethod(uri.getPath());
|
|
||||||
// resultRequest.setRetCode(response.getStatusCode().value());
|
|
||||||
// resultRequest.setRetStatus(RetStatus.RetSuccess);
|
|
||||||
// String sourceNamespace = MetadataContext.LOCAL_NAMESPACE;
|
|
||||||
// String sourceService = MetadataContext.LOCAL_SERVICE;
|
|
||||||
// if (StringUtils.isNotBlank(sourceNamespace) && StringUtils.isNotBlank(sourceService)) {
|
|
||||||
// resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
|
|
||||||
// }
|
|
||||||
// return resultRequest;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// protected ResponseErrorHandler getDelegateHandler() {
|
|
||||||
// return this.delegateHandler;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// protected void setDelegateHandler(ResponseErrorHandler delegateHandler) {
|
|
||||||
// this.delegateHandler = delegateHandler;
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,29 +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.rpc.enhancement.resttemplate;
|
|
||||||
//
|
|
||||||
//import org.springframework.web.client.ResponseErrorHandler;
|
|
||||||
//
|
|
||||||
///**
|
|
||||||
// * Polaris Response Error Handler Definition Of {@link ResponseErrorHandler}.
|
|
||||||
// *
|
|
||||||
// * @author wh 2022/6/21
|
|
||||||
// */
|
|
||||||
//public interface PolarisResponseErrorHandler extends ResponseErrorHandler {
|
|
||||||
//
|
|
||||||
//}
|
|
@ -1,173 +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.rpc.enhancement.scg;
|
|
||||||
//
|
|
||||||
//import java.net.SocketTimeoutException;
|
|
||||||
//import java.util.Map;
|
|
||||||
//import java.util.Objects;
|
|
||||||
//import java.util.function.BiConsumer;
|
|
||||||
//
|
|
||||||
//import com.tencent.cloud.common.constant.HeaderConstant;
|
|
||||||
//import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
//import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
//import com.tencent.cloud.rpc.enhancement.AbstractPolarisReporterAdapter;
|
|
||||||
//import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
|
||||||
//import com.tencent.polaris.api.core.ConsumerAPI;
|
|
||||||
//import com.tencent.polaris.api.pojo.RetStatus;
|
|
||||||
//import com.tencent.polaris.api.pojo.ServiceKey;
|
|
||||||
//import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
//import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
//import io.netty.handler.codec.http.HttpHeaders;
|
|
||||||
//import org.apache.commons.lang.StringUtils;
|
|
||||||
//import org.slf4j.Logger;
|
|
||||||
//import org.slf4j.LoggerFactory;
|
|
||||||
//import reactor.netty.http.client.HttpClient;
|
|
||||||
//import reactor.netty.http.client.HttpClientConfig;
|
|
||||||
//import reactor.netty.http.client.HttpClientResponse;
|
|
||||||
//
|
|
||||||
//import org.springframework.http.HttpStatus;
|
|
||||||
//
|
|
||||||
//public class EnhancedPolarisHttpClient extends HttpClient {
|
|
||||||
//
|
|
||||||
// private static final Logger LOG = LoggerFactory.getLogger(EnhancedPolarisHttpClient.class);
|
|
||||||
//
|
|
||||||
// private final RpcEnhancementReporterProperties properties;
|
|
||||||
// private final SDKContext context;
|
|
||||||
// private final ConsumerAPI consumerAPI;
|
|
||||||
// private final Reporter adapter;
|
|
||||||
// private final BiConsumer<? super HttpClientResponse, ? super Throwable> handler = new BiConsumer<HttpClientResponse, Throwable>() {
|
|
||||||
// @Override
|
|
||||||
// public void accept(HttpClientResponse httpClientResponse, Throwable throwable) {
|
|
||||||
// if (Objects.isNull(consumerAPI)) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// HttpHeaders responseHeaders = httpClientResponse.responseHeaders();
|
|
||||||
//
|
|
||||||
// ServiceCallResult result = new ServiceCallResult();
|
|
||||||
// result.setCallerService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, MetadataContext.LOCAL_SERVICE));
|
|
||||||
// result.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
|
||||||
//
|
|
||||||
// Map<String, String> metadata = MetadataContextHolder.get().getLoadbalancerMetadata();
|
|
||||||
// result.setDelay(System.currentTimeMillis() - Long.parseLong(metadata.get("startTime")));
|
|
||||||
// result.setService(metadata.get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID));
|
|
||||||
// result.setHost(metadata.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST));
|
|
||||||
// result.setPort(Integer.parseInt(metadata.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT)));
|
|
||||||
// RetStatus status = RetStatus.RetSuccess;
|
|
||||||
// if (Objects.isNull(throwable)) {
|
|
||||||
// if (EnhancedPolarisHttpClient.this.adapter.apply(HttpStatus.valueOf(httpClientResponse.status()
|
|
||||||
// .code()))) {
|
|
||||||
// status = RetStatus.RetFail;
|
|
||||||
// }
|
|
||||||
// org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
|
|
||||||
// responseHeaders.forEach(entry -> headers.add(entry.getKey(), entry.getValue()));
|
|
||||||
// status = adapter.getRetStatusFromRequest(headers, status);
|
|
||||||
// result.setRuleName(adapter.getActiveRuleNameFromRequest(headers));
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// if (throwable instanceof SocketTimeoutException) {
|
|
||||||
// status = RetStatus.RetTimeout;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// result.setMethod(httpClientResponse.uri());
|
|
||||||
// result.setRetCode(httpClientResponse.status().code());
|
|
||||||
// result.setRetStatus(status);
|
|
||||||
// if (Objects.nonNull(context)) {
|
|
||||||
// result.setCallerIp(context.getConfig().getGlobal().getAPI().getBindIP());
|
|
||||||
// }
|
|
||||||
// try {
|
|
||||||
// consumerAPI.updateServiceCallResult(result);
|
|
||||||
// }
|
|
||||||
// catch (Throwable ex) {
|
|
||||||
// LOG.error("update service call result fail", ex);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// private HttpClient target;
|
|
||||||
//
|
|
||||||
// public EnhancedPolarisHttpClient(
|
|
||||||
// HttpClient client,
|
|
||||||
// RpcEnhancementReporterProperties properties,
|
|
||||||
// SDKContext context,
|
|
||||||
// ConsumerAPI consumerAPI) {
|
|
||||||
// this.properties = properties;
|
|
||||||
// this.context = context;
|
|
||||||
// this.consumerAPI = consumerAPI;
|
|
||||||
// this.target = client;
|
|
||||||
// this.adapter = new Reporter(properties);
|
|
||||||
// this.registerReportHandler();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public HttpClientConfig configuration() {
|
|
||||||
// return target.configuration();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// protected HttpClient duplicate() {
|
|
||||||
// return new EnhancedPolarisHttpClient(target, properties, context, consumerAPI);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void registerReportHandler() {
|
|
||||||
// target = target.doOnRequest((request, connection) -> {
|
|
||||||
// String serviceId = request.requestHeaders().get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID);
|
|
||||||
// String host = request.requestHeaders().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST);
|
|
||||||
// String port = request.requestHeaders().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT);
|
|
||||||
// if (StringUtils.isNotBlank(serviceId)) {
|
|
||||||
// MetadataContextHolder.get().setLoadbalancer(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID, serviceId);
|
|
||||||
// MetadataContextHolder.get().setLoadbalancer(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST, host);
|
|
||||||
// MetadataContextHolder.get().setLoadbalancer(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT, port);
|
|
||||||
// MetadataContextHolder.get().setLoadbalancer("startTime", System.currentTimeMillis() + "");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// request.requestHeaders().remove(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID);
|
|
||||||
// request.requestHeaders().remove(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST);
|
|
||||||
// request.requestHeaders().remove(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT);
|
|
||||||
// });
|
|
||||||
// target = target.doOnResponse((httpClientResponse, connection) -> handler.accept(httpClientResponse, null));
|
|
||||||
// target = target.doOnResponseError(handler);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// private static class Reporter extends AbstractPolarisReporterAdapter {
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * Constructor With {@link RpcEnhancementReporterProperties} .
|
|
||||||
// *
|
|
||||||
// * @param reportProperties instance of {@link RpcEnhancementReporterProperties}.
|
|
||||||
// */
|
|
||||||
// protected Reporter(RpcEnhancementReporterProperties reportProperties) {
|
|
||||||
// super(reportProperties);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public boolean apply(HttpStatus httpStatus) {
|
|
||||||
// return super.apply(httpStatus);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public RetStatus getRetStatusFromRequest(org.springframework.http.HttpHeaders headers, RetStatus defaultVal) {
|
|
||||||
// return super.getRetStatusFromRequest(headers, defaultVal);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public String getActiveRuleNameFromRequest(org.springframework.http.HttpHeaders headers) {
|
|
||||||
// return super.getActiveRuleNameFromRequest(headers);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//}
|
|
@ -1,43 +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.rpc.enhancement.scg;
|
|
||||||
//
|
|
||||||
//import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
|
||||||
//import com.tencent.polaris.api.core.ConsumerAPI;
|
|
||||||
//import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
//import reactor.netty.http.client.HttpClient;
|
|
||||||
//
|
|
||||||
//import org.springframework.cloud.gateway.config.HttpClientCustomizer;
|
|
||||||
//
|
|
||||||
//public class EnhancedPolarisHttpClientCustomizer implements HttpClientCustomizer {
|
|
||||||
//
|
|
||||||
// private final RpcEnhancementReporterProperties properties;
|
|
||||||
// private final SDKContext context;
|
|
||||||
// private final ConsumerAPI consumerAPI;
|
|
||||||
//
|
|
||||||
// public EnhancedPolarisHttpClientCustomizer(RpcEnhancementReporterProperties properties, SDKContext context, ConsumerAPI consumerAPI) {
|
|
||||||
// this.properties = properties;
|
|
||||||
// this.context = context;
|
|
||||||
// this.consumerAPI = consumerAPI;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// public HttpClient customize(HttpClient httpClient) {
|
|
||||||
// return new EnhancedPolarisHttpClient(httpClient, properties, context, consumerAPI);
|
|
||||||
// }
|
|
||||||
//}
|
|
@ -1,71 +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.rpc.enhancement.scg;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.Response;
|
|
||||||
import org.springframework.cloud.gateway.filter.headers.HttpHeadersFilter;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
|
||||||
|
|
||||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
|
||||||
|
|
||||||
public class EnhancedPolarisHttpHeadersFilter implements HttpHeadersFilter {
|
|
||||||
|
|
||||||
public EnhancedPolarisHttpHeadersFilter() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
|
|
||||||
Response<ServiceInstance> serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
|
||||||
if (serviceInstanceResponse == null || !serviceInstanceResponse.hasServer()) {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
ServiceInstance instance = serviceInstanceResponse.getServer();
|
|
||||||
write(input, HeaderConstant.INTERNAL_CALLEE_SERVICE_ID, instance.getServiceId(), true);
|
|
||||||
write(input, HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST, instance.getHost(), true);
|
|
||||||
write(input, HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT, instance.getPort() + "", true);
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean supports(Type type) {
|
|
||||||
return Type.REQUEST.equals(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void write(HttpHeaders headers, String name, String value, boolean append) {
|
|
||||||
if (value == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (append) {
|
|
||||||
headers.add(name, value);
|
|
||||||
// these headers should be treated as a single comma separated header
|
|
||||||
List<String> values = headers.get(name);
|
|
||||||
String delimitedValue = StringUtils.collectionToCommaDelimitedString(values);
|
|
||||||
headers.set(name, delimitedValue);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
headers.set(name, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,161 +1,162 @@
|
|||||||
/*
|
///*
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
// * 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.
|
// * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
*
|
// *
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
// * Licensed under the BSD 3-Clause License (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
// * you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
// * You may obtain a copy of the License at
|
||||||
*
|
// *
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
// * https://opensource.org/licenses/BSD-3-Clause
|
||||||
*
|
// *
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed
|
// * 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
|
// * 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
|
// * CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations under the License.
|
// * specific language governing permissions and limitations under the License.
|
||||||
*/
|
// */
|
||||||
|
//
|
||||||
package com.tencent.cloud.rpc.enhancement;
|
//package com.tencent.cloud.rpc.enhancement;
|
||||||
|
//
|
||||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
//import com.tencent.cloud.common.constant.HeaderConstant;
|
||||||
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
//import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementReporterProperties;
|
||||||
import com.tencent.polaris.api.pojo.RetStatus;
|
//import com.tencent.polaris.api.pojo.RetStatus;
|
||||||
import org.assertj.core.api.Assertions;
|
//import org.assertj.core.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
//import org.junit.jupiter.api.Test;
|
||||||
|
//
|
||||||
import org.springframework.http.HttpHeaders;
|
//import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
//import org.springframework.http.HttpStatus;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* Test For {@link AbstractPolarisReporterAdapter}.
|
// * Test For {@link AbstractPolarisReporterAdapter}.
|
||||||
*
|
// *
|
||||||
* @author <a href="mailto:iskp.me@gmail.com">Elve.Xu</a> 2022/7/11
|
// * @author <a href="mailto:iskp.me@gmail.com">Elve.Xu</a> 2022/7/11
|
||||||
*/
|
// */
|
||||||
public class AbstractPolarisReporterAdapterTest {
|
//public class AbstractPolarisReporterAdapterTest {
|
||||||
|
//
|
||||||
@Test
|
// @Test
|
||||||
public void testApplyWithDefaultConfig() {
|
// public void testApplyWithDefaultConfig() {
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
// Mock Condition
|
// // Mock Condition
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
//
|
||||||
// Assert
|
// // Assert
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
// Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Test
|
// @Test
|
||||||
public void testApplyWithoutIgnoreInternalServerError() {
|
// public void testApplyWithoutIgnoreInternalServerError() {
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
// Mock Condition
|
// // Mock Condition
|
||||||
properties.getStatuses().clear();
|
// properties.getStatuses().clear();
|
||||||
properties.setIgnoreInternalServerError(false);
|
// properties.setIgnoreInternalServerError(false);
|
||||||
|
//
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
//
|
||||||
// Assert
|
// // Assert
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(true);
|
// Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(true);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
// Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Test
|
// @Test
|
||||||
public void testApplyWithIgnoreInternalServerError() {
|
// public void testApplyWithIgnoreInternalServerError() {
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
// Mock Condition
|
// // Mock Condition
|
||||||
properties.getStatuses().clear();
|
// properties.getStatuses().clear();
|
||||||
properties.setIgnoreInternalServerError(true);
|
// properties.setIgnoreInternalServerError(true);
|
||||||
|
//
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
//
|
||||||
// Assert
|
// // Assert
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
// Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Test
|
// @Test
|
||||||
public void testApplyWithoutSeries() {
|
// public void testApplyWithoutSeries() {
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
// Mock Condition
|
// // Mock Condition
|
||||||
properties.getStatuses().clear();
|
// properties.getStatuses().clear();
|
||||||
properties.getSeries().clear();
|
// properties.getSeries().clear();
|
||||||
|
//
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
//
|
||||||
// Assert
|
// // Assert
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
// Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(true);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Test
|
// @Test
|
||||||
public void testApplyWithSeries() {
|
// public void testApplyWithSeries() {
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
// Mock Condition
|
// // Mock Condition
|
||||||
properties.getStatuses().clear();
|
// properties.getStatuses().clear();
|
||||||
properties.getSeries().clear();
|
// properties.getSeries().clear();
|
||||||
properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
// properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
||||||
|
//
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
|
//
|
||||||
// Assert
|
// // Assert
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.OK)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.INTERNAL_SERVER_ERROR)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(false);
|
// Assertions.assertThat(adapter.apply(HttpStatus.BAD_GATEWAY)).isEqualTo(false);
|
||||||
Assertions.assertThat(adapter.apply(HttpStatus.FORBIDDEN)).isEqualTo(true);
|
// Assertions.assertThat(adapter.apply(HttpStatus.FORBIDDEN)).isEqualTo(true);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Test
|
//
|
||||||
public void testGetRetStatusFromRequest() {
|
// @Test
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// public void testGetRetStatusFromRequest() {
|
||||||
// Mock Condition
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
properties.getStatuses().clear();
|
// // Mock Condition
|
||||||
properties.getSeries().clear();
|
// properties.getStatuses().clear();
|
||||||
properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
// properties.getSeries().clear();
|
||||||
|
// properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
//
|
||||||
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
HttpHeaders headers = new HttpHeaders();
|
//
|
||||||
RetStatus ret = adapter.getRetStatusFromRequest(headers, RetStatus.RetFail);
|
// HttpHeaders headers = new HttpHeaders();
|
||||||
Assertions.assertThat(ret).isEqualTo(RetStatus.RetFail);
|
// RetStatus ret = adapter.getRetStatusFromRequest(headers, RetStatus.RetFail);
|
||||||
|
// Assertions.assertThat(ret).isEqualTo(RetStatus.RetFail);
|
||||||
headers.set(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetFlowControl.getDesc());
|
//
|
||||||
ret = adapter.getRetStatusFromRequest(headers, RetStatus.RetFail);
|
// headers.set(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetFlowControl.getDesc());
|
||||||
Assertions.assertThat(ret).isEqualTo(RetStatus.RetFlowControl);
|
// ret = adapter.getRetStatusFromRequest(headers, RetStatus.RetFail);
|
||||||
|
// Assertions.assertThat(ret).isEqualTo(RetStatus.RetFlowControl);
|
||||||
headers.set(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetReject.getDesc());
|
//
|
||||||
ret = adapter.getRetStatusFromRequest(headers, RetStatus.RetFail);
|
// headers.set(HeaderConstant.INTERNAL_CALLEE_RET_STATUS, RetStatus.RetReject.getDesc());
|
||||||
Assertions.assertThat(ret).isEqualTo(RetStatus.RetReject);
|
// ret = adapter.getRetStatusFromRequest(headers, RetStatus.RetFail);
|
||||||
}
|
// Assertions.assertThat(ret).isEqualTo(RetStatus.RetReject);
|
||||||
|
// }
|
||||||
@Test
|
//
|
||||||
public void testGetActiveRuleNameFromRequest() {
|
// @Test
|
||||||
RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
// public void testGetActiveRuleNameFromRequest() {
|
||||||
// Mock Condition
|
// RpcEnhancementReporterProperties properties = new RpcEnhancementReporterProperties();
|
||||||
properties.getStatuses().clear();
|
// // Mock Condition
|
||||||
properties.getSeries().clear();
|
// properties.getStatuses().clear();
|
||||||
properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
// properties.getSeries().clear();
|
||||||
|
// properties.getSeries().add(HttpStatus.Series.CLIENT_ERROR);
|
||||||
SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
//
|
||||||
|
// SimplePolarisReporterAdapter adapter = new SimplePolarisReporterAdapter(properties);
|
||||||
HttpHeaders headers = new HttpHeaders();
|
//
|
||||||
String ruleName = adapter.getActiveRuleNameFromRequest(headers);
|
// HttpHeaders headers = new HttpHeaders();
|
||||||
Assertions.assertThat(ruleName).isEqualTo("");
|
// String ruleName = adapter.getActiveRuleNameFromRequest(headers);
|
||||||
|
// Assertions.assertThat(ruleName).isEqualTo("");
|
||||||
headers.set(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME, "mock_rule");
|
//
|
||||||
ruleName = adapter.getActiveRuleNameFromRequest(headers);
|
// headers.set(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME, "mock_rule");
|
||||||
Assertions.assertThat(ruleName).isEqualTo("mock_rule");
|
// ruleName = adapter.getActiveRuleNameFromRequest(headers);
|
||||||
}
|
// Assertions.assertThat(ruleName).isEqualTo("mock_rule");
|
||||||
|
// }
|
||||||
/**
|
//
|
||||||
* Simple Polaris CircuitBreak Adapter Implements .
|
// /**
|
||||||
*/
|
// * Simple Polaris CircuitBreak Adapter Implements .
|
||||||
public static class SimplePolarisReporterAdapter extends AbstractPolarisReporterAdapter {
|
// */
|
||||||
|
// public static class SimplePolarisReporterAdapter extends AbstractPolarisReporterAdapter {
|
||||||
public SimplePolarisReporterAdapter(RpcEnhancementReporterProperties properties) {
|
//
|
||||||
super(properties);
|
// public SimplePolarisReporterAdapter(RpcEnhancementReporterProperties properties) {
|
||||||
}
|
// super(properties);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//}
|
||||||
|
@ -1,135 +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.rpc.enhancement.feign.plugin.reporter;
|
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.constant.RouterConstant;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
|
||||||
import com.tencent.polaris.api.config.Configuration;
|
|
||||||
import com.tencent.polaris.api.config.global.APIConfig;
|
|
||||||
import com.tencent.polaris.api.config.global.GlobalConfig;
|
|
||||||
import com.tencent.polaris.api.pojo.RetStatus;
|
|
||||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
|
||||||
import com.tencent.polaris.client.api.SDKContext;
|
|
||||||
import feign.Request;
|
|
||||||
import feign.RequestTemplate;
|
|
||||||
import feign.Response;
|
|
||||||
import feign.Target;
|
|
||||||
import org.junit.jupiter.api.AfterAll;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.MockedStatic;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
|
|
||||||
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
|
|
||||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
|
||||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for {@link ReporterUtils}.
|
|
||||||
*
|
|
||||||
* @author Haotian Zhang
|
|
||||||
*/
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
public class ReporterUtilsTest {
|
|
||||||
|
|
||||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void beforeAll() {
|
|
||||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
|
||||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
|
||||||
.thenReturn("unit-test");
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterAll
|
|
||||||
static void afterAll() {
|
|
||||||
mockedApplicationContextAwareUtils.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
|
||||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateServiceCallResult() {
|
|
||||||
// mock target
|
|
||||||
Target<?> target = mock(Target.class);
|
|
||||||
doReturn(SERVICE_PROVIDER).when(target).name();
|
|
||||||
|
|
||||||
// mock RequestTemplate.class
|
|
||||||
RequestTemplate requestTemplate = new RequestTemplate();
|
|
||||||
requestTemplate.feignTarget(target);
|
|
||||||
try {
|
|
||||||
requestTemplate.header(RouterConstant.ROUTER_LABEL_HEADER, URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8));
|
|
||||||
}
|
|
||||||
catch (UnsupportedEncodingException e) {
|
|
||||||
throw new RuntimeException("unsupported charset exception " + UTF_8);
|
|
||||||
}
|
|
||||||
|
|
||||||
// mock request
|
|
||||||
Request request = mock(Request.class);
|
|
||||||
doReturn(requestTemplate).when(request).requestTemplate();
|
|
||||||
doReturn("http://1.1.1.1:2345/path").when(request).url();
|
|
||||||
|
|
||||||
// mock request
|
|
||||||
Response response = mock(Response.class);
|
|
||||||
doReturn(502).when(response).status();
|
|
||||||
|
|
||||||
ServiceCallResult serviceCallResult = ReporterUtils.createServiceCallResult(mockSDKContext(), request, response, 10L, RetStatus.RetSuccess, result -> {
|
|
||||||
|
|
||||||
});
|
|
||||||
assertThat(serviceCallResult.getNamespace()).isEqualTo(NAMESPACE_TEST);
|
|
||||||
assertThat(serviceCallResult.getService()).isEqualTo(SERVICE_PROVIDER);
|
|
||||||
assertThat(serviceCallResult.getHost()).isEqualTo("1.1.1.1");
|
|
||||||
assertThat(serviceCallResult.getPort()).isEqualTo(2345);
|
|
||||||
assertThat(serviceCallResult.getRetStatus()).isEqualTo(RetStatus.RetSuccess);
|
|
||||||
assertThat(serviceCallResult.getMethod()).isEqualTo("/path");
|
|
||||||
assertThat(serviceCallResult.getCallerService().getNamespace()).isEqualTo(NAMESPACE_TEST);
|
|
||||||
assertThat(serviceCallResult.getCallerService().getService()).isEqualTo(SERVICE_PROVIDER);
|
|
||||||
assertThat(serviceCallResult.getLabels()).isEqualTo("k1:v1|k2:v2");
|
|
||||||
assertThat(serviceCallResult.getRetCode()).isEqualTo(502);
|
|
||||||
assertThat(serviceCallResult.getDelay()).isEqualTo(10L);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static SDKContext mockSDKContext() {
|
|
||||||
APIConfig apiConfig = mock(APIConfig.class);
|
|
||||||
doReturn("127.0.0.1").when(apiConfig).getBindIP();
|
|
||||||
GlobalConfig globalConfig = mock(GlobalConfig.class);
|
|
||||||
doReturn(apiConfig).when(globalConfig).getAPI();
|
|
||||||
Configuration configuration = mock(Configuration.class);
|
|
||||||
doReturn(globalConfig).when(configuration).getGlobal();
|
|
||||||
SDKContext context = mock(SDKContext.class);
|
|
||||||
doReturn(configuration).when(context).getConfig();
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.tencent.cloud.rpc.enhancement.resttemplate;
|
||||||
|
|
||||||
|
public class EnhancedPolarisRestTemplateReporterTest {
|
||||||
|
|
||||||
|
}
|
@ -1,57 +1,52 @@
|
|||||||
/*
|
///*
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
// * 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.
|
// * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
*
|
// *
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
// * Licensed under the BSD 3-Clause License (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
// * you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
// * You may obtain a copy of the License at
|
||||||
*
|
// *
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
// * https://opensource.org/licenses/BSD-3-Clause
|
||||||
*
|
// *
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed
|
// * 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
|
// * 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
|
// * CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations under the License.
|
// * specific language governing permissions and limitations under the License.
|
||||||
*/
|
// */
|
||||||
|
//
|
||||||
package com.tencent.cloud.rpc.enhancement.scg;
|
//package com.tencent.cloud.rpc.enhancement.scg;
|
||||||
|
//
|
||||||
import java.util.Collections;
|
//import java.util.Collections;
|
||||||
|
//
|
||||||
import com.tencent.cloud.common.constant.HeaderConstant;
|
//import com.tencent.cloud.common.constant.HeaderConstant;
|
||||||
import org.junit.jupiter.api.Assertions;
|
//import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
//import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
//import org.mockito.Mockito;
|
||||||
|
//
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
//import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
//import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
||||||
import org.springframework.http.HttpHeaders;
|
//import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
//import org.springframework.web.server.ServerWebExchange;
|
||||||
|
//
|
||||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
//import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
|
||||||
|
//
|
||||||
public class EnhancedPolarisHttpHeadersFilterTest {
|
//public class EnhancedPolarisHttpHeadersFilterTest {
|
||||||
|
//
|
||||||
@Test
|
// @Test
|
||||||
public void testFilter() {
|
// public void testFilter() {
|
||||||
EnhancedPolarisHttpHeadersFilter filter = new EnhancedPolarisHttpHeadersFilter();
|
// EnhancedPolarisHttpHeadersFilter filter = new EnhancedPolarisHttpHeadersFilter();
|
||||||
|
// ServiceInstance instance = Mockito.mock(ServiceInstance.class);
|
||||||
ServiceInstance instance = Mockito.mock(ServiceInstance.class);
|
// Mockito.doReturn("mock_service").when(instance).getServiceId();
|
||||||
Mockito.doReturn("mock_service").when(instance).getServiceId();
|
// Mockito.doReturn("127.0.0.1").when(instance).getHost();
|
||||||
Mockito.doReturn("127.0.0.1").when(instance).getHost();
|
// Mockito.doReturn(8080).when(instance).getPort();
|
||||||
Mockito.doReturn(8080).when(instance).getPort();
|
// DefaultResponse response = new DefaultResponse(instance);
|
||||||
DefaultResponse response = new DefaultResponse(instance);
|
// ServerWebExchange exchange = Mockito.mock(ServerWebExchange.class);
|
||||||
|
// Mockito.doReturn(response).when(exchange).getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
||||||
ServerWebExchange exchange = Mockito.mock(ServerWebExchange.class);
|
// HttpHeaders input = new HttpHeaders();
|
||||||
Mockito.doReturn(response).when(exchange).getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
|
// HttpHeaders headers = filter.filter(input, exchange);
|
||||||
|
// Assertions.assertEquals(Collections.singletonList("mock_service"), headers.get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID));
|
||||||
HttpHeaders input = new HttpHeaders();
|
// Assertions.assertEquals(Collections.singletonList("127.0.0.1"), headers.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST));
|
||||||
HttpHeaders headers = filter.filter(input, exchange);
|
// Assertions.assertEquals(Collections.singletonList("8080"), headers.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT));
|
||||||
|
// }
|
||||||
Assertions.assertEquals(Collections.singletonList("mock_service"), headers.get(HeaderConstant.INTERNAL_CALLEE_SERVICE_ID));
|
//}
|
||||||
Assertions.assertEquals(Collections.singletonList("127.0.0.1"), headers.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST));
|
|
||||||
Assertions.assertEquals(Collections.singletonList("8080"), headers.get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in new issue