1. 修正服务和集群之间的关系(Correct the relationship between the service and cluster)
2. 创建集群时必须提供名字和所属服务(The name and the service are required to create a cluster) 3. 修正UtilsAndCommons.shakeUp的注释
This commit is contained in:
parent
fdcd379371
commit
22a2969593
@ -58,11 +58,10 @@ public class CatalogController {
|
||||
|
||||
@RequestMapping(value = "/service")
|
||||
public JSONObject serviceDetail(HttpServletRequest request) throws Exception {
|
||||
|
||||
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
|
||||
Constants.DEFAULT_NAMESPACE_ID);
|
||||
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
|
||||
com.alibaba.nacos.naming.core.Service detailedService = serviceManager.getService(namespaceId, serviceName);
|
||||
Service detailedService = serviceManager.getService(namespaceId, serviceName);
|
||||
if (detailedService == null) {
|
||||
throw new NacosException(NacosException.NOT_FOUND, "serivce " + serviceName + " is not found!");
|
||||
}
|
||||
@ -70,9 +69,9 @@ public class CatalogController {
|
||||
JSONObject detailView = new JSONObject();
|
||||
|
||||
JSONObject serviceObject = new JSONObject();
|
||||
serviceObject.put("name", NamingUtils.getServiceName(serviceName));
|
||||
serviceObject.put("name", detailedService.getName());
|
||||
serviceObject.put("protectThreshold", detailedService.getProtectThreshold());
|
||||
serviceObject.put("groupName", NamingUtils.getGroupName(serviceName));
|
||||
serviceObject.put("groupName", detailedService.getGroupName());
|
||||
serviceObject.put("selector", detailedService.getSelector());
|
||||
serviceObject.put("metadata", detailedService.getMetadata());
|
||||
|
||||
@ -80,7 +79,7 @@ public class CatalogController {
|
||||
|
||||
List<Cluster> clusters = new ArrayList<>();
|
||||
|
||||
for (Cluster cluster : detailedService.getClusterMap().values()) {
|
||||
for (com.alibaba.nacos.naming.core.Cluster cluster : detailedService.getClusterMap().values()) {
|
||||
Cluster clusterView = new Cluster();
|
||||
clusterView.setName(cluster.getName());
|
||||
clusterView.setHealthChecker(cluster.getHealthChecker());
|
||||
@ -88,7 +87,7 @@ public class CatalogController {
|
||||
clusterView.setUseIPPort4Check(cluster.isUseIPPort4Check());
|
||||
clusterView.setDefaultPort(cluster.getDefaultPort());
|
||||
clusterView.setDefaultCheckPort(cluster.getDefaultCheckPort());
|
||||
clusterView.setServiceName(serviceName);
|
||||
clusterView.setServiceName(cluster.getService().getName());
|
||||
clusters.add(clusterView);
|
||||
}
|
||||
|
||||
@ -248,7 +247,7 @@ public class CatalogController {
|
||||
return ipAddressInfos;
|
||||
}
|
||||
|
||||
private JSONObject serviceList(HttpServletRequest request) throws Exception {
|
||||
private JSONObject serviceList(HttpServletRequest request) {
|
||||
|
||||
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
|
||||
Constants.DEFAULT_NAMESPACE_ID);
|
||||
|
@ -67,9 +67,7 @@ public class ClusterController {
|
||||
Cluster cluster = service.getClusterMap().get(clusterName);
|
||||
if (cluster == null) {
|
||||
Loggers.SRV_LOG.warn("[UPDATE-CLUSTER] cluster not exist, will create it: {}, service: {}", clusterName, serviceName);
|
||||
cluster = new Cluster();
|
||||
cluster.setName(clusterName);
|
||||
cluster.setService(service);
|
||||
cluster = new Cluster(clusterName, service);
|
||||
}
|
||||
|
||||
cluster.setDefCkport(NumberUtils.toInt(checkPort));
|
||||
|
@ -20,15 +20,18 @@ import com.alibaba.nacos.naming.healthcheck.HealthCheckReactor;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckStatus;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckTask;
|
||||
import com.alibaba.nacos.naming.misc.Loggers;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang3.builder.HashCodeBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author nkorange
|
||||
* @author jifengnan 2019-04-26
|
||||
*/
|
||||
public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implements Cloneable {
|
||||
|
||||
@ -59,11 +62,19 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
|
||||
|
||||
private Map<String, String> metadata = new ConcurrentHashMap<>();
|
||||
|
||||
public Cluster() {
|
||||
}
|
||||
|
||||
public Cluster(String clusterName) {
|
||||
/**
|
||||
* 创建一个集群(create a cluster)。
|
||||
* <p>集群名不能为空,且只能由阿拉伯数字,英文字母和减号(-)组成(the cluster name cannot be null, and only the arabic numerals, letters and endashes are allowed)。
|
||||
*
|
||||
* @param clusterName 集群名
|
||||
* @param service 服务
|
||||
* @throws IllegalArgumentException 服务为空,或者集群名为空,或者集群名不合法(the service is null, or the cluster name is null, or the cluster name is illegal)。
|
||||
* @since 1.1.0
|
||||
* @author jifengnan 2019-04-26
|
||||
*/
|
||||
public Cluster(String clusterName, Service service) {
|
||||
this.setName(clusterName);
|
||||
this.service = service;
|
||||
validate();
|
||||
}
|
||||
|
||||
@ -113,19 +124,40 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
|
||||
return service;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为当前的集群更换服务(replace the service for the current cluster)。
|
||||
* <p>不建议使用,集群所属的服务不应该允许被更改(服务内部的可变属性可以更改,但不应该将一个A服务的集群改成B服务)。
|
||||
* 如果一个集群对应的服务都变了,其实应该新建一个集群。
|
||||
* (Deprecated because the service shouldn't be replaced.
|
||||
* (the service fields can be changed, but the service A shouldn't be replaced to service B).
|
||||
* If the service of a cluster is required to replace, actually, a new cluster is required)
|
||||
*
|
||||
* @param service 服务
|
||||
*/
|
||||
@Deprecated
|
||||
public void setService(Service service) {
|
||||
this.service = service;
|
||||
this.setServiceName(service.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 该方法计划在未来被废除,请使用<code>getService.getName()</code>方法(this method is deprecated, please use <code>getService.getName()</code> instead)。
|
||||
*
|
||||
* @param serviceName 服务名
|
||||
* @since 1.1.0
|
||||
* @author jifengnan 2019-04-26
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public void setServiceName(String serviceName) {
|
||||
throw new UnsupportedOperationException("This method has been deprecated, please use getService.getName() instead.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cluster clone() throws CloneNotSupportedException {
|
||||
super.clone();
|
||||
Cluster cluster = new Cluster();
|
||||
|
||||
Cluster cluster = new Cluster(this.getName(), service);
|
||||
cluster.setHealthChecker(getHealthChecker().clone());
|
||||
cluster.setService(getService());
|
||||
cluster.persistentInstances = new HashSet<Instance>();
|
||||
cluster.persistentInstances = new HashSet<>();
|
||||
cluster.checkTask = null;
|
||||
cluster.metadata = new HashMap<>(metadata);
|
||||
return cluster;
|
||||
@ -263,16 +295,28 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getName());
|
||||
return new HashCodeBuilder(17, 37)
|
||||
.append(getName())
|
||||
.append(service)
|
||||
.toHashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (!(obj instanceof Cluster)) {
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return getName().equals(((Cluster) obj).getName());
|
||||
Cluster cluster = (Cluster) o;
|
||||
|
||||
return new EqualsBuilder()
|
||||
.append(getName(), cluster.getName())
|
||||
.append(service, cluster.service)
|
||||
.isEquals();
|
||||
}
|
||||
|
||||
public int getDefCkport() {
|
||||
@ -330,7 +374,15 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
|
||||
return persistentInstances.contains(ip) || ephemeralInstances.contains(ip);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证当前集群是否合法(validate the current cluster)。
|
||||
* <p>集群名不能为空,且只能由阿拉伯数字,英文字母和减号(-)组成(the cluster name cannot be null, and only the arabic numerals, letters and endashes are allowed)。
|
||||
*
|
||||
* @throws IllegalArgumentException 服务为空,或者集群名为空,或者集群名不合法(the service is null, or the cluster name is null, or the cluster name is illegal)。
|
||||
*/
|
||||
public void validate() {
|
||||
Assert.notNull(getName(), "cluster name cannot be null");
|
||||
Assert.notNull(service, "service cannot be null");
|
||||
if (!getName().matches(CLUSTER_NAME_SYNTAX)) {
|
||||
throw new IllegalArgumentException("cluster name can only have these characters: 0-9a-zA-Z-, current: " + getName());
|
||||
}
|
||||
|
@ -215,8 +215,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
|
||||
if (!clusterMap.containsKey(instance.getClusterName())) {
|
||||
Loggers.SRV_LOG.warn("cluster: {} not found, ip: {}, will create new cluster with default configuration.",
|
||||
instance.getClusterName(), instance.toJSON());
|
||||
Cluster cluster = new Cluster(instance.getClusterName());
|
||||
cluster.setService(this);
|
||||
Cluster cluster = new Cluster(instance.getClusterName(), this);
|
||||
cluster.init();
|
||||
getClusterMap().put(instance.getClusterName(), cluster);
|
||||
}
|
||||
|
@ -486,8 +486,7 @@ public class ServiceManager implements RecordListener<Service> {
|
||||
|
||||
for (Instance instance : ips) {
|
||||
if (!service.getClusterMap().containsKey(instance.getClusterName())) {
|
||||
Cluster cluster = new Cluster(instance.getClusterName());
|
||||
cluster.setService(service);
|
||||
Cluster cluster = new Cluster(instance.getClusterName(), service);
|
||||
cluster.init();
|
||||
service.getClusterMap().put(instance.getClusterName(), cluster);
|
||||
Loggers.SRV_LOG.warn("cluster: {} not found, ip: {}, will create new cluster with default configuration.",
|
||||
|
@ -36,6 +36,7 @@ import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
|
||||
|
||||
/**
|
||||
* @author nacos
|
||||
* @author jifengnan
|
||||
*/
|
||||
public class UtilsAndCommons {
|
||||
|
||||
@ -240,7 +241,7 @@ public class UtilsAndCommons {
|
||||
|
||||
/**
|
||||
* 根据指定的字符串计算出一个0(含)到{@code upperLimit}(不含)之间的数字,本方法会试图让不同的字符串较均匀的分布在0到{@code upperLimit}之间。
|
||||
* (Provide a number between 0(include) and {@code upperLimit}(exclude) for the given {@code string}, the number will be nearly uniform distribution.)
|
||||
* (Provide a number between 0(inclusive) and {@code upperLimit}(exclusive) for the given {@code string}, the number will be nearly uniform distribution.)
|
||||
* <p>
|
||||
* <p>
|
||||
* 举个例子:假设有N个提供相同服务的服务器地址被存在一个数组中,为了实现负载均衡,可以根据调用者的名字决定使用哪个服务器。
|
||||
@ -253,9 +254,10 @@ public class UtilsAndCommons {
|
||||
*
|
||||
* @param string 字符串。如果为null会固定返回0 (a string. the number 0 will be returned if it's null)
|
||||
* @param upperLimit 返回值的上限,必须为正整数(>0) (the upper limit of the returned number, must be a positive integer, which means > 0)
|
||||
* @return 0(含)到upperLimit(不含)之间的一个数字 (a number between 0(include) and upperLimit(exclude))
|
||||
* @return 0(含)到upperLimit(不含)之间的一个数字 (a number between 0(inclusive) and upperLimit(exclusive))
|
||||
* @throws IllegalArgumentException if the upper limit equals or less than 0
|
||||
* @since 1.0.0
|
||||
* @author jifengnan
|
||||
*/
|
||||
public static int shakeUp(String string, int upperLimit) {
|
||||
if (upperLimit < 1) {
|
||||
|
@ -15,15 +15,26 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming;
|
||||
|
||||
import com.alibaba.nacos.naming.boot.SpringContext;
|
||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
|
||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer;
|
||||
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet;
|
||||
import com.alibaba.nacos.naming.core.DistroMapper;
|
||||
import com.alibaba.nacos.naming.core.ServiceManager;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckProcessorDelegate;
|
||||
import com.alibaba.nacos.naming.misc.NetUtils;
|
||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||
import com.alibaba.nacos.naming.push.PushService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* @author nkorange
|
||||
@ -39,6 +50,9 @@ public class BaseTest {
|
||||
@Mock
|
||||
public RaftCore raftCore;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
@ -49,5 +63,27 @@ public class BaseTest {
|
||||
Mockito.when(peerSet.local()).thenReturn(peer);
|
||||
Mockito.when(peerSet.getLeader()).thenReturn(peer);
|
||||
Mockito.when(peerSet.isLeader(NetUtils.localServer())).thenReturn(true);
|
||||
|
||||
new SpringContext().setApplicationContext(context);
|
||||
doReturn(distroMapper).when(context).getBean(DistroMapper.class);
|
||||
doReturn(switchDomain).when(context).getBean(SwitchDomain.class);
|
||||
doReturn(delegate).when(context).getBean(HealthCheckProcessorDelegate.class);
|
||||
doReturn(pushService).when(context).getBean(PushService.class);
|
||||
}
|
||||
|
||||
protected static final String TEST_CLUSTER_NAME = "test-cluster";
|
||||
protected static final String TEST_SERVICE_NAME = "test-service";
|
||||
protected static final String TEST_GROUP_NAME = "test-group-name";
|
||||
protected static final String TEST_NAMESPACE = "test-namespace";
|
||||
|
||||
@Spy
|
||||
protected ApplicationContext context;
|
||||
@Mock
|
||||
protected DistroMapper distroMapper;
|
||||
@Spy
|
||||
protected SwitchDomain switchDomain;
|
||||
@Mock
|
||||
protected HealthCheckProcessorDelegate delegate;
|
||||
@Mock
|
||||
protected PushService pushService;
|
||||
}
|
||||
|
@ -0,0 +1,90 @@
|
||||
package com.alibaba.nacos.naming.controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.naming.BaseTest;
|
||||
import com.alibaba.nacos.naming.core.Cluster;
|
||||
import com.alibaba.nacos.naming.core.Service;
|
||||
import com.alibaba.nacos.naming.exception.NacosException;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.isA;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author jifengnan 2019-04-29
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MockServletContext.class)
|
||||
@WebAppConfiguration
|
||||
public class CatalogControllerTest extends BaseTest {
|
||||
@InjectMocks
|
||||
private CatalogController catalogController;
|
||||
|
||||
private MockMvc mockmvc;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
mockmvc = MockMvcBuilders.standaloneSetup(catalogController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceDetail() throws Exception {
|
||||
Service service = new Service(TEST_SERVICE_NAME);
|
||||
service.setNamespaceId(Constants.DEFAULT_NAMESPACE_ID);
|
||||
service.setProtectThreshold(12.34f);
|
||||
service.setGroupName(TEST_GROUP_NAME);
|
||||
Cluster cluster = new Cluster(TEST_CLUSTER_NAME, service);
|
||||
cluster.setDefaultPort(1);
|
||||
|
||||
service.addCluster(cluster);
|
||||
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
|
||||
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.get(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/catalog/service")
|
||||
.param("namespaceId", Constants.DEFAULT_NAMESPACE_ID)
|
||||
.param("serviceName", TEST_SERVICE_NAME);
|
||||
JSONObject result = JSONObject.parseObject(mockmvc.perform(builder).andReturn().getResponse().getContentAsString());
|
||||
|
||||
JSONObject serviceResult = (JSONObject) result.get("service");
|
||||
Assert.assertEquals(TEST_SERVICE_NAME, serviceResult.get("name"));
|
||||
Assert.assertEquals(12.34, Float.parseFloat(serviceResult.get("protectThreshold").toString()), 0.01);
|
||||
Assert.assertEquals(TEST_GROUP_NAME, serviceResult.get("groupName"));
|
||||
|
||||
JSONArray clusterResults = (JSONArray) result.get("clusters");
|
||||
Assert.assertEquals(1, clusterResults.size());
|
||||
JSONObject clusterResult = (JSONObject) clusterResults.get(0);
|
||||
Assert.assertEquals(TEST_CLUSTER_NAME, clusterResult.get("name"));
|
||||
Assert.assertEquals(1, Integer.parseInt(clusterResult.get("defaultPort").toString()));
|
||||
Assert.assertTrue(Boolean.parseBoolean(clusterResult.get("useIPPort4Check").toString()));
|
||||
Assert.assertEquals(TEST_SERVICE_NAME, clusterResult.get("serviceName"));
|
||||
Assert.assertEquals(80, Integer.parseInt(clusterResult.get("defaultCheckPort").toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceDetailNotFound() throws Exception {
|
||||
expectedException.expectCause(isA(NacosException.class));
|
||||
expectedException.expectMessage(containsString("serivce test-service is not found!"));
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.get(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/catalog/service")
|
||||
.param("namespaceId", Constants.DEFAULT_NAMESPACE_ID)
|
||||
.param("serviceName", TEST_SERVICE_NAME);
|
||||
mockmvc.perform(builder);
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
package com.alibaba.nacos.naming.controllers;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.naming.BaseTest;
|
||||
import com.alibaba.nacos.naming.boot.SpringContext;
|
||||
import com.alibaba.nacos.naming.core.Cluster;
|
||||
import com.alibaba.nacos.naming.core.DistroMapper;
|
||||
import com.alibaba.nacos.naming.core.Service;
|
||||
import com.alibaba.nacos.naming.exception.NacosException;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckProcessorDelegate;
|
||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.isA;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author jifengnan 2019-04-29
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MockServletContext.class)
|
||||
@WebAppConfiguration
|
||||
public class ClusterControllerTest extends BaseTest {
|
||||
@InjectMocks
|
||||
private ClusterController clusterController;
|
||||
|
||||
private MockMvc mockmvc;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
mockmvc = MockMvcBuilders.standaloneSetup(clusterController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() throws Exception {
|
||||
Service service = new Service(TEST_SERVICE_NAME);
|
||||
service.setNamespaceId("test-namespace");
|
||||
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
|
||||
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster")
|
||||
.param("clusterName", TEST_CLUSTER_NAME)
|
||||
.param("serviceName", TEST_SERVICE_NAME)
|
||||
.param("healthChecker", "{\"type\":\"HTTP\"}")
|
||||
.param("checkPort", "1")
|
||||
.param("useInstancePort4Check", "true");
|
||||
Assert.assertEquals("ok", mockmvc.perform(builder).andReturn().getResponse().getContentAsString());
|
||||
|
||||
Cluster expectedCluster = new Cluster(TEST_CLUSTER_NAME, service);
|
||||
Cluster actualCluster = service.getClusterMap().get(TEST_CLUSTER_NAME);
|
||||
|
||||
Assert.assertEquals(expectedCluster, actualCluster);
|
||||
Assert.assertEquals(1, actualCluster.getDefCkport());
|
||||
Assert.assertTrue(actualCluster.isUseIPPort4Check());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateNoService() throws Exception {
|
||||
expectedException.expectCause(isA(NacosException.class));
|
||||
expectedException.expectMessage("service not found:test-service-not-found");
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster")
|
||||
.param("clusterName", TEST_CLUSTER_NAME)
|
||||
.param("serviceName", "test-service-not-found")
|
||||
.param("healthChecker", "{\"type\":\"HTTP\"}")
|
||||
.param("checkPort", "1")
|
||||
.param("useInstancePort4Check", "true");
|
||||
mockmvc.perform(builder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateInvalidType() throws Exception {
|
||||
expectedException.expectCause(isA(NacosException.class));
|
||||
expectedException.expectMessage("unknown health check type:{\"type\":\"123\"}");
|
||||
Service service = new Service(TEST_SERVICE_NAME);
|
||||
service.setNamespaceId(Constants.DEFAULT_NAMESPACE_ID);
|
||||
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster")
|
||||
.param("clusterName", TEST_CLUSTER_NAME)
|
||||
.param("serviceName", TEST_SERVICE_NAME)
|
||||
.param("healthChecker", "{\"type\":\"123\"}")
|
||||
.param("checkPort", "1")
|
||||
.param("useInstancePort4Check", "true");
|
||||
mockmvc.perform(builder);
|
||||
}
|
||||
|
||||
}
|
@ -71,27 +71,25 @@ public class InstanceControllerTest extends BaseTest {
|
||||
public void registerInstance() throws Exception {
|
||||
|
||||
Service service = new Service();
|
||||
service.setName("nacos.test.1");
|
||||
service.setName(TEST_SERVICE_NAME);
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
cluster.setService(service);
|
||||
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, service);
|
||||
service.addCluster(cluster);
|
||||
|
||||
Instance instance = new Instance();
|
||||
instance.setIp("1.1.1.1");
|
||||
instance.setPort(9999);
|
||||
List<Instance> ipList = new ArrayList<Instance>();
|
||||
List<Instance> ipList = new ArrayList<>();
|
||||
ipList.add(instance);
|
||||
service.updateIPs(ipList, false);
|
||||
|
||||
Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, "nacos.test.1")).thenReturn(service);
|
||||
Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
|
||||
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.put("/naming/instance")
|
||||
.param("serviceName", "nacos.test.1")
|
||||
.param("ip", "1.1.1.1")
|
||||
.param("port", "9999");
|
||||
MockMvcRequestBuilders.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance")
|
||||
.param("serviceName", TEST_SERVICE_NAME)
|
||||
.param("ip", "1.1.1.1")
|
||||
.param("port", "9999");
|
||||
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
|
||||
|
||||
Assert.assertEquals("ok", actualValue);
|
||||
@ -101,11 +99,11 @@ public class InstanceControllerTest extends BaseTest {
|
||||
public void deregisterInstance() throws Exception {
|
||||
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.delete("/naming/instance")
|
||||
.param("serviceName", "nacos.test.1")
|
||||
.param("ip", "1.1.1.1")
|
||||
.param("port", "9999")
|
||||
.param("clusterName", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
MockMvcRequestBuilders.delete(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance")
|
||||
.param("serviceName", TEST_SERVICE_NAME)
|
||||
.param("ip", "1.1.1.1")
|
||||
.param("port", "9999")
|
||||
.param("clusterName", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
|
||||
|
||||
Assert.assertEquals("ok", actualValue);
|
||||
@ -115,34 +113,32 @@ public class InstanceControllerTest extends BaseTest {
|
||||
public void getInstances() throws Exception {
|
||||
|
||||
Service service = new Service();
|
||||
service.setName("nacos.test.1");
|
||||
service.setName(TEST_SERVICE_NAME);
|
||||
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
cluster.setService(service);
|
||||
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, service);
|
||||
service.addCluster(cluster);
|
||||
|
||||
Instance instance = new Instance();
|
||||
instance.setIp("10.10.10.10");
|
||||
instance.setPort(8888);
|
||||
instance.setWeight(2.0);
|
||||
List<Instance> ipList = new ArrayList<Instance>();
|
||||
instance.setServiceName(TEST_SERVICE_NAME);
|
||||
List<Instance> ipList = new ArrayList<>();
|
||||
ipList.add(instance);
|
||||
service.updateIPs(ipList, false);
|
||||
|
||||
Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, "nacos.test.1")).thenReturn(service);
|
||||
Mockito.when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
|
||||
|
||||
MockHttpServletRequestBuilder builder =
|
||||
MockMvcRequestBuilders.get("/v1/ns/instances")
|
||||
.param("serviceName", "nacos.test.1");
|
||||
MockMvcRequestBuilders.get(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance/list")
|
||||
.param("serviceName", TEST_SERVICE_NAME);
|
||||
|
||||
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
|
||||
String actualValue = response.getContentAsString();
|
||||
JSONObject result = JSON.parseObject(actualValue);
|
||||
|
||||
Assert.assertEquals("nacos.test.1", result.getString("serviceName"));
|
||||
Assert.assertEquals(TEST_SERVICE_NAME, result.getString("name"));
|
||||
JSONArray hosts = result.getJSONArray("hosts");
|
||||
Assert.assertTrue(hosts != null);
|
||||
Assert.assertNotNull(hosts);
|
||||
Assert.assertEquals(hosts.size(), 1);
|
||||
|
||||
|
@ -36,9 +36,7 @@ public class ClusterTest {
|
||||
Service service = new Service();
|
||||
service.setName("nacos.service.1");
|
||||
|
||||
cluster = new Cluster();
|
||||
cluster.setName("nacos-cluster-1");
|
||||
cluster.setService(service);
|
||||
cluster = new Cluster("nacos-cluster-1", service);
|
||||
cluster.setDefCkport(80);
|
||||
cluster.setDefIPPort(8080);
|
||||
}
|
||||
@ -46,8 +44,10 @@ public class ClusterTest {
|
||||
|
||||
@Test
|
||||
public void updateCluster() {
|
||||
Service service = new Service();
|
||||
service.setName("nacos.service.2");
|
||||
|
||||
Cluster newCluster = new Cluster();
|
||||
Cluster newCluster = new Cluster("nacos-cluster-1", service);
|
||||
newCluster.setDefCkport(8888);
|
||||
newCluster.setDefIPPort(9999);
|
||||
AbstractHealthChecker.Http healthCheckConfig = new AbstractHealthChecker.Http();
|
||||
@ -56,17 +56,12 @@ public class ClusterTest {
|
||||
healthCheckConfig.setHeaders("Client-Version:nacos-test-1");
|
||||
newCluster.setHealthChecker(healthCheckConfig);
|
||||
|
||||
Service service = new Service();
|
||||
service.setName("nacos.service.2");
|
||||
|
||||
newCluster.setService(service);
|
||||
|
||||
cluster.update(newCluster);
|
||||
|
||||
Assert.assertEquals(8888, cluster.getDefCkport());
|
||||
Assert.assertEquals(9999, cluster.getDefIPPort());
|
||||
Assert.assertTrue(cluster.getHealthChecker() instanceof AbstractHealthChecker.Http);
|
||||
AbstractHealthChecker.Http httpHealthCheck = (AbstractHealthChecker.Http)(cluster.getHealthChecker());
|
||||
AbstractHealthChecker.Http httpHealthCheck = (AbstractHealthChecker.Http) (cluster.getHealthChecker());
|
||||
Assert.assertEquals("/nacos-path-1", httpHealthCheck.getPath());
|
||||
Assert.assertEquals(500, httpHealthCheck.getExpectedResponseCode());
|
||||
Assert.assertEquals("Client-Version:nacos-test-1", httpHealthCheck.getHeaders());
|
||||
@ -83,7 +78,7 @@ public class ClusterTest {
|
||||
instance2.setIp("1.1.1.1");
|
||||
instance2.setPort(2345);
|
||||
|
||||
List<Instance> list = new ArrayList<Instance>();
|
||||
List<Instance> list = new ArrayList<>();
|
||||
list.add(instance1);
|
||||
list.add(instance2);
|
||||
|
||||
@ -97,4 +92,31 @@ public class ClusterTest {
|
||||
Assert.assertEquals("1.1.1.1", ips.get(1).getIp());
|
||||
Assert.assertEquals(2345, ips.get(1).getPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidate() {
|
||||
Service service = new Service("nacos.service.2");
|
||||
cluster = new Cluster("nacos-cluster-1", service);
|
||||
cluster.validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testValidateClusterNameNull() {
|
||||
Service service = new Service("nacos.service.2");
|
||||
cluster = new Cluster(null, service);
|
||||
cluster.validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testValidateServiceNull() {
|
||||
cluster = new Cluster("nacos-cluster-1", null);
|
||||
cluster.validate();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void testSetServiceName() {
|
||||
Service service = new Service("nacos.service.2");
|
||||
cluster = new Cluster("nacos-cluster-1", service);
|
||||
cluster.setServiceName(null);
|
||||
}
|
||||
}
|
||||
|
@ -15,31 +15,45 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.nacos.naming.boot.SpringContext;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckProcessorDelegate;
|
||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import com.alibaba.nacos.naming.push.PushService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* @author nkorange
|
||||
*/
|
||||
public class DomainTest {
|
||||
|
||||
private Service service;
|
||||
@Spy
|
||||
protected ApplicationContext context;
|
||||
@Mock
|
||||
protected PushService pushService;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
service = new Service();
|
||||
service.setName("nacos.service.1");
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
cluster.setService(service);
|
||||
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, service);
|
||||
service.addCluster(cluster);
|
||||
new SpringContext().setApplicationContext(context);
|
||||
doReturn(pushService).when(context).getBean(PushService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -48,9 +62,7 @@ public class DomainTest {
|
||||
Service newDomain = new Service();
|
||||
newDomain.setName("nacos.service.1");
|
||||
newDomain.setProtectThreshold(0.7f);
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
cluster.setService(newDomain);
|
||||
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, newDomain);
|
||||
newDomain.addCluster(cluster);
|
||||
|
||||
service.update(newDomain);
|
||||
@ -60,8 +72,7 @@ public class DomainTest {
|
||||
|
||||
@Test
|
||||
public void addCluster() {
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("nacos-cluster-1");
|
||||
Cluster cluster = new Cluster("nacos-cluster-1", service);
|
||||
|
||||
service.addCluster(cluster);
|
||||
|
||||
|
@ -0,0 +1,77 @@
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.nacos.naming.BaseTest;
|
||||
import com.alibaba.nacos.naming.boot.SpringContext;
|
||||
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.healthcheck.HealthCheckProcessorDelegate;
|
||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author jifengnan 2019-04-29
|
||||
*/
|
||||
public class ServiceManagerTest extends BaseTest {
|
||||
|
||||
@Spy
|
||||
private ServiceManager serviceManager;
|
||||
|
||||
@Mock
|
||||
private ConsistencyService consistencyService;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateIpAddresses() throws Exception {
|
||||
ReflectionTestUtils.setField(serviceManager, "consistencyService", consistencyService);
|
||||
Service service = new Service(TEST_SERVICE_NAME);
|
||||
service.setNamespaceId(TEST_NAMESPACE);
|
||||
Instance instance = new Instance("1.1.1.1", 1);
|
||||
instance.setClusterName(TEST_CLUSTER_NAME);
|
||||
List<Instance> instanceList = serviceManager.updateIpAddresses(service, UtilsAndCommons.UPDATE_INSTANCE_ACTION_ADD, true, instance);
|
||||
Assert.assertEquals(1, instanceList.size());
|
||||
Assert.assertEquals(instance, instanceList.get(0));
|
||||
Assert.assertEquals(1, service.getClusterMap().size());
|
||||
Assert.assertEquals(new Cluster(instance.getClusterName(), service), service.getClusterMap().get(TEST_CLUSTER_NAME));
|
||||
|
||||
Datum datam = new Datum();
|
||||
datam.key = KeyBuilder.buildInstanceListKey(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
|
||||
Instances instances = new Instances();
|
||||
instanceList.add(new Instance("2.2.2.2", 2));
|
||||
instances.setInstanceList(instanceList);
|
||||
datam.value = instances;
|
||||
when(consistencyService.get(KeyBuilder.buildInstanceListKey(TEST_NAMESPACE, TEST_SERVICE_NAME, true))).thenReturn(datam);
|
||||
service.getClusterMap().get(TEST_CLUSTER_NAME).updateIPs(instanceList, true);
|
||||
instanceList = serviceManager.updateIpAddresses(service, UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE, true, instance);
|
||||
Assert.assertEquals(1, instanceList.size());
|
||||
Assert.assertEquals(new Instance("2.2.2.2", 2), instanceList.get(0));
|
||||
Assert.assertEquals(1, service.getClusterMap().size());
|
||||
Assert.assertEquals(new Cluster(instance.getClusterName(), service), service.getClusterMap().get(TEST_CLUSTER_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateIpAddressesNoInstance() throws Exception {
|
||||
expectedException.expect(IllegalArgumentException.class);
|
||||
expectedException.expectMessage("ip list can not be empty, service: test-service, ip list: []");
|
||||
ReflectionTestUtils.setField(serviceManager, "consistencyService", consistencyService);
|
||||
Service service = new Service(TEST_SERVICE_NAME);
|
||||
service.setNamespaceId(TEST_NAMESPACE);
|
||||
serviceManager.updateIpAddresses(service, UtilsAndCommons.UPDATE_INSTANCE_ACTION_ADD, true);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.nacos.naming.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Spy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jifengnan 2019-04-28
|
||||
*/
|
||||
public class ServiceTest extends BaseTest {
|
||||
@Spy
|
||||
private Service service;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
super.before();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateIPs() {
|
||||
service.setName("test-service");
|
||||
List<Instance> instances = new ArrayList<>();
|
||||
Instance instance = new Instance("1.1.1.1", 1, "test-instance1");
|
||||
instances.add(instance);
|
||||
service.updateIPs(instances, true);
|
||||
Assert.assertEquals(instances, service.allIPs(true));
|
||||
|
||||
instances = new ArrayList<>();
|
||||
instance = new Instance();
|
||||
instance.setIp("2.2.2.2");
|
||||
instance.setPort(2);
|
||||
instances.add(instance);
|
||||
instances.add(null);
|
||||
service.updateIPs(instances, true);
|
||||
instances.remove(null);
|
||||
Assert.assertEquals(instances, service.allIPs(true));
|
||||
}
|
||||
}
|
@ -36,9 +36,6 @@ public class UtilsAndCommonsTest {
|
||||
MockEnvironment environment = new MockEnvironment();
|
||||
|
||||
Assert.assertEquals(DEFAULT_NACOS_NAMING_CONTEXT, environment.resolvePlaceholders(NACOS_NAMING_CONTEXT));
|
||||
|
||||
|
||||
Assert.assertEquals("/nacos/v1/ns", DEFAULT_NACOS_NAMING_CONTEXT);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
Loading…
Reference in New Issue
Block a user