replace fastJson with jackson for naming instance

This commit is contained in:
KomachiSion 2020-05-21 15:33:18 +08:00
parent fc5089602f
commit 70a0bb7c1b
4 changed files with 137 additions and 26 deletions

View File

@ -15,13 +15,16 @@
*/
package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.healthcheck.HealthCheckStatus;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import org.apache.commons.lang3.math.NumberUtils;
import java.util.Set;
@ -34,6 +37,7 @@ import java.util.regex.Pattern;
*
* @author nkorange
*/
@JsonInclude(Include.NON_NULL)
public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance implements Comparable {
private static final double MAX_WEIGHT_VALUE = 10000.0D;
@ -42,7 +46,7 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
private volatile long lastBeat = System.currentTimeMillis();
@JSONField(serialize = false)
@JsonIgnore
private volatile boolean mockValid = false;
private volatile boolean marked = false;
@ -173,7 +177,11 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
}
public String toJSON() {
return JSON.toJSONString(this);
try {
return JacksonUtils.toJson(this);
} catch (Exception e) {
throw new RuntimeException("Instance toJSON failed", e);
}
}
@ -181,7 +189,8 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
Instance ip;
try {
ip = JSON.parseObject(json, Instance.class);
ip = JacksonUtils.toObj(json, Instance.class);
// ip = JSON.parseObject(json, Instance.class);
} catch (Exception e) {
ip = fromString(json);
}
@ -224,7 +233,7 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
&& this.isEphemeral() == other.isEphemeral();
}
@JSONField(serialize = false)
@JsonIgnore
public String getDatumKey() {
if (getPort() > 0) {
return getIp() + ":" + getPort() + ":" + UtilsAndCommons.LOCALHOST_SITE + ":" + getClusterName();
@ -233,7 +242,7 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
}
}
@JSONField(serialize = false)
@JsonIgnore
public String getDefaultKey() {
if (getPort() > 0) {
return getIp() + ":" + getPort() + ":" + UtilsAndCommons.UNKNOWN_SITE;
@ -255,22 +264,22 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
return HealthCheckStatus.get(this).isBeingChecked.compareAndSet(false, true);
}
@JSONField(serialize = false)
@JsonIgnore
public long getCheckRT() {
return HealthCheckStatus.get(this).checkRT;
}
@JSONField(serialize = false)
@JsonIgnore
public AtomicInteger getOKCount() {
return HealthCheckStatus.get(this).checkOKCount;
}
@JSONField(serialize = false)
@JsonIgnore
public AtomicInteger getFailCount() {
return HealthCheckStatus.get(this).checkFailCount;
}
@JSONField(serialize = false)
@JsonIgnore
public void setCheckRT(long checkRT) {
HealthCheckStatus.get(this).checkRT = checkRT;
}

View File

@ -15,11 +15,12 @@
*/
package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.naming.pojo.Record;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
@ -47,11 +48,15 @@ public class Instances implements Record {
@Override
public String toString() {
return JSON.toJSONString(this);
try {
return JacksonUtils.toJson(this);
} catch (Exception e) {
throw new RuntimeException("Instances toJSON failed", e);
}
}
@Override
@JSONField(serialize = false)
@JsonIgnore
public String getChecksum() {
return recalculateChecksum();

View File

@ -17,12 +17,16 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.naming.healthcheck.RsInfo;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.HashMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author nkorange
@ -42,18 +46,77 @@ public class InstanceTest {
instance.setPort(1234);
instance.setWeight(5);
Assert.assertEquals("1.1.1.1", instance.getIp());
Assert.assertEquals(1234, instance.getPort());
Assert.assertEquals(5, instance.getWeight(), 0.001);
assertEquals("1.1.1.1", instance.getIp());
assertEquals(1234, instance.getPort());
assertEquals(5, instance.getWeight(), 0.001);
}
@Test
public void fromJson() {
public void testToJsonWithAllParam() {
instance = new Instance("1.1.1.1", 1234, "TEST", "TENANT", "APP");
String actual = instance.toJSON();
assertTrue(actual.contains("\"app\":\"APP\""));
assertTrue(actual.contains("\"clusterName\":\"TEST\""));
assertTrue(actual.contains("\"enabled\":true"));
assertTrue(actual.contains("\"ephemeral\":true"));
assertTrue(actual.contains("\"healthy\":true"));
assertTrue(actual.contains("\"instanceHeartBeatInterval\":5000"));
assertTrue(actual.contains("\"instanceHeartBeatTimeOut\":15000"));
assertTrue(actual.contains("\"instanceIdGenerator\":\"simple\""));
assertTrue(actual.contains("\"ip\":\"1.1.1.1\""));
assertTrue(actual.contains("\"ipDeleteTimeout\":30000"));
assertTrue(actual.contains("\"lastBeat\":" + instance.getLastBeat()));
assertTrue(actual.contains("\"marked\":false"));
assertTrue(actual.contains("\"metadata\":{}"));
assertTrue(actual.contains("\"port\":1234"));
assertTrue(actual.contains("\"tenant\":\"TENANT\""));
assertTrue(actual.contains("\"weight\":1.0"));
assertFalse(actual.contains("\"mockValid\""));
assertFalse(actual.contains("\"failCount\""));
}
@Test
public void testToJsonWithoutTenantAndApp() {
instance = new Instance("1.1.1.1", 1234, "TEST");
String actual = instance.toJSON();
System.out.println(actual);
assertTrue(actual.contains("\"clusterName\":\"TEST\""));
assertTrue(actual.contains("\"enabled\":true"));
assertTrue(actual.contains("\"ephemeral\":true"));
assertTrue(actual.contains("\"healthy\":true"));
assertTrue(actual.contains("\"instanceHeartBeatInterval\":5000"));
assertTrue(actual.contains("\"instanceHeartBeatTimeOut\":15000"));
assertTrue(actual.contains("\"instanceIdGenerator\":\"simple\""));
assertTrue(actual.contains("\"ip\":\"1.1.1.1\""));
assertTrue(actual.contains("\"ipDeleteTimeout\":30000"));
assertTrue(actual.contains("\"lastBeat\":" + instance.getLastBeat()));
assertTrue(actual.contains("\"marked\":false"));
assertTrue(actual.contains("\"metadata\":{}"));
assertTrue(actual.contains("\"port\":1234"));
assertTrue(actual.contains("\"weight\":1.0"));
assertFalse(actual.contains("\"app\""));
assertFalse(actual.contains("\"tenant\":"));
assertFalse(actual.contains("\"mockValid\""));
assertFalse(actual.contains("\"failCount\""));
}
@Test
public void testFromJsonByJson() {
instance = Instance.fromJSON("{\"clusterName\":\"TEST\",\"enabled\":true,\"ephemeral\":true,\"healthy\":true,\"instanceHeartBeatInterval\":5000,\"instanceHeartBeatTimeOut\":15000,\"instanceIdGenerator\":\"simple\",\"ip\":\"1.1.1.1\",\"ipDeleteTimeout\":30000,\"lastBeat\":1590043805463,\"marked\":false,\"metadata\":{},\"port\":1234,\"weight\":1.0}\n");
assertEquals("1.1.1.1", instance.getIp());
assertEquals(1234, instance.getPort());
assertEquals("TEST", instance.getClusterName());
assertNull(instance.getApp());
assertNull(instance.getTenant());
}
@Test
public void testFromJsonByNoJson() {
instance = Instance.fromJSON("2.2.2.2:8888_2_TEST1");
Assert.assertEquals("2.2.2.2", instance.getIp());
Assert.assertEquals(8888, instance.getPort());
Assert.assertEquals(2, instance.getWeight(), 0.001);
Assert.assertEquals("TEST1", instance.getClusterName());
assertEquals("2.2.2.2", instance.getIp());
assertEquals(8888, instance.getPort());
assertEquals(2, instance.getWeight(), 0.001);
assertEquals("TEST1", instance.getClusterName());
}
@Test

View File

@ -16,13 +16,19 @@
package com.alibaba.nacos.naming.core;
import org.junit.Test;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import static org.junit.Assert.assertEquals;
/**
* @author lkxiaolou
@ -81,6 +87,34 @@ public class InstancesTest {
assert catchException.get();
}
@Test
public void testToString() {
Instances actual = new Instances();
Collection<Instance> instancesCase = createInstancesCase();
actual.getInstanceList().addAll(instancesCase);
String expected = "{\"instanceList\":[" + StringUtils.join(instancesCase.stream().map(Instance::toJSON).collect(Collectors.toList()), ",") + "]}";
assertEquals(expected, actual.toString());
}
@Test
public void testDeserialzeFromJson() throws Exception {
Collection<Instance> expected = createInstancesCase();
String instancesJson = "{\"instanceList\":[" + StringUtils.join(expected.stream().map(Instance::toJSON).collect(Collectors.toList()), ",") + "]}";
Instances actual = JacksonUtils.toObj(instancesJson, Instances.class);
assertEquals(expected, actual.getInstanceList());
}
private Collection<Instance> createInstancesCase() {
Collection<Instance> result = new ArrayList<>();
Instance instanceWithBasicParam = new Instance("1.1.1.1", 1111);
Instance instanceWithCluster = new Instance("1.1.1.1", 1112, "TEST");
Instance instanceWithAllParam = new Instance("1.1.1.1", 1112, "TEST", "TENANT", "APP");
result.add(instanceWithBasicParam);
result.add(instanceWithCluster);
result.add(instanceWithAllParam);
return result;
}
//@Test
// 跑起来比较久所以注释掉
public void checkSumThreadSafe() throws Exception {