Search in sources :

Example 61 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project apollo by ctripcorp.

the class ZookeeperDiscoveryServiceTest method testGetServiceInstances.

@Test
public void testGetServiceInstances() {
    String someIp = "1.2.3.4";
    int somePort = 8080;
    String someInstanceId = "someInstanceId";
    ServiceInstance someServiceInstance = mockServiceInstance(someInstanceId, someIp, somePort);
    when(zookeeperDiscoveryClient.getInstances(someServiceId)).thenReturn(Lists.newArrayList(someServiceInstance));
    List<ServiceDTO> serviceDTOList = zookeeperDiscoveryService.getServiceInstances(someServiceId);
    ServiceDTO serviceDTO = serviceDTOList.get(0);
    assertEquals(1, serviceDTOList.size());
    assertEquals(someServiceId, serviceDTO.getAppName());
    assertEquals("http://1.2.3.4:8080/", serviceDTO.getHomepageUrl());
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) Test(org.junit.Test)

Example 62 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project apollo by ctripcorp.

the class ZookeeperDiscoveryServiceTest method mockServiceInstance.

private ServiceInstance mockServiceInstance(String instanceId, String ip, int port) {
    ServiceInstance serviceInstance = mock(ServiceInstance.class);
    when(serviceInstance.getInstanceId()).thenReturn(instanceId);
    when(serviceInstance.getHost()).thenReturn(ip);
    when(serviceInstance.getPort()).thenReturn(port);
    return serviceInstance;
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 63 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project tutorials by eugenp.

the class CacheConfiguration method hazelcastInstance.

@Bean
public HazelcastInstance hazelcastInstance(JHipsterProperties jHipsterProperties) {
    log.debug("Configuring Hazelcast");
    Config config = new Config();
    config.setInstanceName("gateway");
    // The serviceId is by default the application's name, see Spring Boot's eureka.instance.appname property
    String serviceId = discoveryClient.getLocalServiceInstance().getServiceId();
    log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId);
    // In development, everything goes through 127.0.0.1, with a different port
    if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) {
        log.debug("Application is running with the \"dev\" profile, Hazelcast " + "cluster will only work with localhost instances");
        System.setProperty("hazelcast.local.localAddress", "127.0.0.1");
        config.getNetworkConfig().setPort(serverProperties.getPort() + 5701);
        config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
        for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
            String clusterMember = "127.0.0.1:" + (instance.getPort() + 5701);
            log.debug("Adding Hazelcast (dev) cluster member " + clusterMember);
            config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember);
        }
    } else {
        // Production configuration, one host per instance all using port 5701
        config.getNetworkConfig().setPort(5701);
        config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
        for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
            String clusterMember = instance.getHost() + ":5701";
            log.debug("Adding Hazelcast (prod) cluster member " + clusterMember);
            config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember);
        }
    }
    config.getMapConfigs().put("default", initializeDefaultMapConfig());
    config.getMapConfigs().put("com.gateway.domain.*", initializeDomainMapConfig(jHipsterProperties));
    return Hazelcast.newHazelcastInstance(config);
}
Also used : Config(com.hazelcast.config.Config) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 64 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project tutorials by eugenp.

the class CacheConfiguration method hazelcastInstance.

@Bean
public HazelcastInstance hazelcastInstance(JHipsterProperties jHipsterProperties) {
    log.debug("Configuring Hazelcast");
    Config config = new Config();
    config.setInstanceName("dealerapp");
    // The serviceId is by default the application's name, see Spring Boot's eureka.instance.appname property
    String serviceId = discoveryClient.getLocalServiceInstance().getServiceId();
    log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId);
    // In development, everything goes through 127.0.0.1, with a different port
    if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) {
        log.debug("Application is running with the \"dev\" profile, Hazelcast " + "cluster will only work with localhost instances");
        System.setProperty("hazelcast.local.localAddress", "127.0.0.1");
        config.getNetworkConfig().setPort(serverProperties.getPort() + 5701);
        config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
        for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
            String clusterMember = "127.0.0.1:" + (instance.getPort() + 5701);
            log.debug("Adding Hazelcast (dev) cluster member " + clusterMember);
            config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember);
        }
    } else {
        // Production configuration, one host per instance all using port 5701
        config.getNetworkConfig().setPort(5701);
        config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
        for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
            String clusterMember = instance.getHost() + ":5701";
            log.debug("Adding Hazelcast (prod) cluster member " + clusterMember);
            config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember);
        }
    }
    config.getMapConfigs().put("default", initializeDefaultMapConfig());
    config.getMapConfigs().put("com.dealer.app.domain.*", initializeDomainMapConfig(jHipsterProperties));
    return Hazelcast.newHazelcastInstance(config);
}
Also used : Config(com.hazelcast.config.Config) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 65 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancerClientTests method testChooseMissing.

@Test
public void testChooseMissing() {
    given(this.clientFactory.getLoadBalancer(this.loadBalancer.getName())).willReturn(null);
    given(this.loadBalancer.getName()).willReturn("missingservice");
    RibbonLoadBalancerClient client = new RibbonLoadBalancerClient(this.clientFactory);
    ServiceInstance instance = client.choose("missingservice");
    assertNull("instance wasn't null", instance);
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) 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