bugfix: optimize ratelimit actuator (#413)

pull/428/head
weihubeats 2 years ago committed by GitHub
parent 9432cdbcb0
commit d99e99583c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,3 +1,4 @@
# Change Log # Change Log
--- ---
- [Bugfix: optimize ratelimit actuator](https://github.com/Tencent/spring-cloud-tencent/pull/413)

@ -22,11 +22,16 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.tencent.cloud.common.util.JacksonUtils;
import com.tencent.cloud.polaris.context.ServiceRuleManager; import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties; import com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties;
import com.tencent.polaris.client.pb.RateLimitProto; import com.tencent.polaris.client.pb.RateLimitProto;
import com.tencent.polaris.client.pb.RoutingProto; import com.tencent.polaris.client.pb.RoutingProto;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
@ -42,6 +47,8 @@ import org.springframework.util.CollectionUtils;
@Endpoint(id = "polaris-ratelimit") @Endpoint(id = "polaris-ratelimit")
public class PolarisRateLimitRuleEndpoint { public class PolarisRateLimitRuleEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(PolarisRateLimitRuleEndpoint.class);
private final ServiceRuleManager serviceRuleManager; private final ServiceRuleManager serviceRuleManager;
private final PolarisRateLimitProperties polarisRateLimitProperties; private final PolarisRateLimitProperties polarisRateLimitProperties;
@ -74,26 +81,15 @@ public class PolarisRateLimitRuleEndpoint {
} }
for (RateLimitProto.Rule rule : rateLimit.getRulesList()) { for (RateLimitProto.Rule rule : rateLimit.getRulesList()) {
Map<String, Object> ruleMap = new HashMap<>(); String ruleJson = "";
ruleMap.put("id", rule.getId()); try {
ruleMap.put("priority", rule.getPriority()); ruleJson = JsonFormat.printer().print(rule);
ruleMap.put("resource", rule.getResource()); }
ruleMap.put("type", rule.getType()); catch (InvalidProtocolBufferException e) {
ruleMap.put("labels", rule.getLabelsMap()); LOG.error("rule to Json failed. check rule {}.", rule, e);
ruleMap.put("amounts", rule.getAmountsList()); throw new RuntimeException("Json failed.", e);
ruleMap.put("action", rule.getAction()); }
ruleMap.put("disable", rule.getDisable()); rateLimitRule.add(JacksonUtils.deserialize2Map(ruleJson));
ruleMap.put("report", rule.getReport());
ruleMap.put("create_time", rule.getCtime());
ruleMap.put("modify_time", rule.getMtime());
ruleMap.put("revision", rule.getRevision());
ruleMap.put("service_token", rule.getServiceToken());
ruleMap.put("adjuster", rule.getAdjuster());
ruleMap.put("regex_combine", rule.getRegexCombine());
ruleMap.put("amount_mode", rule.getAmountMode());
ruleMap.put("failover", rule.getFailover());
ruleMap.put("cluster", rule.getCluster());
rateLimitRule.add(ruleMap);
} }
return rateLimitRule; return rateLimitRule;
} }

Loading…
Cancel
Save