use of com.alibaba.nacos.client.naming.beat.BeatInfo in project Sermant by huaweicloud.
the class NacosHealthInterceptor method close.
@Override
protected void close() {
// 关闭nacos心跳发送
BeatInfo beatInfo = (BeatInfo) arguments[0];
beatInfo.setStopped(true);
LOGGER.info("Nacos heartbeat has been closed.");
}
use of com.alibaba.nacos.client.naming.beat.BeatInfo in project nacos by alibaba.
the class NamingHttpClientProxyTest method testSendBeat.
@Test
public void testSendBeat() throws Exception {
// given
NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
HttpRestResult<Object> a = new HttpRestResult<Object>();
a.setData("");
a.setCode(200);
when(nacosRestTemplate.exchangeForm(any(), any(), any(), any(), any(), any())).thenReturn(a);
SecurityProxy proxy = mock(SecurityProxy.class);
ServerListManager mgr = mock(ServerListManager.class);
when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
Properties props = new Properties();
ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
final Field nacosRestTemplateField = NamingHttpClientProxy.class.getDeclaredField("nacosRestTemplate");
nacosRestTemplateField.setAccessible(true);
nacosRestTemplateField.set(clientProxy, nacosRestTemplate);
BeatInfo beat = new BeatInfo();
// when
clientProxy.sendBeat(beat, true);
// then
verify(nacosRestTemplate, times(1)).exchangeForm(endsWith("/instance/beat"), any(), any(), any(), eq(HttpMethod.PUT), any());
}
use of com.alibaba.nacos.client.naming.beat.BeatInfo in project nacos by alibaba.
the class AutoDeregisterInstance_ITCase method autoRegDomClustersTest.
/**
* 指定cluster中(单个、多个)实例,客户端停止上报实例心跳,服务端自动注销实例,恢复心跳,服务端自动注册实例
*
* @throws Exception
*/
@Test
@Ignore("Nacos 2.0 does not support HTTP beat check any more, it uses gRPC instead.")
public void autoRegDomClustersTest() throws Exception {
String serviceName = randomDomainName();
naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
TimeUnit.SECONDS.sleep(5);
List<Instance> instances;
instances = naming.getAllInstances(serviceName);
Assert.assertEquals(1, instances.size());
NacosNamingService namingServiceImpl = (NacosNamingService) naming;
NamingTestUtils.getBeatReactorByReflection(namingServiceImpl).removeBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, "127.0.0.1", TEST_PORT);
verifyInstanceList(instances, 1, serviceName);
instances = naming.getAllInstances(serviceName);
Assert.assertEquals(1, instances.size());
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName);
beatInfo.setIp("127.0.0.1");
beatInfo.setPort(TEST_PORT);
beatInfo.setCluster("c1");
NamingTestUtils.getBeatReactorByReflection(namingServiceImpl).addBeatInfo(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName, beatInfo);
// TimeUnit.SECONDS.sleep(15);
verifyInstanceList(instances, 2, serviceName);
instances = naming.getAllInstances(serviceName);
Assert.assertEquals(1, instances.size());
instances = naming.getAllInstances(serviceName, Arrays.asList("c2"));
Assert.assertEquals(1, instances.size());
TimeUnit.SECONDS.sleep(5);
instances = naming.getAllInstances(serviceName, Arrays.asList("c1"));
Assert.assertEquals(1, instances.size());
}
use of com.alibaba.nacos.client.naming.beat.BeatInfo in project nacos by alibaba.
the class NamingHttpClientProxyTest method testUpdateBeatInfo.
@Test
public void testUpdateBeatInfo() throws Exception {
// given
BeatReactor mockBeatReactor = mock(BeatReactor.class);
Field dom2Beat = BeatReactor.class.getDeclaredField("dom2Beat");
ConcurrentHashMap<String, BeatInfo> beatMap = new ConcurrentHashMap<>();
String beatServiceName = "service1#127.0.0.1#10000";
beatMap.put(beatServiceName, new BeatInfo());
dom2Beat.setAccessible(true);
dom2Beat.set(mockBeatReactor, beatMap);
SecurityProxy proxy = mock(SecurityProxy.class);
ServerListManager mgr = mock(ServerListManager.class);
when(mgr.getServerList()).thenReturn(Arrays.asList("localhost"));
Properties props = new Properties();
ServiceInfoHolder holder = mock(ServiceInfoHolder.class);
NamingHttpClientProxy clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props, holder);
final Field mockBeatReactorField = NamingHttpClientProxy.class.getDeclaredField("beatReactor");
mockBeatReactorField.setAccessible(true);
mockBeatReactorField.set(clientProxy, mockBeatReactor);
Instance instance = new Instance();
instance.setInstanceId("id1");
instance.setIp("127.0.0.1");
instance.setPort(10000);
instance.setServiceName("service1");
instance.setClusterName("cluster1");
Set<Instance> beats = new HashSet<>();
beats.add(instance);
when(mockBeatReactor.buildKey("service1", "127.0.0.1", 10000)).thenReturn(beatServiceName);
// when
clientProxy.updateBeatInfo(beats);
// then
verify(mockBeatReactor, times(1)).addBeatInfo(eq("service1"), any());
}
use of com.alibaba.nacos.client.naming.beat.BeatInfo in project nacos by alibaba.
the class NamingHttpClientProxy method registerService.
@Override
public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with 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>(32);
params.put(CommonParams.NAMESPACE_ID, namespaceId);
params.put(CommonParams.SERVICE_NAME, groupedServiceName);
params.put(CommonParams.GROUP_NAME, groupName);
params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
params.put(IP_PARAM, instance.getIp());
params.put(PORT_PARAM, String.valueOf(instance.getPort()));
params.put(WEIGHT_PARAM, String.valueOf(instance.getWeight()));
params.put(REGISTER_ENABLE_PARAM, String.valueOf(instance.isEnabled()));
params.put(HEALTHY_PARAM, String.valueOf(instance.isHealthy()));
params.put(EPHEMERAL_PARAM, String.valueOf(instance.isEphemeral()));
params.put(META_PARAM, JacksonUtils.toJson(instance.getMetadata()));
reqApi(UtilAndComs.nacosUrlInstance, params, HttpMethod.POST);
}
Aggregations