Update deprecated code

pull/107/head
Oliver 3 years ago
parent 93c6710dd7
commit 5d7c2990ce

@ -363,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()); try {
return callServer(api, params, body, nacosDomain, method);
for (int i = 0; i < servers.size(); i++) { } catch (NacosException e) {
// 获取nacos所在的ip+port地址 exception = e;
String server = servers.get(index); if (NAMING_LOGGER.isDebugEnabled()) {
try { NAMING_LOGGER.debug("request {} failed.", nacosDomain, e);
// 进行请求 }
return callServer(api, params, body, server, method); }
} catch (NacosException e) { }
exception = e; } else {
if (NAMING_LOGGER.isDebugEnabled()) { Random random = new Random(System.currentTimeMillis());
NAMING_LOGGER.debug("request {} failed.", server, e); int index = random.nextInt(servers.size());
}
} for (int i = 0; i < servers.size(); i++) {
index = (index + 1) % servers.size(); String server = servers.get(index);
} try {
} return callServer(api, params, body, server, method);
} catch (NacosException e) {
if (StringUtils.isNotBlank(nacosDomain)) { exception = e;
for (int i = 0; i < UtilAndComs.REQUEST_DOMAIN_RETRY_COUNT; i++) { if (NAMING_LOGGER.isDebugEnabled()) {
try { NAMING_LOGGER.debug("request {} failed.", server, e);
return callServer(api, params, body, nacosDomain, method); }
} catch (NacosException e) { }
exception = e; index = (index + 1) % servers.size();
if (NAMING_LOGGER.isDebugEnabled()) { }
NAMING_LOGGER.debug("request {} failed.", nacosDomain, e); }
}
} NAMING_LOGGER.error("request: {} failed, servers: {}, code: {}, msg: {}", api, servers, exception.getErrCode(),
} exception.getErrMsg());
}
throw new NacosException(exception.getErrCode(),
NAMING_LOGGER.error("request: {} failed, servers: {}, code: {}, msg: {}", "failed to req API:" + api + " after all servers(" + servers + ") tried: " + exception.getMessage());
api, servers, exception.getErrCode(), exception.getErrMsg());
throw new NacosException(exception.getErrCode(), "failed to req API:/api/" + api + " after all servers(" + servers + ") tried: "
+ exception.getMessage());
}
``` ```
**学习点** **学习点**
@ -441,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);
@ -647,78 +646,42 @@ public Instance getInstance(String namespaceId, String serviceName, String clust
3. 组装结果返回 3. 组装结果返回
```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, @RequestParam(defaultValue = StringUtils.EMPTY) String ip,
JSONObject result = new JSONObject(); @RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String clusterName,
@RequestParam(defaultValue = "0") Integer port, @RequestParam(defaultValue = StringUtils.EMPTY) String beat)
result.put("clientBeatInterval", switchDomain.getClientBeatInterval()); throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, ObjectNode result = JacksonUtils.createEmptyJsonNode();
Constants.DEFAULT_NAMESPACE_ID); result.put(SwitchEntry.CLIENT_BEAT_INTERVAL, switchDomain.getClientBeatInterval());
String clusterName = WebUtils.optional(request, CommonParams.CLUSTER_NAME, RsInfo clientBeat = null;
UtilsAndCommons.DEFAULT_CLUSTER_NAME); if (StringUtils.isNotBlank(beat)) {
String ip = WebUtils.optional(request, "ip", StringUtils.EMPTY); clientBeat = JacksonUtils.toObj(beat, RsInfo.class);
int port = Integer.parseInt(WebUtils.optional(request, "port", "0")); }
String beat = WebUtils.optional(request, "beat", StringUtils.EMPTY); if (clientBeat != null) {
if (StringUtils.isNotBlank(clientBeat.getCluster())) {
RsInfo clientBeat = null;
if (StringUtils.isNotBlank(beat)) {
clientBeat = JSON.parseObject(beat, RsInfo.class);
}
if (clientBeat != null) {
if (StringUtils.isNotBlank(clientBeat.getCluster())) {
clusterName = clientBeat.getCluster(); clusterName = clientBeat.getCluster();
} } else {
ip = clientBeat.getIp(); // fix #2533
port = clientBeat.getPort(); clientBeat.setCluster(clusterName);
} }
ip = clientBeat.getIp();
if (Loggers.SRV_LOG.isDebugEnabled()) { port = clientBeat.getPort();
Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}", clientBeat, serviceName); }
}
// 获取实例 NamingUtils.checkServiceNameFormat(serviceName);
Instance instance = serviceManager.getInstance(namespaceId, serviceName, clusterName, ip, port); Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}, namespaceId: {}", clientBeat,
serviceName, namespaceId);
if (instance == null) { BeatInfoInstanceBuilder builder = BeatInfoInstanceBuilder.newBuilder();
if (clientBeat == null) { int resultCode = instanceServiceV2
result.put(CommonParams.CODE, NamingResponseCode.RESOURCE_NOT_FOUND); .handleBeat(namespaceId, serviceName, ip, port, clusterName, clientBeat, builder);
result.put(CommonParams.CODE, resultCode);
result.put(SwitchEntry.CLIENT_BEAT_INTERVAL,
instanceServiceV2.getHeartBeatInterval(namespaceId, serviceName, ip, port, clusterName));
result.put(SwitchEntry.LIGHT_BEAT_ENABLED, switchDomain.isLightBeatEnabled());
return result; 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());
return result;
}
``` ```

Loading…
Cancel
Save