Replace fastjson with jackson for nacos-client
This commit is contained in:
parent
ff6e09e58d
commit
5b9780e2fc
@ -34,7 +34,6 @@ import com.alibaba.nacos.client.naming.net.NamingProxy;
|
||||
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||
import com.alibaba.nacos.client.naming.utils.InitUtils;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import com.alibaba.nacos.client.security.SecurityProxy;
|
||||
import com.alibaba.nacos.client.utils.ValidatorUtils;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -44,7 +43,6 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Nacos Naming Service
|
||||
|
@ -15,13 +15,14 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.backups;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
||||
import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil;
|
||||
import com.alibaba.nacos.client.naming.cache.DiskCache;
|
||||
import com.alibaba.nacos.client.naming.core.HostReactor;
|
||||
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@ -180,7 +181,7 @@ public class FailoverReactor {
|
||||
String json;
|
||||
if ((json = reader.readLine()) != null) {
|
||||
try {
|
||||
dom = JSON.parseObject(json, ServiceInfo.class);
|
||||
dom = JacksonUtils.toObj(json, ServiceInfo.class);
|
||||
} catch (Exception e) {
|
||||
NAMING_LOGGER.error("[NA] error while parsing cached dom : " + json, e);
|
||||
}
|
||||
|
@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.beat;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -36,7 +34,17 @@ public class BeatInfo {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.toJSONString(this);
|
||||
return "BeatInfo{" +
|
||||
"port=" + port +
|
||||
", ip='" + ip + '\'' +
|
||||
", weight=" + weight +
|
||||
", serviceName='" + serviceName + '\'' +
|
||||
", cluster='" + cluster + '\'' +
|
||||
", metadata=" + metadata +
|
||||
", scheduled=" + scheduled +
|
||||
", period=" + period +
|
||||
", stopped=" + stopped +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
|
@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.beat;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.CommonParams;
|
||||
@ -26,6 +24,8 @@ import com.alibaba.nacos.api.naming.utils.NamingUtils;
|
||||
import com.alibaba.nacos.client.monitor.MetricsMonitor;
|
||||
import com.alibaba.nacos.client.naming.net.NamingProxy;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
@ -43,7 +43,7 @@ public class BeatReactor {
|
||||
|
||||
private boolean lightBeatEnabled = false;
|
||||
|
||||
public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>();
|
||||
public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<>();
|
||||
|
||||
public BeatReactor(NamingProxy serverProxy) {
|
||||
this(serverProxy, UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
|
||||
@ -105,19 +105,19 @@ public class BeatReactor {
|
||||
}
|
||||
long nextTime = beatInfo.getPeriod();
|
||||
try {
|
||||
JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
|
||||
long interval = result.getIntValue("clientBeatInterval");
|
||||
JsonNode result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
|
||||
long interval = result.get("clientBeatInterval").asInt();
|
||||
boolean lightBeatEnabled = false;
|
||||
if (result.containsKey(CommonParams.LIGHT_BEAT_ENABLED)) {
|
||||
lightBeatEnabled = result.getBooleanValue(CommonParams.LIGHT_BEAT_ENABLED);
|
||||
if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) {
|
||||
lightBeatEnabled = result.get(CommonParams.LIGHT_BEAT_ENABLED).asBoolean();
|
||||
}
|
||||
BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
|
||||
if (interval > 0) {
|
||||
nextTime = interval;
|
||||
}
|
||||
int code = NamingResponseCode.OK;
|
||||
if (result.containsKey(CommonParams.CODE)) {
|
||||
code = result.getIntValue(CommonParams.CODE);
|
||||
if (result.has(CommonParams.CODE)) {
|
||||
code = result.get(CommonParams.CODE).asInt();
|
||||
}
|
||||
if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {
|
||||
Instance instance = new Instance();
|
||||
@ -137,7 +137,7 @@ public class BeatReactor {
|
||||
}
|
||||
} catch (NacosException ne) {
|
||||
NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
|
||||
JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg());
|
||||
JacksonUtils.toJson(beatInfo), ne.getErrCode(), ne.getErrMsg());
|
||||
|
||||
}
|
||||
executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
|
||||
|
@ -15,11 +15,12 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.cache;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
||||
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@ -58,7 +59,7 @@ public class DiskCache {
|
||||
String json = dom.getJsonFromServer();
|
||||
|
||||
if (StringUtils.isEmpty(json)) {
|
||||
json = JSON.toJSONString(dom);
|
||||
json = JacksonUtils.toJson(dom);
|
||||
}
|
||||
|
||||
keyContentBuffer.append(json);
|
||||
@ -112,10 +113,10 @@ public class DiskCache {
|
||||
continue;
|
||||
}
|
||||
|
||||
newFormat = JSON.parseObject(json, ServiceInfo.class);
|
||||
newFormat = JacksonUtils.toObj(json, ServiceInfo.class);
|
||||
|
||||
if (StringUtils.isEmpty(newFormat.getName())) {
|
||||
ips.add(JSON.parseObject(json, Instance.class));
|
||||
ips.add(JacksonUtils.toObj(json, Instance.class));
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
NAMING_LOGGER.error("[NA] error while parsing cache file: " + json, e);
|
||||
|
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.core;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
||||
@ -24,6 +23,8 @@ import com.alibaba.nacos.client.naming.backups.FailoverReactor;
|
||||
import com.alibaba.nacos.client.naming.cache.DiskCache;
|
||||
import com.alibaba.nacos.client.naming.net.NamingProxy;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
@ -98,7 +99,7 @@ public class HostReactor {
|
||||
}
|
||||
|
||||
public ServiceInfo processServiceJSON(String json) {
|
||||
ServiceInfo serviceInfo = JSON.parseObject(json, ServiceInfo.class);
|
||||
ServiceInfo serviceInfo = JacksonUtils.toObj(json, ServiceInfo.class);
|
||||
ServiceInfo oldService = serviceInfoMap.get(serviceInfo.getKey());
|
||||
if (serviceInfo.getHosts() == null || !serviceInfo.validate()) {
|
||||
//empty or error push, just ignore
|
||||
@ -162,19 +163,19 @@ public class HostReactor {
|
||||
if (newHosts.size() > 0) {
|
||||
changed = true;
|
||||
NAMING_LOGGER.info("new ips(" + newHosts.size() + ") service: "
|
||||
+ serviceInfo.getKey() + " -> " + JSON.toJSONString(newHosts));
|
||||
+ serviceInfo.getKey() + " -> " + JacksonUtils.toJson(newHosts));
|
||||
}
|
||||
|
||||
if (remvHosts.size() > 0) {
|
||||
changed = true;
|
||||
NAMING_LOGGER.info("removed ips(" + remvHosts.size() + ") service: "
|
||||
+ serviceInfo.getKey() + " -> " + JSON.toJSONString(remvHosts));
|
||||
+ serviceInfo.getKey() + " -> " + JacksonUtils.toJson(remvHosts));
|
||||
}
|
||||
|
||||
if (modHosts.size() > 0) {
|
||||
changed = true;
|
||||
NAMING_LOGGER.info("modified ips(" + modHosts.size() + ") service: "
|
||||
+ serviceInfo.getKey() + " -> " + JSON.toJSONString(modHosts));
|
||||
+ serviceInfo.getKey() + " -> " + JacksonUtils.toJson(modHosts));
|
||||
}
|
||||
|
||||
serviceInfo.setJsonFromServer(json);
|
||||
@ -186,8 +187,8 @@ public class HostReactor {
|
||||
|
||||
} else {
|
||||
changed = true;
|
||||
NAMING_LOGGER.info("init new ips(" + serviceInfo.ipCount() + ") service: " + serviceInfo.getKey() + " -> " + JSON
|
||||
.toJSONString(serviceInfo.getHosts()));
|
||||
NAMING_LOGGER.info("init new ips(" + serviceInfo.ipCount() + ") service: " + serviceInfo.getKey() + " -> " +
|
||||
JacksonUtils.toJson(serviceInfo.getHosts()));
|
||||
serviceInfoMap.put(serviceInfo.getKey(), serviceInfo);
|
||||
eventDispatcher.serviceChanged(serviceInfo);
|
||||
serviceInfo.setJsonFromServer(json);
|
||||
@ -198,7 +199,7 @@ public class HostReactor {
|
||||
|
||||
if (changed) {
|
||||
NAMING_LOGGER.info("current ips:(" + serviceInfo.ipCount() + ") service: " + serviceInfo.getKey() +
|
||||
" -> " + JSON.toJSONString(serviceInfo.getHosts()));
|
||||
" -> " + JacksonUtils.toJson(serviceInfo.getHosts()));
|
||||
}
|
||||
|
||||
return serviceInfo;
|
||||
@ -214,7 +215,7 @@ public class HostReactor {
|
||||
public ServiceInfo getServiceInfoDirectlyFromServer(final String serviceName, final String clusters) throws NacosException {
|
||||
String result = serverProxy.queryList(serviceName, clusters, 0, false);
|
||||
if (StringUtils.isNotEmpty(result)) {
|
||||
return JSON.parseObject(result, ServiceInfo.class);
|
||||
return JacksonUtils.toObj(result, ServiceInfo.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.core;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import com.alibaba.nacos.common.utils.IoUtils;
|
||||
|
||||
@ -75,7 +75,7 @@ public class PushReceiver implements Runnable {
|
||||
String json = new String(IoUtils.tryDecompress(packet.getData()), "UTF-8").trim();
|
||||
NAMING_LOGGER.info("received push data: " + json + " from " + packet.getAddress().toString());
|
||||
|
||||
PushPacket pushPacket = JSON.parseObject(json, PushPacket.class);
|
||||
PushPacket pushPacket = JacksonUtils.toObj(json, PushPacket.class);
|
||||
String ack;
|
||||
if ("dom".equals(pushPacket.type) || "service".equals(pushPacket.type)) {
|
||||
hostReactor.processServiceJSON(pushPacket.data);
|
||||
@ -89,7 +89,7 @@ public class PushReceiver implements Runnable {
|
||||
ack = "{\"type\": \"dump-ack\""
|
||||
+ ", \"lastRefTime\": \"" + pushPacket.lastRefTime
|
||||
+ "\", \"data\":" + "\""
|
||||
+ StringUtils.escapeJavaScript(JSON.toJSONString(hostReactor.getServiceInfoMap()))
|
||||
+ StringUtils.escapeJavaScript(JacksonUtils.toJson(hostReactor.getServiceInfoMap()))
|
||||
+ "\"}";
|
||||
} else {
|
||||
// do nothing send ack only
|
||||
|
@ -15,9 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.naming.net;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||
import com.alibaba.nacos.api.SystemPropertyKeyConst;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
@ -42,8 +39,12 @@ import com.alibaba.nacos.client.utils.TemplateUtils;
|
||||
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
|
||||
import com.alibaba.nacos.common.utils.HttpMethod;
|
||||
import com.alibaba.nacos.common.utils.IoUtils;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.alibaba.nacos.common.utils.UuidUtils;
|
||||
import com.alibaba.nacos.common.utils.VersionUtils;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -207,7 +208,7 @@ public class NamingProxy {
|
||||
params.put("enable", String.valueOf(instance.isEnabled()));
|
||||
params.put("healthy", String.valueOf(instance.isHealthy()));
|
||||
params.put("ephemeral", String.valueOf(instance.isEphemeral()));
|
||||
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
|
||||
params.put("metadata", JacksonUtils.toJson(instance.getMetadata()));
|
||||
|
||||
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST);
|
||||
|
||||
@ -243,7 +244,7 @@ public class NamingProxy {
|
||||
params.put("weight", String.valueOf(instance.getWeight()));
|
||||
params.put("enabled", String.valueOf(instance.isEnabled()));
|
||||
params.put("ephemeral", String.valueOf(instance.isEphemeral()));
|
||||
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
|
||||
params.put("metadata", JacksonUtils.toJson(instance.getMetadata()));
|
||||
|
||||
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.PUT);
|
||||
}
|
||||
@ -258,8 +259,7 @@ public class NamingProxy {
|
||||
params.put(CommonParams.GROUP_NAME, groupName);
|
||||
|
||||
String result = reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.GET);
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
return jsonObject.toJavaObject(Service.class);
|
||||
return JacksonUtils.toObj(result, Service.class);
|
||||
}
|
||||
|
||||
public void createService(Service service, AbstractSelector selector) throws NacosException {
|
||||
@ -272,8 +272,8 @@ public class NamingProxy {
|
||||
params.put(CommonParams.SERVICE_NAME, service.getName());
|
||||
params.put(CommonParams.GROUP_NAME, service.getGroupName());
|
||||
params.put("protectThreshold", String.valueOf(service.getProtectThreshold()));
|
||||
params.put("metadata", JSON.toJSONString(service.getMetadata()));
|
||||
params.put("selector", JSON.toJSONString(selector));
|
||||
params.put("metadata", JacksonUtils.toJson(service.getMetadata()));
|
||||
params.put("selector", JacksonUtils.toJson(selector));
|
||||
|
||||
reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.POST);
|
||||
|
||||
@ -301,8 +301,8 @@ public class NamingProxy {
|
||||
params.put(CommonParams.SERVICE_NAME, service.getName());
|
||||
params.put(CommonParams.GROUP_NAME, service.getGroupName());
|
||||
params.put("protectThreshold", String.valueOf(service.getProtectThreshold()));
|
||||
params.put("metadata", JSON.toJSONString(service.getMetadata()));
|
||||
params.put("selector", JSON.toJSONString(selector));
|
||||
params.put("metadata", JacksonUtils.toJson(service.getMetadata()));
|
||||
params.put("selector", JacksonUtils.toJson(selector));
|
||||
|
||||
reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.PUT);
|
||||
}
|
||||
@ -321,7 +321,7 @@ public class NamingProxy {
|
||||
return reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params, HttpMethod.GET);
|
||||
}
|
||||
|
||||
public JSONObject sendBeat(BeatInfo beatInfo, boolean lightBeatEnabled) throws NacosException {
|
||||
public JsonNode sendBeat(BeatInfo beatInfo, boolean lightBeatEnabled) throws NacosException {
|
||||
|
||||
if (NAMING_LOGGER.isDebugEnabled()) {
|
||||
NAMING_LOGGER.debug("[BEAT] {} sending beat to server: {}", namespaceId, beatInfo.toString());
|
||||
@ -330,7 +330,7 @@ public class NamingProxy {
|
||||
String body = StringUtils.EMPTY;
|
||||
if (!lightBeatEnabled) {
|
||||
try {
|
||||
body = "beat=" + URLEncoder.encode(JSON.toJSONString(beatInfo), "UTF-8");
|
||||
body = "beat=" + URLEncoder.encode(JacksonUtils.toJson(beatInfo), "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new NacosException(NacosException.SERVER_ERROR, "encode beatInfo error", e);
|
||||
}
|
||||
@ -341,7 +341,7 @@ public class NamingProxy {
|
||||
params.put("ip", beatInfo.getIp());
|
||||
params.put("port", String.valueOf(beatInfo.getPort()));
|
||||
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/beat", params, body, HttpMethod.PUT);
|
||||
return JSON.parseObject(result);
|
||||
return JacksonUtils.toObj(result);
|
||||
}
|
||||
|
||||
public boolean serverHealthy() {
|
||||
@ -349,8 +349,8 @@ public class NamingProxy {
|
||||
try {
|
||||
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/operator/metrics",
|
||||
new HashMap<String, String>(2), HttpMethod.GET);
|
||||
JSONObject json = JSON.parseObject(result);
|
||||
String serverStatus = json.getString("status");
|
||||
JsonNode json = JacksonUtils.toObj(result);
|
||||
String serverStatus = json.get("status").asText();
|
||||
return "UP".equals(serverStatus);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
@ -375,7 +375,7 @@ public class NamingProxy {
|
||||
break;
|
||||
case label:
|
||||
ExpressionSelector expressionSelector = (ExpressionSelector) selector;
|
||||
params.put("selector", JSON.toJSONString(expressionSelector));
|
||||
params.put("selector", JacksonUtils.toJson(expressionSelector));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -384,11 +384,10 @@ public class NamingProxy {
|
||||
|
||||
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/service/list", params, HttpMethod.GET);
|
||||
|
||||
JSONObject json = JSON.parseObject(result);
|
||||
ListView<String> listView = new ListView<String>();
|
||||
listView.setCount(json.getInteger("count"));
|
||||
listView.setData(JSON.parseObject(json.getString("doms"), new TypeReference<List<String>>() {
|
||||
}));
|
||||
JsonNode json = JacksonUtils.toObj(result);
|
||||
ListView<String> listView = new ListView<>();
|
||||
listView.setCount(json.get("count").asInt());
|
||||
listView.setData(JacksonUtils.toObj(json.get("doms").asText(), new TypeReference<List<String>>() {}));
|
||||
|
||||
return listView;
|
||||
}
|
||||
|
@ -15,13 +15,13 @@
|
||||
*/
|
||||
package com.alibaba.nacos.client.security;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.nacos.api.PropertyKeyConst;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.client.naming.net.HttpClient;
|
||||
import com.alibaba.nacos.common.utils.HttpMethod;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import org.apache.commons.codec.Charsets;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
@ -122,14 +122,14 @@ public class SecurityProxy {
|
||||
params, body, Charsets.UTF_8.name(), HttpMethod.POST);
|
||||
|
||||
if (result.code != HttpURLConnection.HTTP_OK) {
|
||||
SECURITY_LOGGER.error("login failed: {}", JSON.toJSONString(result));
|
||||
SECURITY_LOGGER.error("login failed: {}", JacksonUtils.toJson(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
JSONObject obj = JSON.parseObject(result.content);
|
||||
if (obj.containsKey(Constants.ACCESS_TOKEN)) {
|
||||
accessToken = obj.getString(Constants.ACCESS_TOKEN);
|
||||
tokenTtl = obj.getIntValue(Constants.TOKEN_TTL);
|
||||
JsonNode obj = JacksonUtils.toObj(result.content);
|
||||
if (obj.has(Constants.ACCESS_TOKEN)) {
|
||||
accessToken = obj.get(Constants.ACCESS_TOKEN).asText();
|
||||
tokenTtl = obj.get(Constants.TOKEN_TTL).asInt();
|
||||
tokenRefreshWindow = tokenTtl / 10;
|
||||
}
|
||||
}
|
||||
|
95
client/src/test/java/com/alibaba/nacos/client/naming/cache/DiskCacheTest.java
vendored
Normal file
95
client/src/test/java/com/alibaba/nacos/client/naming/cache/DiskCacheTest.java
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.client.naming.cache;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
||||
|
||||
public class DiskCacheTest {
|
||||
|
||||
private static final String CACHE_DIR = DiskCacheTest.class.getResource("/").getPath() + "cache/";
|
||||
|
||||
private ServiceInfo serviceInfo;
|
||||
|
||||
private Instance instance;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
System.out.println(CACHE_DIR);
|
||||
serviceInfo = new ServiceInfo("testName", "testClusters");
|
||||
instance = new Instance();
|
||||
instance.setClusterName("testClusters");
|
||||
instance.setIp("1.1.1.1");
|
||||
instance.setPort(1234);
|
||||
instance.setServiceName("testName");
|
||||
serviceInfo.setHosts(Collections.singletonList(instance));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
File file = new File(CACHE_DIR);
|
||||
if (file.exists() && file.list().length > 0) {
|
||||
for (File each : file.listFiles()) {
|
||||
each.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCache() {
|
||||
DiskCache.write(serviceInfo, CACHE_DIR);
|
||||
Map<String, ServiceInfo> actual = DiskCache.read(CACHE_DIR);
|
||||
assertEquals(1, actual.size());
|
||||
assertTrue(actual.containsKey(serviceInfo.getKeyEncoded()));
|
||||
assertServiceInfo(actual.get(serviceInfo.getKeyEncoded()), serviceInfo);
|
||||
}
|
||||
|
||||
private void assertServiceInfo(ServiceInfo actual, ServiceInfo expected) {
|
||||
assertEquals(actual.getName(), expected.getName());
|
||||
assertEquals(actual.getGroupName(), expected.getGroupName());
|
||||
assertEquals(actual.getClusters(), expected.getClusters());
|
||||
assertEquals(actual.getCacheMillis(), expected.getCacheMillis());
|
||||
assertEquals(actual.getLastRefTime(), expected.getLastRefTime());
|
||||
assertEquals(actual.getKey(), expected.getKey());
|
||||
assertHosts(actual.getHosts(), expected.getHosts());
|
||||
}
|
||||
|
||||
private void assertHosts(List<Instance> actual, List<Instance> expected) {
|
||||
assertEquals(actual.size(), expected.size());
|
||||
for (int i = 0; i < expected.size(); i++) {
|
||||
assertInstance(actual.get(i), expected.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
private void assertInstance(Instance actual, Instance expected) {
|
||||
assertEquals(actual.getServiceName(), actual.getServiceName());
|
||||
assertEquals(actual.getClusterName(), actual.getClusterName());
|
||||
assertEquals(actual.getIp(), actual.getIp());
|
||||
assertEquals(actual.getPort(), actual.getPort());
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.client.naming.core;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
|
||||
import com.alibaba.nacos.client.naming.net.NamingProxy;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class HostReactorTest {
|
||||
|
||||
private static final String CACHE_DIR = HostReactorTest.class.getResource("/").getPath() + "cache/";
|
||||
|
||||
@Mock
|
||||
private NamingProxy namingProxy;
|
||||
|
||||
@Mock
|
||||
private EventDispatcher eventDispatcher;
|
||||
|
||||
private HostReactor hostReactor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
hostReactor = new HostReactor(eventDispatcher, namingProxy, CACHE_DIR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessServiceJSON() {
|
||||
ServiceInfo actual = hostReactor.processServiceJSON(EXAMPLE);
|
||||
assertServiceInfo(actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServiceInfoDirectlyFromServer() throws NacosException {
|
||||
when(namingProxy.queryList("testName", "testClusters", 0, false)).thenReturn(EXAMPLE);
|
||||
ServiceInfo actual = hostReactor.getServiceInfoDirectlyFromServer("testName", "testClusters");
|
||||
assertServiceInfo(actual);
|
||||
}
|
||||
|
||||
private void assertServiceInfo(ServiceInfo actual) {
|
||||
assertEquals("testName", actual.getName());
|
||||
assertEquals("testClusters", actual.getClusters());
|
||||
assertEquals("", actual.getChecksum());
|
||||
assertEquals(1000, actual.getCacheMillis());
|
||||
assertEquals(0, actual.getLastRefTime());
|
||||
assertNull(actual.getGroupName());
|
||||
assertTrue(actual.isValid());
|
||||
assertFalse(actual.isAllIPs());
|
||||
assertEquals(1, actual.getHosts().size());
|
||||
assertInstance(actual.getHosts().get(0));
|
||||
}
|
||||
|
||||
private void assertInstance(Instance actual) {
|
||||
assertEquals("1.1.1.1", actual.getIp());
|
||||
assertEquals("testClusters", actual.getClusterName());
|
||||
assertEquals("testName", actual.getServiceName());
|
||||
assertEquals(1234, actual.getPort());
|
||||
}
|
||||
|
||||
private static final String EXAMPLE = "{\n"
|
||||
+ "\t\"name\": \"testName\",\n"
|
||||
+ "\t\"clusters\": \"testClusters\",\n"
|
||||
+ "\t\"cacheMillis\": 1000,\n"
|
||||
+ "\t\"hosts\": [{\n"
|
||||
+ "\t\t\"ip\": \"1.1.1.1\",\n"
|
||||
+ "\t\t\"port\": 1234,\n"
|
||||
+ "\t\t\"weight\": 1.0,\n"
|
||||
+ "\t\t\"healthy\": true,\n"
|
||||
+ "\t\t\"enabled\": true,\n"
|
||||
+ "\t\t\"ephemeral\": true,\n"
|
||||
+ "\t\t\"clusterName\": \"testClusters\",\n"
|
||||
+ "\t\t\"serviceName\": \"testName\",\n"
|
||||
+ "\t\t\"metadata\": {},\n"
|
||||
+ "\t\t\"instanceHeartBeatInterval\": 5000,\n"
|
||||
+ "\t\t\"instanceHeartBeatTimeOut\": 15000,\n"
|
||||
+ "\t\t\"ipDeleteTimeout\": 30000,\n"
|
||||
+ "\t\t\"instanceIdGenerator\": \"simple\"\n"
|
||||
+ "\t}],\n"
|
||||
+ "\t\"lastRefTime\": 0,\n"
|
||||
+ "\t\"checksum\": \"\",\n"
|
||||
+ "\t\"allIPs\": false,\n"
|
||||
+ "\t\"valid\": true\n"
|
||||
+ "}";
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.client.security;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.common.utils.JacksonUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class SecurityProxyTest {
|
||||
|
||||
/**
|
||||
* Just test for replace fastjson with jackson.
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testLogin() {
|
||||
String example = "{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}";
|
||||
JsonNode obj = JacksonUtils.toObj(example);
|
||||
if (obj.has(Constants.ACCESS_TOKEN)) {
|
||||
if (obj.has(Constants.ACCESS_TOKEN)) {
|
||||
assertEquals("ttttttttttttttttt", obj.get(Constants.ACCESS_TOKEN).asText());
|
||||
assertEquals(1000, obj.get(Constants.TOKEN_TTL).asInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,9 @@ package com.alibaba.nacos.common.utils;
|
||||
import com.alibaba.nacos.api.exception.NacosDeserializationException;
|
||||
import com.alibaba.nacos.api.exception.NacosSerializationException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.NamedType;
|
||||
|
||||
@ -69,6 +71,14 @@ public final class JacksonUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T toObj(String json, TypeReference<T> typeReference) {
|
||||
try {
|
||||
return mapper.readValue(json, typeReference);
|
||||
} catch (IOException e) {
|
||||
throw new NacosDeserializationException(typeReference.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T toObj(String json, Class<T> cls) {
|
||||
try {
|
||||
return mapper.readValue(json, cls);
|
||||
@ -85,6 +95,14 @@ public final class JacksonUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonNode toObj(String json) {
|
||||
try {
|
||||
return mapper.readTree(json);
|
||||
} catch (IOException e) {
|
||||
throw new NacosDeserializationException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerSubtype(Class<?> clz, String type) {
|
||||
mapper.registerSubtypes(new NamedType(clz, type));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user