spring cloud--服务容错保护Hystrix

来源:互联网 发布:淘宝客qq群里的优惠券 编辑:程序博客网 时间:2024/05/17 03:23

简言之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@SpringBootApplicationpublic class ConsumerApplication {    @Bean    @LoadBalanced    RestTemplate restTemplate(){        return new RestTemplate();    }    public static void main(String[] args){        SpringApplication.run(ConsumerApplication.class, args);    }}

新增HelloService类:

@Servicepublic 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实例:

@RestControllerpublic 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-consumerserver.port=9000eureka.client.serviceUrl.defaultZone = http://peer1:1111/eureka/

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


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

@RestControllerpublic 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 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 傲风秦界篇 秦傲风 类傲风 傲风傲风 黎诺冷傲风全文免费 黎诺冷傲风全文阅读 冷傲风黎诺免费阅读 黎诺冷傲风全文480章免费 创世中文网傲风大结局 傲风全文阅读无弹窗 傲风全集下载 傲风无弹窗广告 傲风之光芒大陆 变身纪念 傲风归 傲风创世中文网 风行烈为何不写傲风了 穆少情动傲骄妻 临时老公太傲骄 临时老公傲骄 兵骄将傲 天才萌宝傲骄妻 错上蛇王傲骄蛇宝腹黑娘亲 傲骄妈咪超给力 傲骄校花痞高手 少爷别傲骄 米璐璐 总裁傲宠小骄妻 穆少情动傲骄妻免费阅读 千万婚契腹黑总裁的傲骄妻 萌宝双娇腹黑总裁傲骄妻 傲龙 我傻白甜老婆 傻白甜老婆 傻甜白老婆 傻白甜老婆陈平 农村傻大妮 村长的傻儿子 农村傻大妮儿 傻白老婆 犯傻的年纪 傻白甜