Search in sources :

Example 71 with ServiceInstance

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

the class DefaultServiceInstanceConverterTest method should_convert_service_with_uri_and_metadata_different_port.

@Test
public void should_convert_service_with_uri_and_metadata_different_port() {
    Map<String, String> metadata = new HashMap<>();
    metadata.put("health.path", "ping");
    metadata.put("management.context-path", "mgmt");
    metadata.put("management.port", "1234");
    metadata.put("management.address", "127.0.0.1");
    ServiceInstance service = new TestServiceInstance("test", URI.create("http://localhost/test"), metadata);
    Registration registration = new DefaultServiceInstanceConverter().convert(service);
    assertThat(registration.getName()).isEqualTo("test");
    assertThat(registration.getServiceUrl()).isEqualTo("http://localhost/test");
    assertThat(registration.getManagementUrl()).isEqualTo("http://127.0.0.1:1234/mgmt");
    assertThat(registration.getHealthUrl()).isEqualTo("http://127.0.0.1:1234/mgmt/ping");
    assertThat(registration.getMetadata()).isEqualTo(metadata);
}
Also used : HashMap(java.util.HashMap) Registration(de.codecentric.boot.admin.server.domain.values.Registration) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.jupiter.api.Test)

Example 72 with ServiceInstance

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

the class DefaultServiceInstanceConverterTest method should_convert_with_custom_defaults.

@Test
public void should_convert_with_custom_defaults() {
    DefaultServiceInstanceConverter converter = new DefaultServiceInstanceConverter();
    converter.setHealthEndpointPath("ping");
    converter.setManagementContextPath("mgmt");
    ServiceInstance service = new DefaultServiceInstance("test-1", "test", "localhost", 80, false);
    Registration registration = converter.convert(service);
    assertThat(registration.getName()).isEqualTo("test");
    assertThat(registration.getServiceUrl()).isEqualTo("http://localhost:80");
    assertThat(registration.getManagementUrl()).isEqualTo("http://localhost:80/mgmt");
    assertThat(registration.getHealthUrl()).isEqualTo("http://localhost:80/mgmt/ping");
}
Also used : DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) Registration(de.codecentric.boot.admin.server.domain.values.Registration) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.jupiter.api.Test)

Example 73 with ServiceInstance

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

the class InstanceDiscoveryListenerTest method should_remove_instances_when_they_are_no_longer_available_in_discovery.

@Test
public void should_remove_instances_when_they_are_no_longer_available_in_discovery() {
    StepVerifier.create(this.registry.register(Registration.create("ignored", "http://health").build())).consumeNextWith((id) -> {
    }).verifyComplete();
    StepVerifier.create(this.registry.register(Registration.create("different-source", "http://health2").source("http-api").build())).consumeNextWith((id) -> {
    }).verifyComplete();
    this.listener.setIgnoredServices(singleton("ignored"));
    List<ServiceInstance> instances = new ArrayList<>();
    instances.add(new DefaultServiceInstance("test-1", "service", "localhost", 80, false));
    instances.add(new DefaultServiceInstance("test-1", "service", "example.net", 80, false));
    when(this.discovery.getServices()).thenReturn(singletonList("service"));
    when(this.discovery.getInstances("service")).thenReturn(instances);
    this.listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
    StepVerifier.create(this.registry.getInstances("service")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("service")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("service")).verifyComplete();
    StepVerifier.create(this.registry.getInstances("ignored")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("ignored")).verifyComplete();
    StepVerifier.create(this.registry.getInstances("different-source")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("different-source")).verifyComplete();
    instances.remove(0);
    this.listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
    StepVerifier.create(this.registry.getInstances("service")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("service")).verifyComplete();
    StepVerifier.create(this.registry.getInstances("ignored")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("ignored")).verifyComplete();
    StepVerifier.create(this.registry.getInstances("different-source")).assertNext((a) -> assertThat(a.getRegistration().getName()).isEqualTo("different-source")).verifyComplete();
    // shouldn't deregister a second time
    this.listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
    verify(this.registry, times(1)).deregister(any(InstanceId.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) HashingInstanceUrlIdGenerator(de.codecentric.boot.admin.server.services.HashingInstanceUrlIdGenerator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) InstanceRegisteredEvent(org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent) HashMap(java.util.HashMap) DiscoveryClient(org.springframework.cloud.client.discovery.DiscoveryClient) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) EventsourcingInstanceRepository(de.codecentric.boot.admin.server.domain.entities.EventsourcingInstanceRepository) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) InstanceRegistry(de.codecentric.boot.admin.server.services.InstanceRegistry) InMemoryEventStore(de.codecentric.boot.admin.server.eventstore.InMemoryEventStore) InstanceRepository(de.codecentric.boot.admin.server.domain.entities.InstanceRepository) Registration(de.codecentric.boot.admin.server.domain.values.Registration) ParentHeartbeatEvent(org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) ParentHeartbeatEvent(org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent) InstanceId(de.codecentric.boot.admin.server.domain.values.InstanceId) ArrayList(java.util.ArrayList) DefaultServiceInstance(org.springframework.cloud.client.DefaultServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Test(org.junit.jupiter.api.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