Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ACL token support for Consul data-source #2307

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,12 +44,14 @@
* </p>
*
* @author wavesZh
* @author Zhiguo.Chen
*/
public class ConsulDataSource<T> extends AbstractDataSource<String, T> {

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.
Expand Down Expand Up @@ -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<String, T> 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<String, T> 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();
Expand Down Expand Up @@ -193,7 +211,11 @@ private Response<GetValue> getValueImmediately(String key) {
*/
private Response<GetValue> 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);
}
Expand Down