Search in sources :

Example 1 with BeatInfo

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.");
}
Also used : BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo)

Example 2 with BeatInfo

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());
}
Also used : Field(java.lang.reflect.Field) ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 3 with BeatInfo

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());
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo) NacosNamingService(com.alibaba.nacos.client.naming.NacosNamingService) Ignore(org.junit.Ignore) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with BeatInfo

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());
}
Also used : ServiceInfoHolder(com.alibaba.nacos.client.naming.cache.ServiceInfoHolder) ServerListManager(com.alibaba.nacos.client.naming.core.ServerListManager) Instance(com.alibaba.nacos.api.naming.pojo.Instance) BeatReactor(com.alibaba.nacos.client.naming.beat.BeatReactor) BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo) Properties(java.util.Properties) Field(java.lang.reflect.Field) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SecurityProxy(com.alibaba.nacos.client.security.SecurityProxy) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with BeatInfo

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);
}
Also used : HashMap(java.util.HashMap) BeatInfo(com.alibaba.nacos.client.naming.beat.BeatInfo)

Aggregations

BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)8 Instance (com.alibaba.nacos.api.naming.pojo.Instance)4 Test (org.junit.Test)4 NacosNamingService (com.alibaba.nacos.client.naming.NacosNamingService)2 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)2 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)2 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)2 Field (java.lang.reflect.Field)2 Properties (java.util.Properties)2 Ignore (org.junit.Ignore)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 BeatReactor (com.alibaba.nacos.client.naming.beat.BeatReactor)1 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)1 NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1