Skip to content

Commit

Permalink
fix:fix consul token read null bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman committed Aug 4, 2023
1 parent 360b843 commit 49030e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import com.ecwid.consul.v1.health.HealthServicesRequest;
import com.ecwid.consul.v1.health.model.HealthService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.polaris.api.config.global.ServerConnectorConfig;
import com.tencent.polaris.api.config.plugin.DefaultPlugins;
Expand Down Expand Up @@ -62,7 +63,6 @@
import com.tencent.polaris.plugins.connector.common.DestroyableServerConnector;
import com.tencent.polaris.plugins.connector.common.ServiceInstancesResponse;
import com.tencent.polaris.plugins.connector.common.ServiceUpdateTask;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant;
import org.slf4j.Logger;

import java.util.ArrayList;
Expand Down Expand Up @@ -148,8 +148,8 @@ public void init(InitContext ctx) throws PolarisException {
if (CollectionUtils.isNotEmpty(serverConnectorConfigs)) {
for (ServerConnectorConfigImpl serverConnectorConfig : serverConnectorConfigs) {
if (DefaultPlugins.SERVER_CONNECTOR_CONSUL.equals(serverConnectorConfig.getProtocol())) {
initActually(ctx, serverConnectorConfig);
mapper = new ObjectMapper();
initActually(ctx, serverConnectorConfig);
}
}
}
Expand All @@ -169,6 +169,7 @@ private void initActually(InitContext ctx, ServerConnectorConfig connectorConfig
int lastIndex = address.lastIndexOf(":");
String agentHost = address.substring(0, lastIndex);
int agentPort = Integer.parseInt(address.substring(lastIndex + 1));
LOG.debug("Consul Server : [" + address + "]");
consulRawClient = new ConsulRawClient(agentHost, agentPort);
consulClient = new ConsulClient(consulRawClient);

Expand Down Expand Up @@ -226,19 +227,28 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<St
NewService service = buildRegisterInstanceRequest(req);
String json = getGson().toJson(service);
HttpResponse rawResponse;
if (req.getMetadata().containsKey(ConsulConstant.MetadataMapKey.TOKEN_KEY)) {
String token = req.getMetadata().get(ConsulConstant.MetadataMapKey.TOKEN_KEY);
if (StringUtils.isNotBlank(req.getToken())) {
String token = req.getToken();
UrlParameters tokenParam = token != null ? new SingleUrlParameters("token", token) : null;
rawResponse = consulRawClient.makePutRequest("/v1/agent/service/register", json,
tokenParam);
} else {
rawResponse = consulRawClient.makePutRequest("/v1/agent/service/register", json);
}
if (rawResponse.getStatusCode() != 200) {
try {
LOG.warn(new ObjectMapper().writeValueAsString(rawResponse));
} catch (JsonProcessingException ignore) {
}
throw new OperationException(rawResponse);
}
CommonProviderResponse resp = new CommonProviderResponse();
consulContext.setInstanceId(service.getId());
String checkId = consulContext.getInstanceId();
if (!checkId.startsWith("service:")) {
checkId = "service:" + checkId;
}
consulContext.setCheckId(checkId);
resp.setInstanceID(service.getId());
resp.setExists(false);
LOG.info("Registered service to Consul: " + service);
Expand Down Expand Up @@ -304,7 +314,7 @@ public void deregisterInstance(CommonProviderRequest req) throws PolarisExceptio
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
LOG.info("Unregistering service to Consul: " + consulContext.getInstanceId());
this.consulClient.agentServiceDeregister(consulContext.getInstanceId());
this.consulClient.agentServiceDeregister(consulContext.getInstanceId(), req.getToken());
LOG.info("Unregistered service to Consul: " + consulContext.getInstanceId());
ieRegistered = false;
} catch (ConsulException e) {
Expand All @@ -320,9 +330,9 @@ public void heartbeat(CommonProviderRequest req) throws PolarisException {
if (ieRegistered) {
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
try {
this.consulClient.agentCheckPass("service:" + consulContext.getInstanceId());
this.consulClient.agentCheckPass(consulContext.getCheckId(), null, req.getToken());
if (LOG.isDebugEnabled()) {
LOG.debug("Heartbeat service to Consul: " + consulContext.getInstanceId());
LOG.debug("Heartbeat service to Consul: " + consulContext.getCheckId());
}
} catch (ConsulException e) {
throw new RetriableException(ErrorCode.NETWORK_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class ConsulContext {

private NewService.Check check;

private String checkId;

public ConsulContext() {
serviceName = "";
instanceId = "";
Expand Down Expand Up @@ -108,4 +110,12 @@ public NewService.Check getCheck() {
public void setCheck(NewService.Check check) {
this.check = check;
}

public String getCheckId() {
return checkId;
}

public void setCheckId(String checkId) {
this.checkId = checkId;
}
}

0 comments on commit 49030e2

Please sign in to comment.