JavaEE:Sentinel使用

本文详细介绍了如何在JavaEE环境中使用Sentinel作为控制组件,涵盖Sentinel控制台Dashboard的启动、Sentinel与Apollo的集成、流量控制、熔断降级、系统保护规则的设定,以及在Dubbo、RocketMQ中的应用,还包括集群流控和Web应用从Apollo拉取Sentinel规则的配置方法。
摘要由CSDN通过智能技术生成

说明:

Sentinel是控制组件,可实现流量控制、熔断降级、系统自适应保护等多个维度保护微服务的稳定性。

一、启动Sentinel控制台Dashboard(192.168.233.129):

1.使用官方默认sentinel-dashboard(退出后在管理页创建的规则会丢失):

(1)下载sentinel-dashboard-1.8.3.jar到D盘根目录:

https://github.com/alibaba/Sentinel/releases

(2)打开cmd启动控制台(设置用户和密码都为root):

D:\>java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=192.168.233.129:8080 -Dsentinel.dashboard.auth.username=root -Dsentinel.dashboard.auth.password=root -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.3.jar

2.搭建sentinel-dashboard(Push模式,能将管理页增加的规则持久化到Apollo):

Apollo安装(一、二章):

https://blog.csdn.net/a526001650a/article/details/123188040

(1)下载Sentinel-1.8.3.zip,解压后用IntelliJ IDEA打开:

https://github.com/alibaba/Sentinel/releases/tag/1.8.3

(2)删除apollo-openapi中<scope>test</scope>,在sentinel-dashboard模块pom.xml中(后面所有操作均在sentinel-dashboard模块中进行):

<!-- for Apollo rule publisher sample -->
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-openapi</artifactId>
    <version>1.2.0</version>
</dependency>

(3)配置加载类,修改ApolloConfig(从test目录移到main目录):

package com.alibaba.csp.sentinel.dashboard.rule.apollo;
...
@Configuration
@ComponentScan("com.alibaba.csp.sentinel.dashboard.rule.apollo.*")//扫描自定义包下所有规则类
public class ApolloConfig implements InitializingBean {
    public static String url = "http://192.168.233.128:8070"; //apollo地址
    public static String appId = "apollo";                    //应用id
    public static String token = "76e317c68505c32a4d4adab1e8abf84ff5bf1f1a"; //token
    public static String env = "DEV";     //环境
    public static String nameSpace = "application"; //名称空间
    public static String account = "apollo";         //管理员账号
    public static String clusterName = "default";
    @Override
    public void afterPropertiesSet() throws Exception {
    }
    @Bean
    public ApolloOpenApiClient apolloOpenApiClient() { //供外部使用
        return ApolloOpenApiClient.newBuilder()
                .withPortalUrl(url)   //apollo服务器地址
                .withToken(token)     //token
                .build();
    }
    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEncoder() { //限流规则列表转json
        return JSON::toJSONString;
    }
    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleDecoder() {//限流规则json转列表
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }
    @Bean
    public Converter<List<DegradeRuleEntity>, String> degradeRuleEncoder() { //熔断规则列表转json
        return JSON::toJSONString;
    }
    @Bean
    public Converter<String, List<DegradeRuleEntity>> degradeRuleDecoder() { //熔断规则json转列表
        return s -> JSON.parseArray(s, DegradeRuleEntity.class);
    }
    @Bean
    public Converter<List<SystemRuleEntity>, String> systemRuleEncoder() { //系统保护规则列表转json
        return JSON::toJSONString;
    }
    @Bean
    public Converter<String, List<SystemRuleEntity>> systemRuleDecoder() { //系统保护规则json转列表
        return s -> JSON.parseArray(s, SystemRuleEntity.class);
    }
    @Bean
    public Converter<List<AuthorityRuleEntity>, String> authorityRuleEncoder() { //访问控制规则列表转json
        return JSON::toJSONString;
    }
    @Bean
    public Converter<String, List<AuthorityRuleEntity>> authorityRuleDecoder() { //访问控制规则json转列表
        return s -> JSON.parseArray(s, AuthorityRuleEntity.class);
    }
    @Bean
    public Converter<List<ParamFlowRuleEntity>, String> paramFlowRuleEncoder() { //热点限流规则列表转json
        return JSON::toJSONString;
    }
    @Bean
    public Converter<String, List<ParamFlowRuleEntity>> paramFlowRuleDecoder() { //热点参数限流规则json转列表
        return s -> JSON.parseArray(s, ParamFlowRuleEntity.class);
    }
}

(4)flowDataId工具类,修改ApolloConfigUtil(从test目录移到main目录):

public final class ApolloConfigUtil { //flowDataId工具类
    public static final String POSTFIX = "rules";
    private ApolloConfigUtil() {
    }
    public static String getFlowDataId(String appName) {  //限流DataId
        return String.format("%s-%s-%s", appName, "flow", POSTFIX);
    }
    public static String getDegradeDataId(String appName) { //熔断DataId
        return String.format("%s-%s-%s", appName, "degrade", POSTFIX);
    }
    public static String getSystemDataId(String appName) {  //系统保护DataId
        return String.format("%s-%s-%s", appName, "system", POSTFIX);
    }
    public static String getAuthorityDataId(String appName) { //访问控制DataId
        return String.format("%s-%s-%s", appName, "authority", POSTFIX);
    }
    public static String getParamFlowDataId(String appName) { //热点参数限流DataId
        return String.format("%s-%s-%s", appName, "paramFlow", POSTFIX);
    }
}

(5)配置限流规则发布,修改FlowRuleApolloPublisher(从test目录移到main目录,熔断等规则需要创建新的DynamicRulePublisher实现类):

package com.alibaba.csp.sentinel.dashboard.rule.apollo;
...
@Component("flowRuleApolloPublisher")
public class FlowRuleApolloPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { //限流规则发布
    @Autowired
    private ApolloOpenApiClient apolloOpenApiClient;
    @Autowired
    private Converter<List<FlowRuleEntity>, String> converter;
    private FastDateFormat dateFormat = FastDateFormat.getInstance("yyyyMMdd HH:mm:ss");
    @Override
    public void publish(String appName, List<FlowRuleEntity> rules) {
        if (rules == null) {
            return;
        }
        String flowDataId = ApolloConfigUtil.getFlowDataId(appName);
        //(1)修改
        OpenItemDTO itemDTO = new OpenItemDTO();
        itemDTO.setKey(flowDataId);
        itemDTO.setValue(converter.convert(rules));
        itemDTO.setComment("修改时间: " + dateFormat.format(new Date()));  //描述
        itemDT
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值