Skip to content

Commit

Permalink
make consul registry suppor ACL (apache#6313)
Browse files Browse the repository at this point in the history
* make consul registry suppor ACL
  • Loading branch information
tswstarplanet authored Jun 18, 2020
1 parent c4f11fc commit 1e51703
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static org.apache.dubbo.registry.consul.AbstractConsulRegistry.SERVICE_TAG;
import static org.apache.dubbo.registry.consul.AbstractConsulRegistry.URL_META_KEY;
import static org.apache.dubbo.registry.consul.AbstractConsulRegistry.WATCH_TIMEOUT;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;

/**
* registry center implementation for consul
Expand All @@ -78,10 +79,15 @@ public class ConsulRegistry extends FailbackRegistry {
new NamedThreadFactory("dubbo-consul-notifier", true));
private ConcurrentMap<URL, ConsulNotifier> notifiers = new ConcurrentHashMap<>();
private ScheduledExecutorService ttlConsulCheckExecutor;
/**
* The ACL token
*/
private String token;


public ConsulRegistry(URL url) {
super(url);
token = url.getParameter(TOKEN_KEY, (String) null);
String host = url.getHost();
int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT;
client = new ConsulClient(host, port);
Expand All @@ -102,7 +108,11 @@ public void register(URL url) {

@Override
public void doRegister(URL url) {
client.agentServiceRegister(buildService(url));
if (token == null) {
client.agentServiceRegister(buildService(url));
} else {
client.agentServiceRegister(buildService(url), token);
}
}

@Override
Expand All @@ -116,7 +126,11 @@ public void unregister(URL url) {

@Override
public void doUnregister(URL url) {
client.agentServiceDeregister(buildId(url));
if (token == null) {
client.agentServiceDeregister(buildId(url));
} else {
client.agentServiceDeregister(buildId(url), token);
}
}

@Override
Expand Down Expand Up @@ -198,12 +212,16 @@ private void checkPass() {
for (URL url : getRegistered()) {
String checkId = buildId(url);
try {
client.agentCheckPass("service:" + checkId);
if (token == null) {
client.agentCheckPass("service:" + checkId);
} else {
client.agentCheckPass("service:" + checkId, null, token);
}
if (logger.isDebugEnabled()) {
logger.debug("check pass for url: " + url + " with check id: " + checkId);
}
} catch (Throwable t) {
logger.warn("fail to check pass for url: " + url + ", check id is: " + checkId);
logger.warn("fail to check pass for url: " + url + ", check id is: " + checkId, t);
}
}
}
Expand All @@ -213,13 +231,15 @@ private Response<List<HealthService>> getHealthServices(String service, long ind
.setTag(SERVICE_TAG)
.setQueryParams(new QueryParams(watchTimeout, index))
.setPassing(true)
.setToken(token)
.build();
return client.getHealthServices(service, request);
}

private Response<Map<String, List<String>>> getAllServices(long index, int watchTimeout) {
CatalogServicesRequest request = CatalogServicesRequest.newBuilder()
.setQueryParams(new QueryParams(watchTimeout, index))
.setToken(token)
.build();
return client.getCatalogServices(request);
}
Expand Down

0 comments on commit 1e51703

Please sign in to comment.