目录
- 一、简介
 - 二、作用
 - 三、核心概念
 - 3.1 熔断目的
 - 3.2 降级目的
 - 四、实例
 - 4.1 基于Hystrix
 - 4.1.1 熔断触发降级
 - 4.1.2 超时触发降级
 - 4.1.3 资源隔离触发降级
 - 4.2 基于OpenFeign pom.xml
 
一、简介
当微服务中的某个子服务,发生异常服务器宕机,其他服务在进行时不能正常访问而一直占用资源导致正常的服务也发生资源不能释放而崩溃,这时为了不造成整个微服务群瘫痪,进行的保护机制 就叫做熔断,是一种降级策略
熔断的目的:保护微服务集群
二、作用
- 对第三方访问的延迟和故障进行保护和控制
 - 防止复杂分布式系统中的雪崩效应(级联故障)
 - 快速失败,快速恢复
 - 回退,尽可能优雅的降级
 - 启用近实时监控、报警和操作控制
 
三、核心概念
3.1 熔断目的
应对雪崩效应,快速失败,快速恢复
3.2 降级目的
保证整体系统的高可用性
四、实例
4.1 基于Hystrix
pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
application
@EnableHystrix // 开启熔断
@SpringBootApplication
public class HystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication.class, args);
    }
}
4.1.1 熔断触发降级
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
    @Autowired
    private RestTemplate restTemplate;
    /**
     * @param num 参数
     * @return 字符串
     */
    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),  // 默认20, 最小产生5次请求
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), // 熔断时间, 该时间内快速失败
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), // 10s内失败率达到50%触发熔断
            }, // 10s 内最少 5 个请求, 如果失败率大于 50% 则触发熔断
            fallbackMethod = "fallback"
    )  // 服务调用失败时,触发熔断进行降级
    @GetMapping("/test")
    public String test(@RequestParam Integer num) {
        if (num % 2 == 0) {
            return "访问正常";
        }
        List<?> list = restTemplate.getForObject("http://localhost:7070/product/list", List.class);
        assert list != null;
        return list.toString();
    }
    /**
     * 熔断时触发降级
     *
     * @param num 参数
     * @return 字符串
     */
    private String fallback(Integer num) {
        // 降级操作
        return "系统繁忙";
    }
}
4.1.2 超时触发降级
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
    @Autowired
    private RestTemplate restTemplate;
    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "500")
            },
            fallbackMethod = "timeout"
    )
    @GetMapping("/timeout")
    public String timeoutTest(){
        return restTemplate.getForObject("http://localhost:7070/product/list", String.class);
    }
    /**
     * 超时时触发降级
     *
     * @return 字符串
     */
    private String timeout() {
        // 降级操作
        return "请求超时";
    }
}
4.1.3 资源隔离触发降级
平台隔离、业务隔离、部署隔离
线程池隔离、信号量隔离
4.2 基于OpenFeign pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application
@SpringBootApplication
@ComponentScan(basePackages = {  
        "com.study.provider.service",  // feign 包目录
        "com.study.hystrix"  // 当前模块启动目录
})
@EnableFeignClients(basePackages = "com.study.provider.service")  // feign 目录
public class HystrixApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication.class, args);
    }
}
HystrixFeignController
@RestController
@RequestMapping("/hystrixFeign")
public class HystrixFeignController {
    @Qualifier("com.study.provider.service.ProductionService")
    @Autowired
    ProductionService productService;  // feign Client 
    @GetMapping("test")
    public String test(){
        return productService.getProduction(1).toString();   // 远程调用
    }
}
feign Client
@FeignClient(value = "provider", fallback = ProductionFallback.class)   // fallback 用于熔断
public interface ProductionService {
    @RequestMapping("/product/getProduction")
    Object getProduction(@RequestParam Integer id);
}
ProductionFallback
@Component
public class ProductionFallback implements ProductionService {
    @Override
    public Object getProduction(Integer id) {
        return "请求失败";
    }
    @Override
    public Map createProduction(Object production) {
        return new HashMap<>();
    }
    @Override
    public Object selectByProduct(Object product) {
        return "请求失败";
    }
}
			
	声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
		
评论(0)