# 8-5流控规则

# 导航

回到spring cloud alibaba导航页

# 流控规则

流控规则

# 针对来源

sentinel可以只对调用者进行限流,比如有两个微服务A,B 微服务 调用内容中心,我可以为A调用者设置200QPS,微服务B设置300QPS,默认default是不区分来源,要做区分来源是要做一些扩展的

# 流控模式

  • 直接
  • 关联
  • 链路

# 关联

  • 当关联的资源达到阈值,就限流自己

/actuator/sentinel 这个端点的QPS达到设置的阈值,那么就限流shares/1这个API

关联

关联在实际项目中经常使用,比如:有一个写的接口,写多的时候,读会影响写,就可以使用关联

# 链路

  • 只记录指定链路上的流量,指定资源 是指 入口进来的资源

# 8-12控制台相关配置项

应用端连接控制台配置项 控制台的配置项

# 8-13 Sentinel API

  • SphU
  • Tracer
  • ContextUtil

** SphU**

最核心的API,定义资源,让资源受到监控,并且可以保护资源

Tracer
可以对我们想要的异常,进行统计
ContextUtil
可以实现调用来源,还可以标记调用

# 8-14 SentinelResource注解详解

SentinelResource注解详解 (opens new window) 代码示例 sentinelResource源码地址

# 8-15 RestTemplate整合Sentinel

只需要加上 @SentinelRestTemplate 注解就可以了

package com.itmuch.contentcenter;
//MapperScan注解作用:扫描mybatis哪些包里面的接口
@MapperScan(basePackages = "com.itmuch")
@SpringBootApplication
@EnableFeignClients//(defaultConfiguration = GlobalFeignConfiguration.class)
public class ContentCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContentCenterApplication.class, args);
    }
    @Bean
    @LoadBalanced
    //SentinelRestTemplate 为RestTemplate整合SentineRestTemplate
    @SentinelRestTemplate
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
resttemplate:
  sentinel:
  # 关闭@SentinelRestTemplate注解
    enabled: false
1
2
3
4

相关源码
com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor

# Feign整合Sentinel

只需要添加如下配置:

  • feign.sentinel.enabled=true

# 限流降级发生时,如何定制自己的处理逻辑?

package com.itmuch.contentcenter.feignclient;

import com.itmuch.contentcenter.configuration.UserCenterFeignConfiguration;
import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.feignclient.fallback.UserCenterFeignClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName:UserCenterFeignClient
 * @Description:
 * @author: Torey
 */
@FeignClient(name = "user-center",fallback = UserCenterFeignClientFallback.class)
public interface UserCenterFeignClient {
    @GetMapping("/users/{id}")
    UserDTO findById(@RequestParam(value = "id",required = false) Integer id);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.itmuch.contentcenter.feignclient.fallback;

import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.feignclient.UserCenterFeignClient;
import org.springframework.stereotype.Component;

/**
 * @ClassName:UserCenterFeignClientFallback
 * @Description:
 * @author: Torey
 */
@Component
public class UserCenterFeignClientFallback implements UserCenterFeignClient {
    @Override
    public UserDTO findById(Integer id) {
        UserDTO userDTO = new UserDTO();
        userDTO.setWxNickname("一个默认用户");
        userDTO.setId(5);
        userDTO.setWxNickname("限流降级的默认用户");
        return userDTO;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 如何获取异常?

fallback 与 fallbackFactory 只能使用一个, fallbackFactory 功能要比 fallback 强大一些,fallbackFactory 可以拿到异常, fallback 拿不到异常

package com.itmuch.contentcenter.feignclient;

import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.feignclient.fallbackfactory.UserCenterFeignClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName:UserCenterFeignClient
 * @Description: fallback 与 fallbackFactory 只能使用一个,
 * fallbackFactory 功能要比 fallback 强大一些,fallbackFactory 可以拿到异常,
 * fallback 拿不到异常
 *
 * @author: Torey
 */
@FeignClient(name = "user-center",
        fallbackFactory =UserCenterFeignClientFallbackFactory.class )
public interface UserCenterFeignClient {
    @GetMapping("/users/{id}")
    UserDTO findById(@RequestParam(value = "id",required = false) Integer id);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.itmuch.contentcenter.feignclient.fallbackfactory;

import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.feignclient.UserCenterFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @ClassName:UserCenterFeignClientFallbackFactory
 * @Description:
 * @author: Torey
 */
@Slf4j
@Component
public class UserCenterFeignClientFallbackFactory
        implements FallbackFactory<UserCenterFeignClient> {
    @Override
    public UserCenterFeignClient create(Throwable throwable) {
        return new UserCenterFeignClient() {
            @Override
            public UserDTO findById(Integer id) {
                log.warn("远程调用被限流或者被降级了!!"+ throwable.getMessage());
                UserDTO userDTO = new UserDTO();
                userDTO.setWxNickname("一个默认用户");
                userDTO.setId(5);
                userDTO.setWxNickname("限流降级的默认用户-UserCenterFeignClientFallbackFactory");
                return userDTO;
            }
        };
    }
}

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

# 8-17 Sentinel使用总结

使用方式 使用方式 使用方法
编码方式 API try...catch...finally
注解方式 SentinelResource blockHandler/fallback
RestTemplate SentinelRestTemplate blockHandler/fallback
Feign feign.sentinel.enabled=true fallback/fallbackFactory

# 导航,上一页,下一页

上一页
下一页

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者