Hystrix实现服务熔断降级和服务熔断(Spring boot 2.1.4.RELEASE, Spring cloud Greenwich.SR1版本)

   hystrix是netflix开源的服务熔断组件,在Spring Cloud中整合进来,形成Spring Cloud的熔断降级体系。服务降级主要是为了服务雪崩,造成下游的服务不可用,可用性是分布式服务的必须要求。

1.pom文件修改:(增添依赖)

<dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>RELEASE</version>
 </dependency>

2.要开启服务,需要在main类上注上@EnableCircuitBreaker

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class OrderApplication {

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

}

3.同样,熔断后需要如何处理,需要调用方写处理逻辑。需要在服务方法上加上@HystrixCommand注解

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@RestController
public class HystrixController {
    @HystrixCommand(fallbackMethod = "fallback")
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(){
        RestTemplate restTemplate=new RestTemplate();
        //调用product服务的listForOrder接口,大家可以根据项目需求自己coding
        return restTemplate.postForObject("http://127.0.0.1:8083/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
    }
    private String fallback(){
        return "太拥挤了,请稍后重试";
    }
}

4.正常访问,效果如图
在这里插入图片描述
5.停掉product服务,就会走fallback函数,如图所示:
在这里插入图片描述
6.超时设置(可以避免被调用方访问时间较长而调用方访问不到)

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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(){
        RestTemplate restTemplate=new RestTemplate();
        return restTemplate.postForObject("http://127.0.0.1:8083/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
    }
    private String fallback(){
        return "太拥挤了,请稍后重试";
    }
    private String defaultFallback(){

        return "默认提示:太拥挤了,请稍后重试";
    }
}

  1. Hystrix实现服务熔断
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
    //超时配置
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//    })
    //服务熔断
    @HystrixCommand(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")   //错误率
    })
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(@RequestParam("number") Integer number){
        if(number%2==0){
            return "success";
        }
        RestTemplate restTemplate=new RestTemplate();
        return restTemplate.postForObject("http://127.0.0.1:8083/product/listForOrder", Arrays.asList("157875196366160022"),String.class);
    }
    private String fallback(){
        return "太拥挤了,请稍后重试";
    }
    private String defaultFallback(){

        return "默认提示:太拥挤了,请稍后重试";
    }
}

8.对熔断可视化的组件Hystrix-dashboard
8.1pom文件的修改:

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

8.2在启动类上新增@EnableHystrixDashboard注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@EnableFeignClients(basePackages = "com.imooc.product.client")
//@SpringBootApplication
//@EnableDiscoveryClient
//@EnableCircuitBreaker
@SpringCloudApplication
@ComponentScan(basePackages = "com.imooc")
@EnableHystrixDashboard //新增可视化组件
public class OrderApplication {

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

}

8.3在application.yml文件新增配置:

management:
  server:
    servlet:
      context-path: /
  endpoints:
    web:
      exposure:
        include: ["health","info","hystrix.stream"]

8.4启动界面
在这里插入图片描述
8.5不停访问后类似如下界面:
在这里插入图片描述
小伙伴们有木有学到呢?赶紧动手尝试一波把!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值