parent
11a10ed6c6
commit
ad9e7cf1d9
@ -0,0 +1,48 @@
|
|||||||
|
<?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-plugin-starters</artifactId>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-cloud-starter-tencent-tsf-adapter-plugin</artifactId>
|
||||||
|
<name>Spring Cloud Starter Tencent TSF Plugin</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- Spring Cloud Tencent dependencies start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-tencent-rpc-enhancement</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Spring Cloud Tencent dependencies end -->
|
||||||
|
|
||||||
|
<!-- Polaris dependencies start -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.tencent.polaris</groupId>
|
||||||
|
<artifactId>polaris-assembly-factory</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Polaris dependencies end -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-inline</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.tsf.adapter.config;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
|
||||||
|
import com.tencent.polaris.factory.config.ConfigurationImpl;
|
||||||
|
|
||||||
|
public class PolarisFlowConfigModifier implements PolarisConfigModifier {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(ConfigurationImpl configuration) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.tsf.adapter.config;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
|
||||||
|
import com.tencent.polaris.api.exception.PolarisException;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnPolarisEnabled
|
||||||
|
@AutoConfigureAfter(RpcEnhancementAutoConfiguration.class)
|
||||||
|
public class PolarisTsfAdapterAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public PolarisFlowConfigModifier polarisFlowConfigModifier() throws PolarisException {
|
||||||
|
return new PolarisFlowConfigModifier();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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.rpc.enhancement.feign;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||||
|
import feign.Request;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.openfeign.loadbalancer.LoadBalancerFeignRequestTransformer;
|
||||||
|
|
||||||
|
import static com.tencent.cloud.rpc.enhancement.resttemplate.PolarisLoadBalancerRequestTransformer.LOAD_BALANCER_SERVICE_INSTANCE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PolarisLoadBalancerFeignRequestTransformer.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class PolarisLoadBalancerFeignRequestTransformer implements LoadBalancerFeignRequestTransformer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform Request, add Loadbalancer ServiceInstance to MetadataContext.
|
||||||
|
* @param request request
|
||||||
|
* @param instance instance
|
||||||
|
* @return HttpRequest
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Request transformRequest(Request request, ServiceInstance instance) {
|
||||||
|
if (instance != null) {
|
||||||
|
MetadataContextHolder.get().setLoadbalancer(LOAD_BALANCER_SERVICE_INSTANCE, instance);
|
||||||
|
}
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* 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.filter;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import org.springframework.web.server.WebFilter;
|
||||||
|
import org.springframework.web.server.WebFilterChain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnhancedReactiveFilter.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class EnhancedReactiveFilter implements WebFilter, Ordered {
|
||||||
|
|
||||||
|
private final EnhancedPluginRunner pluginRunner;
|
||||||
|
|
||||||
|
public EnhancedReactiveFilter(EnhancedPluginRunner pluginRunner) {
|
||||||
|
this.pluginRunner = pluginRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||||
|
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||||
|
|
||||||
|
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||||
|
.httpHeaders(exchange.getRequest().getHeaders())
|
||||||
|
.httpMethod(exchange.getRequest().getMethod())
|
||||||
|
.url(exchange.getRequest().getURI())
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setRequest(enhancedRequestContext);
|
||||||
|
|
||||||
|
// Run pre enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.PRE, enhancedPluginContext);
|
||||||
|
|
||||||
|
long startMillis = System.currentTimeMillis();
|
||||||
|
return chain.filter(exchange)
|
||||||
|
.doOnSuccess(v -> {
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||||
|
|
||||||
|
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(exchange.getResponse().getRawStatusCode())
|
||||||
|
.httpHeaders(exchange.getResponse().getHeaders())
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setResponse(enhancedResponseContext);
|
||||||
|
|
||||||
|
// Run post enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.POST, enhancedPluginContext);
|
||||||
|
})
|
||||||
|
.doOnError(e -> {
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||||
|
enhancedPluginContext.setThrowable(e);
|
||||||
|
// Run exception enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.EXCEPTION, enhancedPluginContext);
|
||||||
|
})
|
||||||
|
.doFinally(v -> {
|
||||||
|
// Run finally enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.FINALLY, enhancedPluginContext);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return MetadataConstant.OrderConstant.WEB_FILTER_ORDER + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* 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.filter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EnhancedServletFilter.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
@Order(MetadataConstant.OrderConstant.WEB_FILTER_ORDER + 1)
|
||||||
|
public class EnhancedServletFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
private final EnhancedPluginRunner pluginRunner;
|
||||||
|
|
||||||
|
public EnhancedServletFilter(EnhancedPluginRunner pluginRunner) {
|
||||||
|
this.pluginRunner = pluginRunner;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
|
||||||
|
|
||||||
|
HttpHeaders requestHeaders = new HttpHeaders();
|
||||||
|
Enumeration<String> requestHeaderNames = request.getHeaderNames();
|
||||||
|
if (requestHeaderNames != null) {
|
||||||
|
while (requestHeaderNames.hasMoreElements()) {
|
||||||
|
String requestHeaderName = requestHeaderNames.nextElement();
|
||||||
|
requestHeaders.addAll(requestHeaderName, Collections.list(request.getHeaders(requestHeaderName)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
|
||||||
|
.httpHeaders(requestHeaders)
|
||||||
|
.httpMethod(HttpMethod.valueOf(request.getMethod()))
|
||||||
|
.url(URI.create(request.getRequestURL().toString()))
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setRequest(enhancedRequestContext);
|
||||||
|
|
||||||
|
// Run pre enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.PRE, enhancedPluginContext);
|
||||||
|
|
||||||
|
long startMillis = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||||
|
|
||||||
|
HttpHeaders responseHeaders = new HttpHeaders();
|
||||||
|
Collection<String> responseHeaderNames = response.getHeaderNames();
|
||||||
|
if (responseHeaderNames != null) {
|
||||||
|
for (String responseHeaderName : responseHeaderNames) {
|
||||||
|
responseHeaders.addAll(responseHeaderName, new ArrayList<>(response.getHeaders(responseHeaderName)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EnhancedResponseContext enhancedResponseContext = EnhancedResponseContext.builder()
|
||||||
|
.httpStatus(response.getStatus())
|
||||||
|
.httpHeaders(responseHeaders)
|
||||||
|
.build();
|
||||||
|
enhancedPluginContext.setResponse(enhancedResponseContext);
|
||||||
|
|
||||||
|
// Run post enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.POST, enhancedPluginContext);
|
||||||
|
}
|
||||||
|
catch (ServletException | IOException e) {
|
||||||
|
enhancedPluginContext.setDelay(System.currentTimeMillis() - startMillis);
|
||||||
|
enhancedPluginContext.setThrowable(e);
|
||||||
|
// Run exception enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.EXCEPTION, enhancedPluginContext);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Run finally enhanced plugins.
|
||||||
|
pluginRunner.run(EnhancedPluginType.Server.FINALLY, enhancedPluginContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedRequestContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyRequestContext.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyRequestContext implements RequestContext {
|
||||||
|
|
||||||
|
private final EnhancedRequestContext requestContext;
|
||||||
|
|
||||||
|
private final ServiceKey callerService;
|
||||||
|
|
||||||
|
private final String callerIp;
|
||||||
|
|
||||||
|
public AssemblyRequestContext(EnhancedRequestContext requestContext, ServiceKey callerService, String callerIp) {
|
||||||
|
this.requestContext = requestContext;
|
||||||
|
this.callerService = callerService;
|
||||||
|
this.callerIp = callerIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMethod() {
|
||||||
|
return requestContext.getHttpMethod().name();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMethod(String method) {
|
||||||
|
requestContext.setHttpMethod(HttpMethod.valueOf(method));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getHeader(String key) {
|
||||||
|
return requestContext.getHttpHeaders().getFirst(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setHeader(String key, String value) {
|
||||||
|
requestContext.getHttpHeaders().set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> listHeaderKeys() {
|
||||||
|
return requestContext.getHttpHeaders().keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCookie(String key) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCookie(String key, String value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> listCookieKeys() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCallerIp() {
|
||||||
|
return callerIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServiceKey getCallerService() {
|
||||||
|
return callerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URI getURI() {
|
||||||
|
return requestContext.getUrl();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedResponseContext;
|
||||||
|
import com.tencent.polaris.api.pojo.RetStatus;
|
||||||
|
import com.tencent.polaris.api.rpc.ResponseContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyResponseContext.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyResponseContext implements ResponseContext {
|
||||||
|
|
||||||
|
private EnhancedResponseContext responseContext;
|
||||||
|
|
||||||
|
private Throwable throwable;
|
||||||
|
|
||||||
|
private RetStatus retStatus;
|
||||||
|
|
||||||
|
public AssemblyResponseContext() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public AssemblyResponseContext(EnhancedResponseContext responseContext) {
|
||||||
|
this.responseContext = responseContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getRetCode() {
|
||||||
|
return this.responseContext.getHttpStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getHeader(String key) {
|
||||||
|
return this.responseContext.getHttpHeaders().getFirst(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> listHeaders() {
|
||||||
|
return this.responseContext.getHttpHeaders().keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThrowable(Throwable throwable) {
|
||||||
|
this.throwable = throwable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Throwable getThrowable() {
|
||||||
|
return this.throwable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRetStatus(RetStatus retStatus) {
|
||||||
|
this.retStatus = retStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RetStatus getRetStatus() {
|
||||||
|
return this.retStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly.client;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||||
|
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyClientExceptionHook.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyClientExceptionHook implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final AssemblyAPI assemblyAPI;
|
||||||
|
|
||||||
|
public AssemblyClientExceptionHook(AssemblyAPI assemblyAPI) {
|
||||||
|
this.assemblyAPI = assemblyAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Client.EXCEPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
AfterRequest afterRequest = new AfterRequest();
|
||||||
|
afterRequest.setTargetService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getTargetServiceInstance().getServiceId()));
|
||||||
|
RequestContext requestContext = new AssemblyRequestContext(
|
||||||
|
context.getRequest(),
|
||||||
|
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||||
|
context.getLocalServiceInstance().getHost()
|
||||||
|
);
|
||||||
|
afterRequest.setRequestContext(requestContext);
|
||||||
|
AssemblyResponseContext responseContext = new AssemblyResponseContext();
|
||||||
|
responseContext.setThrowable(context.getThrowable());
|
||||||
|
responseContext.setRetStatus(PolarisEnhancedPluginUtils.getRetStatusFromRequest(null, null, context.getThrowable()));
|
||||||
|
afterRequest.setResponseContext(responseContext);
|
||||||
|
|
||||||
|
assemblyAPI.afterCallService(afterRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 3;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly.client;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||||
|
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyClientPostHook.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyClientPostHook implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final AssemblyAPI assemblyAPI;
|
||||||
|
|
||||||
|
public AssemblyClientPostHook(AssemblyAPI assemblyAPI) {
|
||||||
|
this.assemblyAPI = assemblyAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Client.POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
AfterRequest afterRequest = new AfterRequest();
|
||||||
|
afterRequest.setTargetService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getTargetServiceInstance().getServiceId()));
|
||||||
|
RequestContext requestContext = new AssemblyRequestContext(
|
||||||
|
context.getRequest(),
|
||||||
|
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||||
|
context.getLocalServiceInstance().getHost()
|
||||||
|
);
|
||||||
|
afterRequest.setRequestContext(requestContext);
|
||||||
|
AssemblyResponseContext responseContext = new AssemblyResponseContext(context.getResponse());
|
||||||
|
responseContext.setRetStatus(PolarisEnhancedPluginUtils.getRetStatusFromRequest(context.getResponse().getHttpHeaders(), context.getResponse().getHttpStatus(), null));
|
||||||
|
afterRequest.setResponseContext(responseContext);
|
||||||
|
|
||||||
|
assemblyAPI.afterCallService(afterRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly.client;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||||
|
import com.tencent.polaris.assembly.api.pojo.BeforeRequest;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyClientPreHook.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyClientPreHook implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final AssemblyAPI assemblyAPI;
|
||||||
|
|
||||||
|
public AssemblyClientPreHook(AssemblyAPI assemblyAPI) {
|
||||||
|
this.assemblyAPI = assemblyAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Client.PRE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
BeforeRequest beforeRequest = new BeforeRequest();
|
||||||
|
beforeRequest.setTargetService(new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getTargetServiceInstance().getServiceId()));
|
||||||
|
RequestContext requestContext = new AssemblyRequestContext(
|
||||||
|
context.getRequest(),
|
||||||
|
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||||
|
context.getLocalServiceInstance().getHost()
|
||||||
|
);
|
||||||
|
beforeRequest.setRequestContext(requestContext);
|
||||||
|
assemblyAPI.beforeCallService(beforeRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly.server;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||||
|
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyServerExceptionHook.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyServerExceptionHook implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final AssemblyAPI assemblyAPI;
|
||||||
|
|
||||||
|
public AssemblyServerExceptionHook(AssemblyAPI assemblyAPI) {
|
||||||
|
this.assemblyAPI = assemblyAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Server.POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
AfterRequest afterRequest = new AfterRequest();
|
||||||
|
RequestContext requestContext = new AssemblyRequestContext(
|
||||||
|
context.getRequest(),
|
||||||
|
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||||
|
context.getLocalServiceInstance().getHost()
|
||||||
|
);
|
||||||
|
afterRequest.setRequestContext(requestContext);
|
||||||
|
AssemblyResponseContext responseContext = new AssemblyResponseContext();
|
||||||
|
responseContext.setThrowable(context.getThrowable());
|
||||||
|
responseContext.setRetStatus(PolarisEnhancedPluginUtils.getRetStatusFromRequest(null, null, context.getThrowable()));
|
||||||
|
afterRequest.setResponseContext(responseContext);
|
||||||
|
|
||||||
|
assemblyAPI.afterProcess(afterRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.rpc.enhancement.plugin.assembly.server;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.PolarisEnhancedPluginUtils;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyResponseContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||||
|
import com.tencent.polaris.assembly.api.pojo.AfterRequest;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyServerPostHook.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyServerPostHook implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final AssemblyAPI assemblyAPI;
|
||||||
|
|
||||||
|
public AssemblyServerPostHook(AssemblyAPI assemblyAPI) {
|
||||||
|
this.assemblyAPI = assemblyAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Server.POST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
AfterRequest afterRequest = new AfterRequest();
|
||||||
|
RequestContext requestContext = new AssemblyRequestContext(
|
||||||
|
context.getRequest(),
|
||||||
|
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||||
|
context.getLocalServiceInstance().getHost()
|
||||||
|
);
|
||||||
|
afterRequest.setRequestContext(requestContext);
|
||||||
|
AssemblyResponseContext responseContext = new AssemblyResponseContext(context.getResponse());
|
||||||
|
responseContext.setRetStatus(PolarisEnhancedPluginUtils.getRetStatusFromRequest(context.getResponse().getHttpHeaders(), context.getResponse().getHttpStatus(), null));
|
||||||
|
afterRequest.setResponseContext(responseContext);
|
||||||
|
|
||||||
|
assemblyAPI.afterProcess(afterRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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.plugin.assembly.server;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPlugin;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
|
||||||
|
import com.tencent.cloud.rpc.enhancement.plugin.assembly.AssemblyRequestContext;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||||
|
import com.tencent.polaris.api.rpc.RequestContext;
|
||||||
|
import com.tencent.polaris.assembly.api.AssemblyAPI;
|
||||||
|
import com.tencent.polaris.assembly.api.pojo.BeforeRequest;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssemblyServerPreHook.
|
||||||
|
*
|
||||||
|
* @author sean yu
|
||||||
|
*/
|
||||||
|
public class AssemblyServerPreHook implements EnhancedPlugin {
|
||||||
|
|
||||||
|
private final AssemblyAPI assemblyAPI;
|
||||||
|
|
||||||
|
public AssemblyServerPreHook(AssemblyAPI assemblyAPI) {
|
||||||
|
this.assemblyAPI = assemblyAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnhancedPluginType getType() {
|
||||||
|
return EnhancedPluginType.Server.PRE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(EnhancedPluginContext context) throws Throwable {
|
||||||
|
BeforeRequest beforeRequest = new BeforeRequest();
|
||||||
|
RequestContext requestContext = new AssemblyRequestContext(
|
||||||
|
context.getRequest(),
|
||||||
|
new ServiceKey(MetadataContext.LOCAL_NAMESPACE, context.getLocalServiceInstance().getServiceId()),
|
||||||
|
context.getLocalServiceInstance().getHost()
|
||||||
|
);
|
||||||
|
beforeRequest.setRequestContext(requestContext);
|
||||||
|
assemblyAPI.beforeProcess(beforeRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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.resttemplate;
|
|
||||||
|
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
|
||||||
import org.aspectj.lang.annotation.Around;
|
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Intercept for BlockingLoadBalancerClient, put host and port to thread local.
|
|
||||||
*
|
|
||||||
* @author lepdou 2022-09-05
|
|
||||||
*/
|
|
||||||
@Aspect
|
|
||||||
public class BlockingLoadBalancerClientAspect {
|
|
||||||
|
|
||||||
@Pointcut("execution(public * org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient.reconstructURI(..)) ")
|
|
||||||
public void pointcut() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Around("pointcut()")
|
|
||||||
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
||||||
LoadBalancerClientAspectUtils.extractLoadBalancerResult(joinPoint);
|
|
||||||
return joinPoint.proceed();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,92 +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 com.tencent.cloud.common.constant.HeaderConstant;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
import com.tencent.cloud.common.metadata.StaticMetadataManager;
|
|
||||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
|
||||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
|
||||||
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.Mock;
|
|
||||||
import org.mockito.MockedStatic;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
public class BlockingLoadBalancerClientAspectTest {
|
|
||||||
|
|
||||||
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
|
|
||||||
@Mock
|
|
||||||
private ProceedingJoinPoint proceedingJoinPoint;
|
|
||||||
|
|
||||||
private BlockingLoadBalancerClientAspect aspect = new BlockingLoadBalancerClientAspect();
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void beforeAll() {
|
|
||||||
mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class);
|
|
||||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
|
||||||
.thenReturn("unit-test");
|
|
||||||
ApplicationContext applicationContext = mock(ApplicationContext.class);
|
|
||||||
MetadataLocalProperties metadataLocalProperties = mock(MetadataLocalProperties.class);
|
|
||||||
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
|
|
||||||
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
|
|
||||||
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
|
|
||||||
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterAll
|
|
||||||
static void afterAll() {
|
|
||||||
mockedApplicationContextAwareUtils.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
MetadataContext.LOCAL_NAMESPACE = NAMESPACE_TEST;
|
|
||||||
MetadataContext.LOCAL_SERVICE = SERVICE_PROVIDER;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() throws Throwable {
|
|
||||||
ServiceInstance serviceInstance = mock(ServiceInstance.class);
|
|
||||||
doReturn("0.0.0.0").when(serviceInstance).getHost();
|
|
||||||
doReturn(80).when(serviceInstance).getPort();
|
|
||||||
doReturn(new Object[]{ serviceInstance }).when(proceedingJoinPoint).getArgs();
|
|
||||||
aspect.invoke(proceedingJoinPoint);
|
|
||||||
aspect.pointcut();
|
|
||||||
assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_HOST)).isEqualTo("0.0.0.0");
|
|
||||||
assertThat(MetadataContextHolder.get().getLoadbalancerMetadata().get(HeaderConstant.INTERNAL_CALLEE_INSTANCE_PORT)).isEqualTo("80");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in new issue