Search in sources :

Example 66 with ServiceInstance

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

the class CommonsInstanceDiscovery method marshall.

/**
 * Private helper that marshals the information from each instance into something that
 * Turbine can understand. Override this method for your own implementation.
 * @param serviceInstance
 * @return Instance
 */
Instance marshall(ServiceInstance serviceInstance) {
    String hostname = serviceInstance.getHost();
    String managementPort = serviceInstance.getMetadata().get("management.port");
    String port = managementPort == null ? String.valueOf(serviceInstance.getPort()) : managementPort;
    String cluster = getClusterName(serviceInstance);
    // TODO: where to get?
    Boolean status = Boolean.TRUE;
    if (hostname != null && cluster != null && status != null) {
        Instance instance = getInstance(hostname, port, cluster, status);
        Map<String, String> metadata = serviceInstance.getMetadata();
        boolean securePortEnabled = serviceInstance.isSecure();
        addMetadata(instance, hostname, port, securePortEnabled, port, metadata);
        return instance;
    } else {
        return null;
    }
}
Also used : Instance(com.netflix.turbine.discovery.Instance) ServiceInstance(org.springframework.cloud.client.ServiceInstance)

Example 67 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project micro-service by Lovnx.

the class ComputeController method add.

// static{
// Timer timer = new Timer();
// timer.schedule(new TimerTask(){
// @Override
// public void run() {
// num.set(0);;
// }
// }, 0, timeRound);
// }
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(@RequestParam Integer a, @RequestParam Integer b) {
    // num.incrementAndGet();
    // 
    // if (num.get() <= flag) {
    // ServiceInstance instance = client.getLocalServiceInstance();
    // Integer r = a + b;
    // logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
    // return "From Service-B, Result is " + r+"\nPort:"+instance.getPort();
    // }
    // return "调用次数超限,一分钟内最多只能调用10次!";
    InterfaceLimit limit = service.getEntityByPri(1);
    Jedis jedis = RedisUtils.getJedis();
    // redis存的超时时间
    String timeRound_1 = jedis.get("timeRound_1");
    // 如果不存在或者是不等于数据库设置值
    if (timeRound_1 == null || !limit.getUnitTime().toString().equals(timeRound_1)) {
        // 重新设置超时时间
        jedis.set("timeRound_1", limit.getUnitTime().toString());
        jedis.expire("num_1", limit.getUnitTime());
    }
    String num_1 = jedis.get("num_1");
    if (num_1 == null) {
        jedis.set("num_1", String.valueOf(0));
        jedis.expire("num_1", limit.getUnitTime());
    }
    jedis.incr("num_1");
    if (Integer.parseInt(jedis.get("num_1")) <= limit.getUnitNum()) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return "From Service-B, Result is " + r + "\nPort:" + instance.getPort();
    }
    return "调用次数超限!";
}
Also used : InterfaceLimit(com.lovnx.entity.InterfaceLimit) Jedis(redis.clients.jedis.Jedis) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 68 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.

@Test
public void should_convert_service_with_uri() {
    ServiceInstance service = new TestServiceInstance("test", URI.create("http://localhost/test"), Collections.emptyMap());
    Registration registration = new DefaultServiceInstanceConverter().convert(service);
    assertThat(registration.getName()).isEqualTo("test");
    assertThat(registration.getServiceUrl()).isEqualTo("http://localhost/test");
    assertThat(registration.getManagementUrl()).isEqualTo("http://localhost/test/actuator");
    assertThat(registration.getHealthUrl()).isEqualTo("http://localhost/test/actuator/health");
    assertThat(registration.getMetadata()).isEmpty();
}
Also used : 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 69 with ServiceInstance

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

the class DefaultServiceInstanceConverterTest method should_convert_with_defaults.

@Test
public void should_convert_with_defaults() {
    ServiceInstance service = new DefaultServiceInstance("test-1", "test", "localhost", 80, false);
    Registration registration = new DefaultServiceInstanceConverter().convert(service);
    assertThat(registration.getName()).isEqualTo("test");
    assertThat(registration.getServiceUrl()).isEqualTo("http://localhost:80");
    assertThat(registration.getManagementUrl()).isEqualTo("http://localhost:80/actuator");
    assertThat(registration.getHealthUrl()).isEqualTo("http://localhost:80/actuator/health");
}
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 70 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_custom_defaults.

@Test
public void should_convert_service_with_uri_and_custom_defaults() {
    DefaultServiceInstanceConverter converter = new DefaultServiceInstanceConverter();
    converter.setHealthEndpointPath("ping");
    converter.setManagementContextPath("mgmt");
    ServiceInstance service = new TestServiceInstance("test", URI.create("http://localhost/test"), Collections.emptyMap());
    Registration registration = converter.convert(service);
    assertThat(registration.getName()).isEqualTo("test");
    assertThat(registration.getServiceUrl()).isEqualTo("http://localhost/test");
    assertThat(registration.getManagementUrl()).isEqualTo("http://localhost/test/mgmt");
    assertThat(registration.getHealthUrl()).isEqualTo("http://localhost/test/mgmt/ping");
}
Also used : 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)

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