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());
}
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();
}
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();
}
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();
}
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;
}
Aggregations