说明:
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