Search in sources :

Example 1 with HelloRequest

use of com.demo.protocol.HelloRequest 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 2 with HelloRequest

use of com.demo.protocol.HelloRequest in project SpringCloudDemo by RickJou.

the class FeignController method helloConsumer2.

@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String helloConsumer2(RestTemplate restTemplate) {
    log.info("feign cosumer2 ......");
    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:" + eurekaConsulmerFeignValue);
    hr.setRandomId((new Random()).nextLong());
    return sayHelloServer.sayHello2(hr).toString();
}
Also used : Random(java.util.Random) HelloRequest(com.demo.protocol.HelloRequest) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

HelloRequest (com.demo.protocol.HelloRequest)2 Random (java.util.Random)2 ServiceInstance (org.springframework.cloud.client.ServiceInstance)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 HystrixCommand (com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1