Search in sources :

Example 16 with ServiceInstance

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

the class DiscoveryClientConfigServiceBootstrapConfiguration method refresh.

private void refresh() {
    try {
        String serviceId = this.config.getDiscovery().getServiceId();
        ServiceInstance server = this.instanceProvider.getConfigServerInstance(serviceId);
        String url = getHomePage(server);
        if (server.getMetadata().containsKey("password")) {
            String user = server.getMetadata().get("user");
            user = user == null ? "user" : user;
            this.config.setUsername(user);
            String password = server.getMetadata().get("password");
            this.config.setPassword(password);
        }
        if (server.getMetadata().containsKey("configPath")) {
            String path = server.getMetadata().get("configPath");
            if (url.endsWith("/") && path.startsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            url = url + path;
        }
        this.config.setUri(url);
    } catch (Exception ex) {
        if (config.isFailFast()) {
            throw ex;
        } else {
            logger.warn("Could not locate configserver via discovery", ex);
        }
    }
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 17 with ServiceInstance

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

the class ConfigServerInstanceProvider method getConfigServerInstance.

@Retryable(interceptor = "configServerRetryInterceptor")
public ServiceInstance getConfigServerInstance(String serviceId) {
    logger.debug("Locating configserver (" + serviceId + ") via discovery");
    List<ServiceInstance> instances = this.client.getInstances(serviceId);
    if (instances.isEmpty()) {
        throw new IllegalStateException("No instances found of configserver (" + serviceId + ")");
    }
    ServiceInstance instance = instances.get(0);
    logger.debug("Located configserver (" + serviceId + ") via discovery: " + instance);
    return instance;
}
Also used : ServiceInstance(org.springframework.cloud.client.ServiceInstance) Retryable(org.springframework.retry.annotation.Retryable)

Example 18 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("carapp");
    // 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.car.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 19 with ServiceInstance

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

the class RibbonLoadBalancerClientTests method testReconstructURI.

private void testReconstructURI(String scheme) throws Exception {
    RibbonServer server = getRibbonServer();
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI uri = client.reconstructURI(serviceInstance, new URL(scheme + "://" + server.getServiceId()).toURI());
    assertThat(uri).hasScheme(scheme).hasHost(serviceInstance.getHost()).hasPort(serviceInstance.getPort());
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) URL(java.net.URL)

Example 20 with ServiceInstance

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

the class RibbonLoadBalancerClientTests method testReconstructHonorsRibbonServerScheme.

@Test
public void testReconstructHonorsRibbonServerScheme() {
    RibbonServer server = new RibbonServer("testService", new Server("ws", "myhost", 9080), false, Collections.singletonMap("mykey", "myvalue"));
    IClientConfig config = mock(IClientConfig.class);
    when(config.get(CommonClientConfigKey.IsSecure)).thenReturn(false);
    when(clientFactory.getClientConfig(server.getServiceId())).thenReturn(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    ServiceInstance serviceInstance = client.choose(server.getServiceId());
    URI uri = client.reconstructURI(serviceInstance, URI.create("http://testService"));
    assertThat(uri).hasScheme("ws").hasHost("myhost").hasPort(9080);
}
Also used : RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) Server(com.netflix.loadbalancer.Server) IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) 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