From 39e808b5837c26e237777eeefac0399efaceab3f Mon Sep 17 00:00:00 2001
From: "Zhiguo.Chen"
Date: Tue, 27 Jul 2021 17:48:49 +0800
Subject: [PATCH] Add ACL token support
---
.../datasource/consul/ConsulDataSource.java | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/sentinel-extension/sentinel-datasource-consul/src/main/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSource.java b/sentinel-extension/sentinel-datasource-consul/src/main/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSource.java
index 276bd853c4..445fe80d97 100644
--- a/sentinel-extension/sentinel-datasource-consul/src/main/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSource.java
+++ b/sentinel-extension/sentinel-datasource-consul/src/main/java/com/alibaba/csp/sentinel/datasource/consul/ConsulDataSource.java
@@ -21,6 +21,7 @@
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AssertUtil;
+import com.alibaba.csp.sentinel.util.StringUtil;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
@@ -43,12 +44,14 @@
*
*
* @author wavesZh
+ * @author Zhiguo.Chen
*/
public class ConsulDataSource extends AbstractDataSource {
private static final int DEFAULT_PORT = 8500;
private final String address;
+ private final String token;
private final String ruleKey;
/**
* Request of query will hang until timeout (in second) or get updated value.
@@ -83,12 +86,27 @@ public ConsulDataSource(String host, String ruleKey, int watchTimeoutInSecond, C
* @param watchTimeout request for querying data will be blocked until new data or timeout. The unit is second (s)
*/
public ConsulDataSource(String host, int port, String ruleKey, int watchTimeout, Converter parser) {
+ this(host, port, null, ruleKey, watchTimeout, parser);
+ }
+
+ /**
+ * Constructor of {@code ConsulDataSource}.
+ *
+ * @param parser customized data parser, cannot be empty
+ * @param host consul agent host
+ * @param port consul agent port
+ * @param token consul agent acl token
+ * @param ruleKey data key in Consul
+ * @param watchTimeout request for querying data will be blocked until new data or timeout. The unit is second (s)
+ */
+ public ConsulDataSource(String host, int port, String token, String ruleKey, int watchTimeout, Converter parser) {
super(parser);
AssertUtil.notNull(host, "Consul host can not be null");
AssertUtil.notEmpty(ruleKey, "Consul ruleKey can not be empty");
AssertUtil.isTrue(watchTimeout >= 0, "watchTimeout should not be negative");
this.client = new ConsulClient(host, port);
this.address = host + ":" + port;
+ this.token = token;
this.ruleKey = ruleKey;
this.watchTimeout = watchTimeout;
loadInitialConfig();
@@ -193,7 +211,11 @@ private Response getValueImmediately(String key) {
*/
private Response getValue(String key, long index, long waitTime) {
try {
- return client.getKVValue(key, new QueryParams(waitTime, index));
+ if (StringUtil.isNotBlank(token)) {
+ return client.getKVValue(key, token, new QueryParams(waitTime, index));
+ } else {
+ return client.getKVValue(key, new QueryParams(waitTime, index));
+ }
} catch (Throwable t) {
RecordLog.warn("[ConsulDataSource] Failed to get value for key: " + key, t);
}