parent
5d7310a9e7
commit
542f6a4c03
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* 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 reactor.netty.Connection;
|
||||
import reactor.netty.http.client.HttpClient;
|
||||
import reactor.netty.http.client.HttpClientConfig;
|
||||
import reactor.netty.http.client.HttpClientRequest;
|
||||
import reactor.netty.http.client.HttpClientResponse;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class EnhancedPolarisHttpClient extends HttpClient {
|
||||
|
||||
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;
|
||||
}
|
||||
status = getRetStatusFromRequest(responseHeaders, status);
|
||||
}
|
||||
else {
|
||||
if (throwable instanceof SocketTimeoutException) {
|
||||
status = RetStatus.RetTimeout;
|
||||
}
|
||||
}
|
||||
result.setMethod(httpClientResponse.uri());
|
||||
result.setRetCode(httpClientResponse.status().code());
|
||||
result.setRuleName(getActiveRuleNameFromRequest(responseHeaders));
|
||||
result.setRetStatus(status);
|
||||
if (Objects.nonNull(context)) {
|
||||
result.setCallerIp(context.getConfig().getGlobal().getAPI().getBindIP());
|
||||
}
|
||||
consumerAPI.updateServiceCallResult(result);
|
||||
}
|
||||
};
|
||||
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();
|
||||
}
|
||||
|
||||
private static RetStatus getRetStatusFromRequest(HttpHeaders headers, RetStatus defaultVal) {
|
||||
if (headers.contains(HeaderConstant.INTERNAL_CALLEE_RET_STATUS)) {
|
||||
String retStatusVal = headers.get(HeaderConstant.INTERNAL_CALLEE_RET_STATUS);
|
||||
if (Objects.equals(retStatusVal, RetStatus.RetFlowControl.getDesc())) {
|
||||
return RetStatus.RetFlowControl;
|
||||
}
|
||||
if (Objects.equals(retStatusVal, RetStatus.RetReject.getDesc())) {
|
||||
return RetStatus.RetReject;
|
||||
}
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
private static String getActiveRuleNameFromRequest(HttpHeaders headers) {
|
||||
if (headers.contains(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME)) {
|
||||
String val = headers.get(HeaderConstant.INTERNAL_ACTIVE_RULE_NAME);
|
||||
return val;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClientConfig configuration() {
|
||||
return target.configuration();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpClient duplicate() {
|
||||
return new EnhancedPolarisHttpClient(target, properties, context, consumerAPI);
|
||||
}
|
||||
|
||||
private void registerReportHandler() {
|
||||
target = target.doOnRequest(new BiConsumer<HttpClientRequest, Connection>() {
|
||||
@Override
|
||||
public void accept(HttpClientRequest request, Connection 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue