服务降级:服务器繁忙,请稍后再试,不让客户端等待,并立即返回一个友好的提示(一般发生在 程序异常,超时,服务熔断触发服务降级,线程池、信号量 打满也会导致服务降级)
服务熔断 : 达到最大服务访问后,直接拒绝访问,然后调用服务降级的方法并返回友好提示(如保险丝一样)
服务限流 : 秒杀等高并发操作,严禁一窝蜂的过来拥挤,排队进入,一秒钟n个,有序进行
大致的rvice和controller层如下
***************control宁波水上乐园ler*****************package com.sky.springcloud.controller;import com.sky.springcloud.rvice.paymentrvice;import lombok.extern.slf4j.slf4j;import org.springframework.beans.factory.annotation.value;import org.springframework.web.bind.annotation.getmapping;import org.springframework.web.bind.annotation.pathvariable;import org.springframework.web.bind.annotation.restcontroller;import javax.annotation.resource;@restcontroller@slf4jpublic class paymentcontroller { @resource private paymentrvice paymentrvice; @value("${rver.port}") private string rverport; @getmapping("/payment/hystrix/ok/{id}") public string paymentinfo_ok(@pathvariable("id") integer id){ string result = paymentrvice.paymentinfo_ok(id); log.info("*****"+ result); return result; } @getmapping("/payment/hystrix/timeout/{id}") public string paymentinfo_timeout(@pathvariable("id") integer id){ string result = paymentrvice.payment_info_timeout(id);}*******************rvice******************** package com.sky.springcloud.rvice;import org.springframework.stereotype.rvice;import java.util.concurrent.timeunit;@rvicepublic class paymentrviceimpl implements paymentrvice{ @override public string paymentinfo_ok(integer id) { return "线程池:" + thread.currentthread().getname() + "paymentinfo_ok. id:" + id + "\t" + "~~~~~"; public string payment_info_timeout(integer id) { try { timeunit.conds.sleep(5); } catch (interruptedexception e) { e.printstacktrace(); } return "线程池:" + thread.currentthread().getname() + "paymentinfo_timeout. id:" + id + "\t" + "~~~~~";
在这种情况时,当通过浏览器访问 timeout这个方法,会三秒后返回结果,而当访问 ok 这个方法时,会直接返回结果(但是这是在访问量很少的时候,一旦访问量过多,访问ok时也会出现延迟,这里可以使用jmeter模拟两万个访问量,如下)
使用 jmeter 模拟后,再去访问ok,会明显出现加载的效果
在主启动类添加注解
@enablehystrix
package com.sky.springcloud;import org.spri杭州艺术学校ngframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.enableeurekaclient;import org.springframework.cloud.netflix.hystrix.enablehystrix;@springbootapplication@enableeurekaclient //启用eureka@enablehystrix //启用hystrixpublic class payment8001 { public static void main(string[] args) { springapplication.run(payment8001.class,args); }}
在原有的基础上,在rvice层加上如下注解
@hystrixcommand(fallbackmethod = "payment_info_timeouthandler",//当服务降级时,调用payment_info_timeouthandler方法 commandproperties = { @hystrixproperty(name = "execution.isolation.thread.timeoutinmilliconds", value = "3000")//设置时间为3秒,超过三秒就为时间超限
*****************改后的rvice*******************package com.sky.springcloud.rvice;import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand;import com.netflix.hystrix.contrib.javanica.annotation.hystrixproperty;import org.springframework.stereotype.rvice;import java.util.concurrent.timeunit;@rvicepublic class paymentrviceimpl implements paymentrvice{ @override public string paymentinfo_ok(integer id) { return "线程池:" + thread.currentthread().getname() + "paymentinfo_ok. id:" + id + "\t" + "~~~~~"; } @hystrixcommand(fallbackmethod = "payment_info_timeouthandler",commandproperties = { @hystrixproperty(name = "execution.isolation.thread.timeoutinmilliconds",value = "3000") }) public string payment_info_timeout(integer id) { try { timeunit.conds.sleep(5); } catch (interruptedexception e) { e.printstacktrace(); } // int a = 10 / 0; return "线程池:" + thread.currentthread().getname() + "paymentinfo_timeout. id:" + id + "\t" + "~~~~~"; } public string payment_info_timeouthandler(integer id){ return "线程超时或异常 " + thread.currentthread().getname(); }}
如上rvice所示,如果再次访问timeout需要等待五秒,但是hystrix设置的超时时间为三秒,所以当三秒后没有结果就会服务降级,从而调用timeouthandler这个指定 的方法来执行(或者有程序出错等情况也会服务降级)
@defaultproperties(defaultfallback = “gloub_test”)
package com.sky.springcloud.rvice;import com.netflix.hystrix.contrib.javanica.annotation.defaultproperties;import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand;import com.netflix.hystrix.contrib.javanica.annotation.hystrixproperty;import org.springframework.stereotype.rvice;@rvice@defaultproperties(defaultfallback = "gloub_test")public class paymentrviceimpl implements paymentrvice{ @override @hystrixcommand public string paymentinfo_ok(integer id) { int a = 10 / 0; return "线程池:" + thread.currentthread().getname() + "paymentinfo_ok. id:" + id + "\t" + "~~~~~"; } @hystrixcommand(fallbackmethod = "payment_info_timeouthandler",commandproperties = { @hystrixproperty(name = "execution.isolation.thread.timeoutinmilliconds",value = "3000") }) public string payment_info_timeout(integer id) { /* try { timeunit.conds.sleep(5); } catch (interruptedexception e) { e.printstacktrace(); }*/ 和颜 return "线程池:" + thread.currentthread().getname() + "paymentinfo_timeout. id:" + id + "\t" + "~~~~~"; public string payment_info_timeouthandler(integer id){ return "线程超时或异常 " + thread.currentthread().getname(); public string gloub_test(){ return "走了全局的";}
如上rvice所示,加了@hystrixcommand的方金水银水法里,
如果没指定fallbackmethod 就会默认去找全局的defaultfallback
这里 当paymentinfo_ok 方法出错时,会调用gloub_test方法
当payment_info_timeout出错时,会调用payment_info_timeouthandler方法
熔断机制是应对雪崩效应的一种微服务链路保护机制,当扇出链路的某个微服务出错不可用或者响应时间太长,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的相应信息.当检测到该节点微服务调用相应正常后,恢复调用链路.
在上面的基础上,改写rvice和controller(添加以下代码)
//是否开启服务熔断(断路器) @hystrixproperty(name = "circuitbreaker.enabled",value = "true"), //服务请求的次数 @hystrixproperty(name = "circuitbreaker.requestvolumethreshold",value = "10"), //时间的窗口期 @hystrixproperty(name = "circuitbreaker.sleepwindowinmilliconds", value = "10000"), //当失败率达到多少后发生熔断 @hystrixproperty(name = "circuitbreaker.errorthresholdpercentage",value = "60"), ******************rvice***************** @hystrixcommand(fallbackmethod = "paymentcircuitbreaker_fallback", commandproperties = { @hystrixproperty(name = "circuitbreaker.enabled",value = "true"), @hystrixproperty(name = "circuitbreaker.requestvolumethreshold",value = "10"), @hystrixproperty(name = "circuitbreaker.sleepwindowinmilliconds", value = "10000"), @hystrixproperty(name = "circuitbreaker.errorthresholdpercentage",value = "60"), }) public string paymentcircuitbreaker(@pathvariable("id") integer id){ if(id<0){ throw new runtimeexception("********** id不能为负"); } string rialnumber = idutil.simpleuuid(); return thread.currentthread().getname() + "\t" + rialnumber; } public string paymentcircuitbreaker_fallback(@pathvariable("id") integer id){ return "id 不能为负:" + id;************controller******************** @getmapping("/payment/circuit/{id}") string result = paymentrvice.paymentcircuitbreaker(id); log.info(result + "***********"); return result;
如上所示,当访问paymentcircuitbreaker时,如果id小于零则会有一个运行错误,这时候就会通过服务降级来调用paymentcircuitbreaker_fallback方法,当错误的次数达到60%的时候(自己设的),就会出现服务熔断,这个时候再访问id大于零的情况,也会发生服务降级,因为开起了服务熔断,需要慢慢的恢复正常.
到此这篇关于springcloud之hystrix的详细使用的文章就介绍到这了,更多相关springcloud hystrix使用内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 18:03:50,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/047db71ae3ba755e9a2b016233e46d7c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringCloud之Hystrix的详细使用.doc
本文 PDF 下载地址:SpringCloud之Hystrix的详细使用.pdf
留言与评论(共有 0 条评论) |