Search in sources :

Example 11 with ServiceInstance

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

the class ConsulDiscoveryServiceTest 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(consulDiscoveryClient.getInstances(someServiceId)).thenReturn(Lists.newArrayList(someServiceInstance));
    List<ServiceDTO> serviceDTOList = consulDiscoveryService.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 12 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.

the class FeignController method helloConsumer.

@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String helloConsumer(RestTemplate restTemplate) {
    log.info("feign consumer1 ......");
    ServiceInstance serverInstance = lbc.choose("EUREKA-PRODUCER");
    // 如果没有服务提供者,那么LoadBalancerClient不能够获取实例,将会导致空指针异常,此时将会使得服务降级不可用.
    if (serverInstance != null) {
        log.info("load blance is:" + serverInstance.getHost() + ":" + serverInstance.getPort() + "/" + serverInstance.getServiceId());
    }
    return sayHelloServer.sayHello("alan", "Hello Feign Client!" + "configure center get value commonSettingValue:" + commonSettingValue + ",eurekaProducerValue:" + eurekaConsulmerFeignValue, (new Random()).nextLong() + "").toString();
}
Also used : Random(java.util.Random) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.

the class RibbonController method helloConsumer2.

/**
 * 简化版post请求,直接将对象作为消息体提交
 *
 * @return
 */
@SuppressWarnings("rawtypes")
@HystrixCommand(fallbackMethod = "helloConsumer2FullBack")
@RequestMapping(value = "/ribbon-consumer2", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String helloConsumer2() {
    ServiceInstance serverInstance = lbc.choose("EUREKA-PRODUCER");
    // 如果没有服务提供者,那么LoadBalancerClient不能够获取实例,将会导致空指针异常,此时将会使得服务降级不可用.
    if (serverInstance != null) {
        log.info("load blance is:" + serverInstance.getHost() + ":" + serverInstance.getPort() + "/" + serverInstance.getServiceId());
    }
    HelloRequest hr = new HelloRequest();
    hr.setName("alan");
    hr.setSay("configure center get value commonSettingValue:" + commonSettingValue + ",eurekaProducerValue:" + eurekaConsumerRibbonValue);
    hr.setRandomId((new Random()).nextLong());
    Map response = restTemplate.postForObject(url2, hr, Map.class);
    return response.toString();
}
Also used : Random(java.util.Random) HelloRequest(com.demo.protocol.HelloRequest) ServiceInstance(org.springframework.cloud.client.ServiceInstance) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HystrixCommand(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.

the class RibbonController method helloConsumer.

/**
 * 自定义消息头消息体 post请求服务提供者demo
 *
 * @return
 */
@SuppressWarnings("rawtypes")
@HystrixCommand(fallbackMethod = "helloConsumerFullBack")
@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String helloConsumer() {
    ServiceInstance serverInstance = lbc.choose("EUREKA-PRODUCER");
    // 如果没有服务提供者,那么LoadBalancerClient不能够获取实例,将会导致空指针异常,此时将会使得服务降级不可用.
    if (serverInstance != null) {
        log.info("load blance is:" + serverInstance.getHost() + ":" + serverInstance.getPort() + "/" + serverInstance.getServiceId());
    }
    // head
    HttpHeaders headers = new HttpHeaders();
    List<MediaType> accept = new LinkedList<>();
    accept.add(MediaType.APPLICATION_JSON_UTF8);
    headers.setAccept(accept);
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    // body
    MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<>();
    postParameters.add("name", "alan");
    postParameters.add("say", "configure center get value commonSettingValue:" + commonSettingValue + ",eurekaProducerValue:" + eurekaConsumerRibbonValue);
    postParameters.add("randomId", (new Random()).nextInt() + "");
    // entity
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(postParameters, headers);
    ResponseEntity<Map> response = restTemplate.postForEntity(url1, requestEntity, Map.class);
    return response.getBody().toString();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ServiceInstance(org.springframework.cloud.client.ServiceInstance) LinkedList(java.util.LinkedList) Random(java.util.Random) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HystrixCommand(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with ServiceInstance

use of org.springframework.cloud.client.ServiceInstance in project SpringCloudDemo by RickJou.

the class HelloController method sayHello.

// 使用该注解描述接口方法信息
@ApiOperation(value = "sayHello", notes = "sayHello http body 参数版")
@ApiImplicitParams({ /*
			 * paramType:参数所在输协议中的位置 header-->@RequestHeader
			 * query-->@RequestParam path(用于restful接口)-->@PathVariable body(不常用)
			 * form(不常用)
			 **/
@ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "say", value = "说点啥", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "randomId", value = "随机数", required = true, dataType = "String", paramType = "query") })
@RequestMapping(value = "/sayHello", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public Map sayHello(@RequestParam String name, @RequestParam String say, @RequestParam String randomId) {
    ServiceInstance instance = discoveryClient.getLocalServiceInstance();
    log.info("discovery client output:" + instance.getHost() + ":" + instance.getPort() + "/" + instance.getServiceId());
    log.info("进入服务生产者");
    Map map = new HashMap<String, String>();
    map.put("name", name);
    map.put("say", say);
    map.put("randomId", randomId);
    log.info("服务生产者返回数据");
    return map;
}
Also used : HashMap(java.util.HashMap) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HashMap(java.util.HashMap) Map(java.util.Map) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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