Hystrix

服务降级

服务降级

一般是用在客户端比较多,两个都可以

1.0 controller一一加上

其实controller和service都可以,因为自己试过了

注意的点:

  1. fallback方法的参数一定要一样
  2. Command和Property注解要一起用,property表示阈值
//启动类加注解
@EnableCircuitBreaker

@RestController

public class ConsumerHystrixFeignController {
    @Resource
    private ConsumerFeignHystrix consumerFeignHystrix;

    @HystrixCommand(fallbackMethod = "swipeAss",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })
    @GetMapping("consumer/payment/hystrix/timeout/{id}")
    //服务器端是健康的 但是这里更加严格
    public String timeOut(@PathVariable("id") int id){
        return consumerFeignHystrix.paymentInfo_TimeOut(id);
    }
    public String swipeAss(int id){
        return "回去写作业把";
    }
}

问题1

代码膨胀,一个个写不如去卖板面

解决1-全局

  1. 这个时候默认方法不需要参数也对应了

  2. 只要加了Hystrix就可以用默认的,要指定的话自己加properties

@RestController
@DefaultProperties(defaultFallback = "swipeAss",commandProperties = {
        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "2000")
})
public class ConsumerHystrixFeign {
    @Resource
    private ConsumerFeignHystrix consumerFeignHystrix;

    @GetMapping("consumer/payment/hystrix/ok/{id}")
    public String ok(@PathVariable("id") int id){
        return consumerFeignHystrix.paymentInfo_OK(id);
    }

    @HystrixCommand
    @GetMapping("consumer/payment/hystrix/timeout/{id}")
    //服务器端是健康的 但是这里更加严格
    public String timeOut(@PathVariable("id") int id){
        return consumerFeignHystrix.paymentInfo_TimeOut(id);
    }
    public String swipeAss(){
        return "回去写作业把";
    }
}

问题2

但还是有问题,业务类插杂了其他代码,高耦合

解决2-接口上操作

之前的解决是在controller,现在则是针对service。

新建一个类实现service接口,统一为里面的方法进行异常处理。

2_1实现接口的fallback类

@Component
public class ConsumerFallbackService implements ConsumerFeignHystrix{
    @Override
    public String paymentInfo_OK(Integer id) {
        return "666_ok";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "666_ot";
    }
}

2_2接口本身加上注解

@Component
@FeignClient(value ="CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = ConsumerFallbackService.class)
public interface ConsumerFeignHystrix {

2_3yaml开启

feign:
  hystrix:
    enabled: true

服务熔断

搞清楚这一点就ok:不是服务内部问题,如果是内部问题怎么恢复你和我说?

只能够是请求参数的问题,成功率过低就熔断了。

流程就是一直监控成功率,如果低了就熔断,然后随着成功率上升逐渐恢复到成功

感觉和1.0很像,在service上配置就可以了。

   //注解都一样
	@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),  //是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),   //请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),  //时间范围
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸
    })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        if (id < 0){
            throw new RuntimeException("*****id 不能负数");
        }
        String serialNumber = IdUtil.simpleUUID();

        return Thread.currentThread().getName()+"\t"+"调用成功,流水号:"+serialNumber;
    }
    //熔断也有fallback
    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
        return "id 不能负数,请稍候再试,(┬_┬)/~~     id: " +id;
    }

服务限流

留到alibaba的sentinel来学

监控面板DashBoard

需要自己新建一个工程,相对比较麻烦的

1pom

	<dependencies>
        <!--新增hystrix dashboard-->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2yml

server:
	port: 9001

3主启动类

@EnableHystrixDashboard

4被监控的服务主启动类

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class,args);
    }
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

5被监控的服务依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

6流程

登录http://localhost:9001/hystrix,然后填写监控地址

http://localhost:8001/hystrix.stream

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值