Search in sources :

Example 1 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-boot-admin by codecentric.

the class ApplicationDiscoveryListener method discover.

protected void discover() {
    final Set<String> staleApplicationIds = getAllApplicationIdsFromRegistry();
    for (String serviceId : discoveryClient.getServices()) {
        if (!ignoreService(serviceId) && registerService(serviceId)) {
            for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
                String applicationId = register(instance);
                staleApplicationIds.remove(applicationId);
            }
        } else {
            LOGGER.debug("Ignoring discovered service {}", serviceId);
        }
    }
    for (String staleApplicationId : staleApplicationIds) {
        LOGGER.info("Application ({}) missing in DiscoveryClient services ", staleApplicationId);
        registry.deregister(staleApplicationId);
    }
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 2 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-boot-admin by codecentric.

the class ApplicationDiscoveryListenerTest method test_ignore.

@Test
public void test_ignore() {
    when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
    when(discovery.getInstances("service")).thenReturn(Collections.singletonList((ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
    listener.setIgnoredServices(singleton("service"));
    listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
    assertEquals(0, registry.getApplications().size());
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.Test)

Example 3 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-boot-admin by codecentric.

the class ApplicationDiscoveryListenerTest method test_register_and_convert.

@Test
public void test_register_and_convert() {
    when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
    when(discovery.getInstances("service")).thenReturn(Collections.singletonList((ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
    listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
    assertEquals(1, registry.getApplications().size());
    Application application = registry.getApplications().iterator().next();
    assertEquals("http://localhost:80/health", application.getHealthUrl());
    assertEquals("http://localhost:80", application.getManagementUrl());
    assertEquals("http://localhost:80", application.getServiceUrl());
    assertEquals("service", application.getName());
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Application(de.codecentric.boot.admin.model.Application) Test(org.junit.Test)

Example 4 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-boot-admin by codecentric.

the class ApplicationDiscoveryListenerTest method test_ignore_pattern.

@Test
public void test_ignore_pattern() {
    when(discovery.getServices()).thenReturn(asList("service", "rabbit-1", "rabbit-2"));
    when(discovery.getInstances("service")).thenReturn(Collections.singletonList((ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
    listener.setIgnoredServices(singleton("rabbit-*"));
    listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
    Collection<Application> applications = registry.getApplications();
    assertEquals(1, applications.size());
    assertEquals("service", applications.iterator().next().getName());
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Application(de.codecentric.boot.admin.model.Application) Test(org.junit.Test)

Example 5 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-boot-admin by codecentric.

the class ApplicationDiscoveryListenerTest method single_discovery_for_same_heartbeat.

@Test
public void single_discovery_for_same_heartbeat() {
    Object heartbeat = new Object();
    listener.onParentHeartbeat(new ParentHeartbeatEvent(new Object(), heartbeat));
    when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
    when(discovery.getInstances("service")).thenReturn(Collections.singletonList((ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
    listener.onApplicationEvent(new HeartbeatEvent(new Object(), heartbeat));
    assertEquals(0, registry.getApplications().size());
    listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
    assertEquals(1, registry.getApplications().size());
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ParentHeartbeatEvent(org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) ParentHeartbeatEvent(org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent) Test(org.junit.Test)

Aggregations

ServiceInstance (org.springframework.cloud.client.ServiceInstance)73 DefaultServiceInstance (org.springframework.cloud.client.DefaultServiceInstance)25 Test (org.junit.Test)24 HashMap (java.util.HashMap)12 URI (java.net.URI)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Registration (de.codecentric.boot.admin.server.domain.values.Registration)10 ArrayList (java.util.ArrayList)10 Test (org.junit.jupiter.api.Test)10 Application (de.codecentric.boot.admin.model.Application)7 Map (java.util.Map)7 List (java.util.List)6 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Random (java.util.Random)4 Collections (java.util.Collections)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 MonitorProperties (com.alibaba.druid.admin.config.MonitorProperties)2 ServiceNode (com.alibaba.druid.admin.model.ServiceNode)2 ConnectionResult (com.alibaba.druid.admin.model.dto.ConnectionResult)2