【Spring Cloud】--服务容错保护Hystrix

简言之Hystrix的作用就是,在服务调用失败的时候,会返回一个错误的相应,而不是长时间的等待。

快速入门:
同样使用http://blog.csdn.net/wangpengzhi19891223/article/details/78840646中的例子对服务进行改造:

1,consumer中引入依赖:

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix</artifactId>
 </dependency>

这里写图片描述

1,ConsumerApplication.java 中使用注解开启断路器功能:

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

新增HelloService类:

@Service
public class HelloService {
    private final Logger logger = Logger.getLogger(HelloService.class);
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String helloService(){
        long start = System.currentTimeMillis();
        String result =  restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();

        logger.info("耗时:" + (System.currentTimeMillis() - start));
        return  result;
    }
    public String helloFallback(){
        return "error";
    }
}

改造ConsumerController,注入service实例:

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private HelloService helloService;

    @RequestMapping(value = "/consumer", method = RequestMethod.GET)
    public String helloConsumer(){
     return  helloService.helloService();
    }
}

同样给出application.properties中的内容:

spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone = http://peer1:1111/eureka/

启动项目,并关闭注册中心,访问:http://localhost:9000/consumer 返回:error,说明调用到了我们写断路器方法。


改造一下service工程中的提供服务的hello方法:

@RestController
public class HelloController {

    private final Logger logger = Logger.getLogger(HelloController.class);
    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index() throws Exception{
        ServiceInstance instance = client.getLocalServiceInstance();
        int sleepTime = new Random().nextInt(3000);
        logger.info("sleeptime"+sleepTime);
        Thread.sleep(sleepTime);
        logger.info("host:"+instance.getHost()+instance.getServiceId()+":"+instance.getPort());
        return "New Hello world server!";
    }
}

当随机数休眠时间大于2000时,返回error
当小于2000的时候返回:New Hello world server!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注网赚的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值