fix: update out-date code (#107)

pull/108/head
Oliver 3 years ago committed by GitHub
parent 6804465d52
commit 3ba1bb6d32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -301,18 +301,18 @@ class BeatTask implements Runnable {
try { try {
// 与nacos进行一次rest请求交互 // 与nacos进行一次rest请求交互
JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled); JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
long interval = result.getIntValue("clientBeatInterval"); long interval = result.get(CLIENT_BEAT_INTERVAL_FIELD).asLong();
boolean lightBeatEnabled = false; boolean lightBeatEnabled = false;
if (result.containsKey(CommonParams.LIGHT_BEAT_ENABLED)) { if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) {
lightBeatEnabled = result.getBooleanValue(CommonParams.LIGHT_BEAT_ENABLED); lightBeatEnabled = result.get(CommonParams.LIGHT_BEAT_ENABLED).asBoolean();
} }
BeatReactor.this.lightBeatEnabled = lightBeatEnabled; BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
if (interval > 0) { if (interval > 0) {
nextTime = interval; nextTime = interval;
} }
int code = NamingResponseCode.OK; int code = NamingResponseCode.OK;
if (result.containsKey(CommonParams.CODE)) { if (result.has(CommonParams.CODE)) {
code = result.getIntValue(CommonParams.CODE); code = result.get(CommonParams.CODE).asInt();
} }
// 如果nacos找不到当前实例, // 如果nacos找不到当前实例,
if (code == NamingResponseCode.RESOURCE_NOT_FOUND) { if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {
@ -336,10 +336,14 @@ class BeatTask implements Runnable {
NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}", NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg()); JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg());
} } catch (Exception unknownEx) {
NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, unknown exception msg: {}",
JacksonUtils.toJson(beatInfo), unknownEx.getMessage(), unknownEx);
} finally {
executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
} }
} }
}
``` ```
- 定时任务说明 - 定时任务说明
@ -359,47 +363,41 @@ class BeatTask implements Runnable {
NacosException exception = new NacosException(); NacosException exception = new NacosException();
if (servers != null && !servers.isEmpty()) { if (serverListManager.isDomain()) {
String nacosDomain = serverListManager.getNacosDomain();
Random random = new Random(System.currentTimeMillis()); for (int i = 0; i < maxRetry; i++) {
int index = random.nextInt(servers.size());
for (int i = 0; i < servers.size(); i++) {
// 获取nacos所在的ip+port地址
String server = servers.get(index);
try { try {
// 进行请求 return callServer(api, params, body, nacosDomain, method);
return callServer(api, params, body, server, method);
} catch (NacosException e) { } catch (NacosException e) {
exception = e; exception = e;
if (NAMING_LOGGER.isDebugEnabled()) { if (NAMING_LOGGER.isDebugEnabled()) {
NAMING_LOGGER.debug("request {} failed.", server, e); NAMING_LOGGER.debug("request {} failed.", nacosDomain, e);
}
} }
index = (index + 1) % servers.size();
} }
} }
} else {
Random random = new Random(System.currentTimeMillis());
int index = random.nextInt(servers.size());
if (StringUtils.isNotBlank(nacosDomain)) { for (int i = 0; i < servers.size(); i++) {
for (int i = 0; i < UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT; i++) { String server = servers.get(index);
try { try {
return callServer(api, params, body, nacosDomain, method); return callServer(api, params, body, server, method);
} catch (NacosException e) { } catch (NacosException e) {
exception = e; exception = e;
if (NAMING_LOGGER.isDebugEnabled()) { if (NAMING_LOGGER.isDebugEnabled()) {
NAMING_LOGGER.debug("request {} failed.", nacosDomain, e); NAMING_LOGGER.debug("request {} failed.", server, e);
} }
} }
index = (index + 1) % servers.size();
} }
} }
NAMING_LOGGER.error("request: {} failed, servers: {}, code: {}, msg: {}", NAMING_LOGGER.error("request: {} failed, servers: {}, code: {}, msg: {}", api, servers, exception.getErrCode(),
api, servers, exception.getErrCode(), exception.getErrMsg()); exception.getErrMsg());
throw new NacosException(exception.getErrCode(), "failed to req API:/api/" + api + " after all servers(" + servers + ") tried: " throw new NacosException(exception.getErrCode(),
+ exception.getMessage()); "failed to req API:" + api + " after all servers(" + servers + ") tried: " + exception.getMessage());
}
``` ```
**学习点** **学习点**
@ -437,19 +435,24 @@ public void registerService(String serviceName, String groupName, Instance insta
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}",
namespaceId, serviceName, instance); namespaceId, serviceName, instance);
String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
if (instance.isEphemeral()) {
BeatInfo beatInfo = beatReactor.buildBeatInfo(groupedServiceName, instance);
beatReactor.addBeatInfo(groupedServiceName, beatInfo);
}
final Map<String, String> params = new HashMap<String, String>(9); final Map<String, String> params = new HashMap<String, String>(32);
params.put(CommonParams.NAMESPACE_ID, namespaceId); params.put(CommonParams.NAMESPACE_ID, namespaceId);
params.put(CommonParams.SERVICE_NAME, serviceName); params.put(CommonParams.SERVICE_NAME, groupedServiceName);
params.put(CommonParams.GROUP_NAME, groupName); params.put(CommonParams.GROUP_NAME, groupName);
params.put(CommonParams.CLUSTER_NAME, instance.getClusterName()); params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
params.put("ip", instance.getIp()); params.put(IP_PARAM, instance.getIp());
params.put("port", String.valueOf(instance.getPort())); params.put(PORT_PARAM, String.valueOf(instance.getPort()));
params.put("weight", String.valueOf(instance.getWeight())); params.put(WEIGHT_PARAM, String.valueOf(instance.getWeight()));
params.put("enable", String.valueOf(instance.isEnabled())); params.put(REGISTER_ENABLE_PARAM, String.valueOf(instance.isEnabled()));
params.put("healthy", String.valueOf(instance.isHealthy())); params.put(HEALTHY_PARAM, String.valueOf(instance.isHealthy()));
params.put("ephemeral", String.valueOf(instance.isEphemeral())); params.put(EPHEMERAL_PARAM, String.valueOf(instance.isEphemeral()));
params.put("metadata", JSON.toJSONString(instance.getMetadata())); params.put(META_PARAM, JacksonUtils.toJson(instance.getMetadata()));
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST); reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST);
@ -645,75 +648,40 @@ public Instance getInstance(String namespaceId, String serviceName, String clust
```java ```java
@CanDistro @CanDistro
@PutMapping("/beat") @PutMapping("/beat")
@Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE) @Secured(action = ActionTypes.WRITE)
public JSONObject beat(HttpServletRequest request) throws Exception { public ObjectNode beat(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
@RequestParam String serviceName,
JSONObject result = new JSONObject(); @RequestParam(defaultValue = StringUtils.EMPTY) String ip,
@RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName,
result.put("clientBeatInterval", switchDomain.getClientBeatInterval()); @RequestParam(defaultValue = "0") Integer port,
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); @RequestParam(defaultValue = StringUtils.EMPTY) String beat)throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID); ObjectNode result = JacksonUtils.createEmptyJsonNode();
String clusterName = WebUtils.optional(request, CommonParams.CLUSTER_NAME, result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
UtilsAndCommons.DEFAULT_CLUSTER_NAME);
String ip = WebUtils.optional(request, "ip", StringUtils.EMPTY);
int port = Integer.parseInt(WebUtils.optional(request, "port", "0"));
String beat = WebUtils.optional(request, "beat", StringUtils.EMPTY);
RsInfo clientBeat = null; RsInfo clientBeat = null;
if (StringUtils.isNotBlank(beat)) { if (StringUtils.isNotBlank(beat)) {
clientBeat = JSON.parseObject(beat, RsInfo.class); clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
} }
if (clientBeat != null) { if (clientBeat != null) {
if (StringUtils.isNotBlank(clientBeat.getCluster())) { if (StringUtils.isNotBlank(clientBeat.getCluster())) {
clusterName = clientBeat.getCluster(); clusterName = clientBeat.getCluster();
} else {
// fix #2533
clientBeat.setCluster(clusterName);
} }
ip = clientBeat.getIp(); ip = clientBeat.getIp();
port = clientBeat.getPort(); port = clientBeat.getPort();
} }
if (Loggers.SRV_LOG.isDebugEnabled()) { NamingUtils.checkServiceNameFormat(serviceName);
Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}", clientBeat, serviceName); Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat,
} serviceName, namespaceId);
// 获取实例 BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
Instance instance = serviceManager.getInstance(namespaceId, serviceName, clusterName, ip, port); int resultCode = instanceServiceV2
.handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
if (instance == null) { result.put(CommonParams.CODE, resultCode);
if (clientBeat == null) { result.put(SwitchEntry.CLIENT_BEAT_INTERVAL,
result.put(CommonParams.CODE, NamingResponseCode.RESOURCE_NOT_FOUND); instanceServiceV2.getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));
return result;
}
instance = new Instance();
instance.setPort(clientBeat.getPort());
instance.setIp(clientBeat.getIp());
instance.setWeight(clientBeat.getWeight());
instance.setMetadata(clientBeat.getMetadata());
instance.setClusterName(clusterName);
instance.setServiceName(serviceName);
instance.setInstanceId(instance.getInstanceId());
instance.setEphemeral(clientBeat.isEphemeral());
serviceManager.registerInstance(namespaceId, serviceName, instance);
}
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
throw new NacosException(NacosException.SERVER_ERROR,
"service not found: " + serviceName + "@" + namespaceId);
}
if (clientBeat == null) {
clientBeat = new RsInfo();
clientBeat.setIp(ip);
clientBeat.setPort(port);
clientBeat.setCluster(clusterName);
}
// 处理心跳方法
service.processClientBeat(clientBeat);
result.put(CommonParams.CODE, NamingResponseCode.OK);
result.put("clientBeatInterval", instance.getInstanceHeartBeatInterval());
result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled()); result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
return result; return result;
} }

Loading…
Cancel
Save