diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c1f8175..5055cb3c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # Change Log --- +- [Bugfix: optimize ratelimit actuator](https://github.com/Tencent/spring-cloud-tencent/pull/419) \ No newline at end of file diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java index 3ea4f5af8..b58a0755c 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/endpoint/PolarisRateLimitRuleEndpoint.java @@ -22,11 +22,16 @@ import java.util.HashMap; import java.util.List; 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.ratelimit.config.PolarisRateLimitProperties; import com.tencent.polaris.client.pb.RateLimitProto; import com.tencent.polaris.client.pb.RoutingProto; 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.ReadOperation; @@ -42,6 +47,8 @@ import org.springframework.util.CollectionUtils; @Endpoint(id = "polaris-ratelimit") public class PolarisRateLimitRuleEndpoint { + private static final Logger LOG = LoggerFactory.getLogger(PolarisRateLimitRuleEndpoint.class); + private final ServiceRuleManager serviceRuleManager; private final PolarisRateLimitProperties polarisRateLimitProperties; @@ -74,26 +81,15 @@ public class PolarisRateLimitRuleEndpoint { } for (RateLimitProto.Rule rule : rateLimit.getRulesList()) { - Map ruleMap = new HashMap<>(); - ruleMap.put("id", rule.getId()); - ruleMap.put("priority", rule.getPriority()); - ruleMap.put("resource", rule.getResource()); - ruleMap.put("type", rule.getType()); - ruleMap.put("labels", rule.getLabelsMap()); - ruleMap.put("amounts", rule.getAmountsList()); - ruleMap.put("action", rule.getAction()); - ruleMap.put("disable", rule.getDisable()); - 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); + String ruleJson = ""; + try { + ruleJson = JsonFormat.printer().print(rule); + } + catch (InvalidProtocolBufferException e) { + LOG.error("rule to Json failed. check rule {}.", rule, e); + throw new RuntimeException("Json failed.", e); + } + rateLimitRule.add(JacksonUtils.deserialize2Map(ruleJson)); } return rateLimitRule; }