# 179. 商品详情页动态渲染系统:高可用架构优化之 hystrix 隔离与降级

前面深入讲解过 hystrix 了,这里就不多说,在服务相互调用时也可以使用 hystrix 来做降级,入门用法如下

添加依赖

compile 'org.springframework.cloud:spring-cloud-starter-hystrix'
1

添加配置

# feign 调用开启 hystrix 与一些配置
feign:
  #  httpclient:
  #    enabled: true
  client:
    config:
      default:           #服务名,填写default为所有服务
        connectTimeout: 10000
        readTimeout: 400000 # 3.3 * 2 分钟
  hystrix:
    enabled: true

# hystrix 的一些配置

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 400000
  threadpool:
    default:
      allowMaximumSizeToDivergeFromCoreSize: true
      coreSize: 20
      maximumSize: 1000
      maxQueueSize: -1
      queueSizeRejectionThreshold: -1
      keepAliveTimeMinutes: 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

增加降级类,这里在数据直连服务中做示例,实现 feign 的接口,这里没有具体的去实现业务逻辑,只讲整合 hystrix 的方法

@Component
public class EshopProductServiceFallback implements EshopProductService {
    @Override
    public String findBrandById(Long id) {
        return null;
    }

    @Override
    public String findCategoryById(Long id) {
        return null;
    }

    @Override
    public String findProductIntroById(Long id) {
        return null;
    }

    @Override
    public String findProductPropertyById(Long id) {
        return null;
    }

    @Override
    public String findProductById(Long id) {
        return null;
    }

    @Override
    public String findProductSpecificationById(Long id) {
        return null;
    }

    @Override
    public String findProductPropertyByProductId(Long productId) {
        return null;
    }

    @Override
    public String findProductSpecificationByProductId(Long productId) {
        return null;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

fegin 中指定这个降级接口

@FeignClient(value = "eshop-product-service",fallback = EshopProductServiceFallback.class)
public interface EshopProductService {
}
1
2
3