fix wrong context data storage. (#180)
parent
fb0fbd7357
commit
a98f904fd2
@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.common.metadata.aop;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import feign.Request;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
|
||||
/**
|
||||
* Aspect for getting service name of peer-service in Feign of Greenwich.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Aspect
|
||||
public class MetadataFeignAspect {
|
||||
|
||||
@Pointcut("execution(* feign.Client.execute(..))")
|
||||
public void execute() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service name before execution of Feign client.
|
||||
* @param joinPoint join point
|
||||
*/
|
||||
@Before("execute()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
Request request = (Request) joinPoint.getArgs()[0];
|
||||
MetadataContextHolder.get().putSystemMetadata(
|
||||
MetadataConstant.SystemMetadataKey.PEER_SERVICE,
|
||||
URI.create(request.url()).getAuthority());
|
||||
}
|
||||
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package com.tencent.cloud.common.metadata.filter.gateway;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.REQUEST_URI_KEY;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY;
|
||||
|
||||
/**
|
||||
* Zuul output first filter used for setting peer info in context.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class MetadataFirstZuulFilter extends ZuulFilter {
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return PRE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return PRE_DECORATION_FILTER_ORDER + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
// get request context
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
|
||||
// TODO The peer namespace is temporarily the same as the local namespace
|
||||
MetadataContextHolder.get().putSystemMetadata(
|
||||
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
|
||||
MetadataContext.LOCAL_NAMESPACE);
|
||||
MetadataContextHolder.get().putSystemMetadata(
|
||||
MetadataConstant.SystemMetadataKey.PEER_SERVICE,
|
||||
(String) requestContext.get(SERVICE_ID_KEY));
|
||||
MetadataContextHolder.get().putSystemMetadata(
|
||||
MetadataConstant.SystemMetadataKey.PEER_PATH,
|
||||
(String) requestContext.get(REQUEST_URI_KEY));
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.common.metadata.interceptor.feign;
|
||||
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* Interceptor used for setting peer info in context.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class MetadataFirstFeignInterceptor implements RequestInterceptor, Ordered {
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return MetadataConstant.OrderConstant.METADATA_FIRST_FEIGN_INTERCEPTOR_ORDER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
// get metadata of current thread
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
|
||||
// TODO The peer namespace is temporarily the same as the local namespace
|
||||
metadataContext.putSystemMetadata(
|
||||
MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
|
||||
MetadataContext.LOCAL_NAMESPACE);
|
||||
// Cannot get service name of peer-service in Feign interceptor of Greenwich.
|
||||
// metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
|
||||
// requestTemplate.feignTarget().name());
|
||||
metadataContext.putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,
|
||||
requestTemplate.path());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue