Replace fastjson with jackson for naming.core

This commit is contained in:
KomachiSion 2020-05-27 15:38:17 +08:00
parent 9fc5a971d6
commit 61135f6dce
6 changed files with 48 additions and 25 deletions

View File

@ -52,10 +52,6 @@
<artifactId>nacos-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>

View File

@ -326,11 +326,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
}
public String toJSON() {
try {
return JacksonUtils.toJson(this);
} catch (Exception e) {
throw new RuntimeException("Service toJson failed", e);
}
}
@JsonIgnore

View File

@ -15,12 +15,10 @@
*/
package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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.utils.NamingUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.naming.consistency.ConsistencyService;
@ -32,14 +30,14 @@ import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet;
import com.alibaba.nacos.naming.misc.GlobalExecutor;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.Message;
import com.alibaba.nacos.naming.misc.NamingProxy;
import com.alibaba.nacos.naming.misc.NetUtils;
import com.alibaba.nacos.naming.misc.ServiceStatusSynchronizer;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.Synchronizer;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.push.PushService;
import com.google.common.collect.Maps;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Collection;
@ -62,7 +60,6 @@ import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@ -274,13 +271,13 @@ public class ServiceManager implements RecordListener<Service> {
public void updatedHealthStatus(String namespaceId, String serviceName, String serverIP) {
Message msg = synchronizer.get(serverIP, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName));
JSONObject serviceJson = JSON.parseObject(msg.getData());
JsonNode serviceJson = JacksonUtils.toObj(msg.getData());
JSONArray ipList = serviceJson.getJSONArray("ips");
ArrayNode ipList = (ArrayNode) serviceJson.get("ips");
Map<String, String> ipsMap = new HashMap<>(ipList.size());
for (int i = 0; i < ipList.size(); i++) {
String ip = ipList.getString(i);
String ip = ipList.get(i).asText();
String[] strings = ip.split("_");
ipsMap.put(strings[0], strings[1]);
}
@ -565,7 +562,7 @@ public class ServiceManager implements RecordListener<Service> {
if (instanceMap.size() <= 0 && UtilsAndCommons.UPDATE_INSTANCE_ACTION_ADD.equals(action)) {
throw new IllegalArgumentException("ip list can not be empty, service: " + service.getName() + ", ip list: "
+ JSON.toJSONString(instanceMap.values()));
+ JacksonUtils.toJson(instanceMap.values()));
}
return new ArrayList<>(instanceMap.values());
@ -839,7 +836,7 @@ public class ServiceManager implements RecordListener<Service> {
Message msg = new Message();
msg.setData(JSON.toJSONString(checksum));
msg.setData(JacksonUtils.toJson(checksum));
Collection<Member> sameSiteServers = memberManager.allMembers();
@ -893,7 +890,7 @@ public class ServiceManager implements RecordListener<Service> {
@Override
public String toString() {
return JSON.toJSONString(this);
return JacksonUtils.toJson(this);
}
}
}

View File

@ -15,8 +15,8 @@
*/
package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.utils.ApplicationUtils;
@ -93,7 +93,7 @@ public class SubscribeManager {
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), paramValues);
if (HttpURLConnection.HTTP_OK == result.code) {
Subscribers subscribers = (Subscribers) JSONObject.parseObject(result.content, Subscribers.class);
Subscribers subscribers = JacksonUtils.toObj(result.content, Subscribers.class);
subscriberList.addAll(subscribers.getSubscribers());
}
}

View File

@ -17,10 +17,14 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.consistency.ConsistencyService;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.core.ServiceManager.ServiceChecksum;
import com.alibaba.nacos.naming.misc.Message;
import com.alibaba.nacos.naming.misc.Synchronizer;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.google.common.collect.Maps;
import org.junit.Assert;
@ -33,6 +37,7 @@ import org.springframework.test.util.ReflectionTestUtils;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
/**
@ -46,6 +51,9 @@ public class ServiceManagerTest extends BaseTest {
@Mock
private ConsistencyService consistencyService;
@Mock
private Synchronizer synchronizer;
@Before
public void before() {
super.before();
@ -116,4 +124,26 @@ public class ServiceManagerTest extends BaseTest {
int instanceId2 = Integer.parseInt(instance2.getInstanceId());
Assert.assertNotEquals(instanceId1, instanceId2);
}
@Test
public void testUpdatedHealthStatus() {
ReflectionTestUtils.setField(serviceManager, "synchronizer", synchronizer);
String namespaceId = "namespaceId";
String serviceName = "testService";
String serverIp = "127.0.0.1";
String example = "{\"ips\":[\"127.0.0.1:8848_true\"]}";
Message message = new Message();
message.setData(example);
when(synchronizer.get(serverIp, UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName))).thenReturn(message);
serviceManager.updatedHealthStatus(namespaceId, serviceName, serverIp);
}
@Test
public void testSerializeServiceChecksum() {
ServiceChecksum checksum = new ServiceChecksum();
checksum.addItem("test", "1234567890");
String actual = JacksonUtils.toJson(checksum);
assertTrue(actual.contains("\"namespaceId\":\"public\""));
assertTrue(actual.contains("\"serviceName2Checksum\":{\"test\":\"1234567890\"}"));
}
}

View File

@ -15,7 +15,6 @@
*/
package com.alibaba.nacos.naming.core;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.selector.NoneSelector;
@ -48,7 +47,6 @@ public class ServiceTest extends BaseTest {
mockInjectHealthCheckProcessor();
mockInjectDistroMapper();
mockInjectSwitchDomain();
JacksonUtils.registerSubtype(NoneSelector.class, SelectorType.none.name());
}
@Test
@ -121,4 +119,10 @@ public class ServiceTest extends BaseTest {
assertTrue(actual.contains("\"protectThreshold\":0.0"));
assertTrue(actual.contains("\"clusters\":[]"));
}
@Test
public void test() {
String a = "{\"appName\":\"\",\"checksum\":\"d9ce515b2d3f26a06cd7ba43a4cba1a7\",\"clusterMap\":{},\"empty\":true,\"enabled\":true,\"finalizeCount\":0,\"groupName\":\"\",\"ipDeleteTimeout\":30000,\"lastModifiedMillis\":1590401570517,\"metadata\":{},\"name\":\"DEFAULT_GROUP@@nacos.test\",\"namespaceId\":\"public\",\"owners\":[],\"protectThreshold\":0.5,\"pushService\":{\"failedPushCount\":0,\"totalPush\":0},\"resetWeight\":false,\"selector\":{\"type\":\"none\"},\"serviceString\":\"{\\\"invalidIPCount\\\":0,\\\"name\\\":\\\"DEFAULT_GROUP@@nacos.test\\\",\\\"ipCount\\\":0,\\\"owners\\\":[],\\\"protectThreshold\\\":0.5,\\\"clusters\\\":[],\\\"token\\\":null}\",\"token\":\"\"}";
// JacksonUtils.toObj(a, Service.class);
}
}