Directorio de artículos
-
- 1 Ejemplo de reglas de control de flujo centinela persistentes en nacos
-
- 1.1 Agregar dependencia al archivo pom del servicio de pedidos
- 1.2 Agregar configuración al archivo yml del servicio de pedidos
- 1.3 Organizar nacos
- 1.4 Transformar el tablero centinela
-
- 1.4.1 Modificar el archivo pom y eliminar el atributo de alcance
- 1.4.4 Modificar el archivo de propiedades, agregar la configuración de nacos, modificar el puerto de inicio predeterminado
- 1.4.5 Modificar el archivo `src \ main \ java \ com \ alibaba \ csp \ sentinel \ dashboard \ rule \ nacos \ NacosConfig.java`
- 1.4.6 Modificar el archivo `sentinel-dashboard / src / main / webapp / resources / app / scripts / directives / sidebar / sidebar.html`
- 1.4.7 Compilar y empaquetar `mvn clean package -Dmaven.test.skip = true`
- 2 Sentinel persiste en el local
1 Ejemplo de reglas de control de flujo centinela persistentes en nacos
URL de referencia 1: http://www.itmuch.com/spring-cloud-alibaba/sentinel-rules-persistence-push-mode-using-nacos/
URL de referencia 2:https://www.sonake.com/2019/12/16/Sentinel-Nacos%E5%AE%9E%E7%8E%B0%E8%A7%84%E5%88%99%E6%8C%81%E4%B9%85%E5%8C%96/
1.1 Agregar dependencia al archivo pom del servicio de pedidos
<!--添加sentinel持久化到nacos的依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
1.2 Agregar configuración al archivo yml del servicio de pedidos
spring:
cloud:
sentinel:
datasource:
# 名称随意
flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
# 规则类型,取值见:
# org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: flow
degrade:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
rule-type: system
authority:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow
1.3 Organizar nacos
1.3.1 Archivo de configuración de la base de datos mysql importar nacos
Busque el archivo nacos-mysql.sql en / nacos / conf, importe y ejecútelo en mysql, y establezca la estructura de la tabla de la base de datos correspondiente a nacos
1.3.2 Modificar archivosnacos-server-1.1.4\nacos\conf\application.properties
# db mysql
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
1.4 Transformar el tablero centinela
1.4.1 Modificar el archivo pom y eliminar el atributo de alcance
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>
#### 1.4.2 Copiar archivos
Copiar directorio sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
a directoriosentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
#### 1.4.3 Modificar el archivo Modificar com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
las siguientes 2 notas flowRuleDefault
paraflowRuleNacos
@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
1.4.4 Modificar el archivo de propiedades, agregar la configuración de nacos, modificar el puerto de inicio predeterminado
#配置nacos服务的地址
nacos.url=127.0.0.1:8848
#修改服务启动端口
server.port=8858
1.4.5 Modificar archivossrc\main\java\com\alibaba\csp\sentinel\dashboard\rule\nacos\NacosConfig.java
Actualizar la dirección de nacos, antes de la modificación
@Bean
public ConfigService nacosConfigService() throws Exception {
return ConfigFactory.createConfigService("localhost");
}
Después de la modificación
@Value("${nacos.url}")
private String serverAddr;
@Bean
public ConfigService nacosConfigService() throws Exception {
return ConfigFactory.createConfigService(serverAddr);
}
1.4.6 Modificar archivossentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
Deja ir el comentario a continuación
<!--<li ui-sref-active="active">-->
<!--<a ui-sref="dashboard.flow({app: entry.app})">-->
<!--<i class="glyphicon glyphicon-filter"></i> 流控规则 V1</a>-->
<!--</li>-->
1.4.7 Compilar y empaquetarmvn clean package -Dmaven.test.skip=true
Busque sentinel-dashboard.jar en el directorio de destino para iniciar la consola para la prueba
Nota: La prueba debe configurarse con "Reglas de control de flujo V1", hay un paquete jar compilado en la carpeta de herramientas para la prueba directa.
2 Sentinel persiste en el local
URL de referenciahttps://www.cnblogs.com/fx-blog/p/11726189.html
2.1 El archivo pom del servicio de pedidos introduce la dependencia persistente de centinela
<!--添加sentinel持久化到本地的依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
</dependency>
2.2 Agregar una clase que persiste en el local
package cn.hzp.config;
import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.*;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* PULL规则持久化
*/
public class SentinelPerFile implements InitFunc {
@Override
public void init() throws Exception {
String ruleDir = System.getProperty("user.dir").substring(0, 2) + "/sentinel/rules";
String flowRulePath = ruleDir + "/flow-rule.json";
String degradeRulePath = ruleDir + "/degrade-rule.json";
String systemRulePath = ruleDir + "/system-rule.json";
String authorityRulePath = ruleDir + "/authority-rule.json";
String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
this.mkdirIfNotExits(ruleDir);
this.createFileIfNotExits(flowRulePath);
this.createFileIfNotExits(degradeRulePath);
this.createFileIfNotExits(systemRulePath);
this.createFileIfNotExits(authorityRulePath);
this.createFileIfNotExits(paramFlowRulePath);
// 注册一个可读数据源,用来定时读取本地的json文件,更新到规则缓存中
// 流控规则
ReadableDataSource<String, List<FlowRule>> flowRuleRDS =
new FileRefreshableDataSource<>(flowRulePath, flowRuleListParser);
// 将可读数据源注册至FlowRuleManager
// 这样当规则文件发生变化时,就会更新规则到内存
FlowRuleManager.register2Property(flowRuleRDS.getProperty());
WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
flowRulePath,
this::encodeJson
);
// 将可写数据源注册至transport模块的WritableDataSourceRegistry中
// 这样收到控制台推送的规则时,Sentinel会先更新到内存,然后将规则写入到文件中
WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
// 降级规则
ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
degradeRulePath,
degradeRuleListParser
);
DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
degradeRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
// 系统规则
ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
systemRulePath,
systemRuleListParser
);
SystemRuleManager.register2Property(systemRuleRDS.getProperty());
WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
systemRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
// 授权规则
ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
authorityRulePath,
authorityRuleListParser
);
AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
authorityRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
// 热点参数规则
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
paramFlowRulePath,
paramFlowRuleListParser
);
ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
paramFlowRulePath,
this::encodeJson
);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
}
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<FlowRule>>() {
}
);
private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<DegradeRule>>() {
}
);
private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<SystemRule>>() {
}
);
private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<AuthorityRule>>() {
}
);
private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<ParamFlowRule>>() {
}
);
private void mkdirIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
}
private void createFileIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
}
private <T> String encodeJson(T t) {
return JSON.toJSONString(t);
}
}
2.3 Indique la fuente de la clase persistente
Cree un archivo en el directorio resources / META-INF / services del proyecto, llamado com.alibaba.csp.sentinel.init.InitFunc, el contenido es: la
ruta completa del nombre del paquete y el nombre de clase de FileDataSourceInit arriba, aquí estácn.hzp.config.SentinelPerFile
2.4 Verificación de prueba
- 1 Inicie el servicio de pedidos y el servidor centinela
- 2 Solicitud del navegador
http://localhost:8091/sentinel/highConcurrency?servName=app