parent
67d30e94cb
commit
b9ebbcdada
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.ratelimit.utils;
|
||||||
|
|
||||||
|
import com.tencent.polaris.api.plugin.ratelimiter.QuotaResult;
|
||||||
|
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
|
||||||
|
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
|
||||||
|
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utils for quota checking
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class QuotaCheckUtils {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckUtils.class);
|
||||||
|
|
||||||
|
public static QuotaResponse getQuota(LimitAPI limitAPI, String namespace, String service, int count,
|
||||||
|
Map<String, String> labels, String method) {
|
||||||
|
// build quota request
|
||||||
|
QuotaRequest quotaRequest = new QuotaRequest();
|
||||||
|
quotaRequest.setNamespace(namespace);
|
||||||
|
quotaRequest.setService(service);
|
||||||
|
quotaRequest.setCount(count);
|
||||||
|
quotaRequest.setLabels(labels);
|
||||||
|
quotaRequest.setMethod(method);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return limitAPI.getQuota(quotaRequest);
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
LOG.error("fail to invoke getQuota of LimitAPI with QuotaRequest[{}].", quotaRequest, throwable);
|
||||||
|
return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, "get quota failed"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
4
spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/MetadataScgFilter.java → spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/Metadata2HeaderScgFilter.java
4
spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/MetadataScgFilter.java → spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/Metadata2HeaderScgFilter.java
2
spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataZuulFilter.java → spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/Metadata2HeaderZuulFilter.java
2
spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataZuulFilter.java → spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/Metadata2HeaderZuulFilter.java
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* 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.gateway.core.zuul.filter;
|
||||||
|
|
||||||
|
import com.netflix.zuul.ZuulFilter;
|
||||||
|
import com.netflix.zuul.context.RequestContext;
|
||||||
|
import com.tencent.cloud.metadata.constant.MetadataConstant;
|
||||||
|
import com.tencent.cloud.metadata.context.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 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 对端命名空间暂时与本地命名空间相同
|
||||||
|
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
|
||||||
|
MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue