1、sentinel-dashbord修改
基于1.8.1版本
目前源码中只提供了流控规则的动态注册到zk中。其他的规则都不可以,得自己去实现
动态降级实现见:https://blog.csdn.net/xshsjl/article/details/114781202
首先将下载下源码,修改sentinel-dashbord模块
(1)将test包下rule中的zookeeper包复制到项目的rule包下
(2)DashboardConfig文件中加入zk的连接地址的参数启动配置项
/**
* Zookeeper address
*/
public static final String CONFIG_ZK_ADDR = "sentinel.dashboard.zk.addr";
public static String getZkAddr() {
return getConfigStr(CONFIG_ZK_ADDR);
}
启动jar包的时候 加上配置项
-Dsentinel.dashboard.zk.addr=127.0.0.1:2181
(3)修改ZookeeperConfig文件,动态获取zk地址
@Bean
public CuratorFramework zkClient() {
System.out.println(DashboardConfig.getZkAddr());
CuratorFramework zkClient =
CuratorFrameworkFactory.newClient(DashboardConfig.getZkAddr(),
new ExponentialBackoffRetry(ZookeeperConfigUtil.SLEEP_TIME, ZookeeperConfigUtil.RETRY_TIMES));
zkClient.start();
return zkClient;
}
(4)修改FlowControllerV2文件,使用zk进行保存和发送规则
(5)修改src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html文件,打开流控动态规则,关闭静态规则
(6)如果出现KeeperErrorCode = Unimplemented for /service错误,则要降低curator-recipes版本(源码中是4.0.1,对于我的zk版本过高)
(7)打包
(8)运行
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.zk.addr=127.0.0.1:2181 -jar sentinel-dashboard.jar
如果需要后台启动并且日志输出到指定文件
nohub java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.zk.addr=127.0.0.1:2181 -jar sentinel-dashboard.jar >log.out 2>&1 &
2、客户配置(自己的项目中)
如果是springboot项目下面仅供参考,有更简单配置方式,比较简单,去官网看看。
(1)引入包
<!-- 推模式:使用 ZooKeeper 配置规则 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-zookeeper</artifactId>
<version>${sentinel.version}</version>
</dependency>
<!--负责dashboard进通信如果你没有使用sentineldashboard它是可选的-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>${sentinel.version}</version>
</dependency>
(2)项目启动时初始化zk动态数据源,并且注册到规则中
注:@Lazy(false)可选。有的项目启动了懒加载,就得加@Lazy(false)将该组件特殊处理成不进行懒加载
sentinel.zookeeper.path如果需要sentinel-dashbord则需要和sentinel-dashbord中的ZookeeperConfigUtil.RULE_ROOT_PAT对应。默认是:/sentinel_rule_config
package com.hjsoft.sentinel;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import javax.annotation.PostConstruct;
import java.util.List;
/**
* @author xsh
*/
@Lazy(false)
@Configuration
@PropertySources({@PropertySource("classpath:sentinel.properties"),@PropertySource("classpath:conf.properties")})
public class SentinelRuleZkConfig {
@Value("${zookeeper.address}")
private String remoteAddress;
@Value("${sentinel.zookeeper.path}")
private String path;
@Value("${project.name}")
private String appName;
@PostConstruct
public void loadRules(){
//流控规则数据源
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path + "/" + appName,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
}
启动项目后,sentinel-dashbord、项目和zk就连通了(项目中修改规则dashbord上也会实时更新)