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 693cc8d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
import com.tencent.polaris.client.api.ServiceCallResultListener;
import com.tencent.polaris.client.util.Utils;
import com.tencent.polaris.logging.LoggerFactory;
import org.slf4j.Logger;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;

public class DefaultDiscoveryFlow implements DiscoveryFlow {

Expand Down Expand Up @@ -151,7 +152,7 @@ private InstanceRegisterResponse doRegister(InstanceRegisterRequest req, Map<Str
CommonProviderRequest request = req.getRequest();
try {
CommonProviderResponse response = serverConnector.registerInstance(request, customHeader);
LOG.info("register {}/{} instance {} succ", req.getNamespace(), req.getService(),
LOG.info("register {}/{} instance {} successfully", req.getNamespace(), req.getService(),
response.getInstanceID());
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
Expand Down Expand Up @@ -189,7 +190,7 @@ public void deRegister(InstanceDeregisterRequest req) {
serverConnector.deregisterInstance(request);
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
LOG.info("deregister instance {} succ", req);
LOG.info("deregister instance {} successfully", req);
return;
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
Expand Down Expand Up @@ -288,8 +289,8 @@ private void reportInvokeStat(ServiceCallResult req) throws PolarisException {
* 上报内部服务调用结果
*
* @param serviceCallResult 服务调用结果
* @param targetServer 目标服务端
* @param method 方法
* @param targetServer 目标服务端
* @param method 方法
*/
public void reportServerCall(ServiceCallResult serviceCallResult, TargetServer targetServer, String method) {
if (null != targetServer) {
Expand All @@ -303,10 +304,10 @@ public void reportServerCall(ServiceCallResult serviceCallResult, TargetServer t
reportInvokeStat(serviceCallResult);
}

@Override
public void destroy() {
if (registerFlow != null) {
registerFlow.destroy();
}
}
@Override
public void destroy() {
if (registerFlow != null) {
registerFlow.destroy();
}
}
}
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 693cc8d

Please sign in to comment.