This commit is contained in:
周伟
2022-05-11 19:04:14 +08:00
parent 9440ac7291
commit d9c5ffd627
826 changed files with 0 additions and 481675 deletions

View File

@@ -25,13 +25,7 @@
<meta name="generator" content="Hexo 4.2.0">
</head>
<body>
<div class="book-container">
<div class="book-sidebar">
@@ -55,289 +49,148 @@
<li><a href="/" class="current-tab">首页</a></li>
</ul>
<ul class="uncollapsible">
<li><a href="../">上一级</a></li>
</ul>
<ul class="uncollapsible">
<li>
<a href="/专栏/深入理解 Sentinel/01 开篇词:一次服务雪崩问题排查经历.md.html">01 开篇词:一次服务雪崩问题排查经历.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/02 为什么需要服务降级以及常见的几种降级方式.md.html">02 为什么需要服务降级以及常见的几种降级方式.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/03 为什么选择 SentinelSentinel 与 Hystrix 的对比.md.html">03 为什么选择 SentinelSentinel 与 Hystrix 的对比.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/04 Sentinel 基于滑动窗口的实时指标数据统计.md.html">04 Sentinel 基于滑动窗口的实时指标数据统计.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/05 Sentinel 的一些概念与核心类介绍.md.html">05 Sentinel 的一些概念与核心类介绍.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/06 Sentinel 中的责任链模式与 Sentinel 的整体工作流程.md.html">06 Sentinel 中的责任链模式与 Sentinel 的整体工作流程.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/07 Java SPI 及 SPI 在 Sentinel 中的应用.md.html">07 Java SPI 及 SPI 在 Sentinel 中的应用.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/08 资源指标数据统计的实现全解析(上).md.html">08 资源指标数据统计的实现全解析(上).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/09 资源指标数据统计的实现全解析(下).md.html">09 资源指标数据统计的实现全解析(下).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/10 限流降级与流量效果控制器(上).md.html">10 限流降级与流量效果控制器(上).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/11 限流降级与流量效果控制器(中).md.html">11 限流降级与流量效果控制器(中).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/12 限流降级与流量效果控制器(下).md.html">12 限流降级与流量效果控制器(下).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/13 熔断降级与系统自适应限流.md.html">13 熔断降级与系统自适应限流.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/14 黑白名单限流与热点参数限流.md.html">14 黑白名单限流与热点参数限流.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/15 自定义 ProcessorSlot 实现开关降级.md.html">15 自定义 ProcessorSlot 实现开关降级.md.html</a>
</li>
<li>
<a class="current-tab" href="/专栏/深入理解 Sentinel/16 Sentinel 动态数据源:规则动态配置.md.html">16 Sentinel 动态数据源:规则动态配置.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/17 Sentinel 主流框架适配.md.html">17 Sentinel 主流框架适配.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/18 Sentinel 集群限流的实现(上).md.html">18 Sentinel 集群限流的实现(上).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/19 Sentinel 集群限流的实现(下).md.html">19 Sentinel 集群限流的实现(下).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/20 结束语Sentinel 对应用的性能影响如何?.md.html">20 结束语Sentinel 对应用的性能影响如何?.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/21 番外篇Sentinel 1.8.0 熔断降级新特性解读.md.html">21 番外篇Sentinel 1.8.0 熔断降级新特性解读.md.html</a>
</li>
</ul>
</div>
</div>
<div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
<div class="sidebar-toggle-inner"></div>
</div>
<script>
function add_inner() {
@@ -347,9 +200,6 @@
inner.classList.add('show')
}
function remove_inner() {
let inner = document.querySelector('.sidebar-toggle-inner')
@@ -357,9 +207,6 @@
inner.classList.remove('show')
}
function sidebar_toggle() {
let sidebar_toggle = document.querySelector('.sidebar-toggle')
@@ -389,9 +236,6 @@
}
function open_sidebar() {
let sidebar = document.querySelector('.book-sidebar')
@@ -415,13 +259,7 @@ function hide_canvas() {
overlay.classList.remove('show')
}
</script>
<div class="off-canvas-content">
<div class="columns">
@@ -493,9 +331,6 @@ function hide_canvas() {
boolean updateValue(T newValue);
}
</code></pre>
<ul>
@@ -549,9 +384,6 @@ function hide_canvas() {
}
}
</code></pre>
<p>可见DynamicSentinelProperty 使用 Set 存储已注册的监听器updateValue 负责通知所有监听器,调用监听器的 configUpdate 方法。</p>
@@ -589,9 +421,6 @@ function hide_canvas() {
// 提供默认的 SentinelProperty
= new DynamicSentinelProperty&lt;List&lt;FlowRule&gt;&gt;();
static {
// 给默认的 SentinelProperty 注册监听器FlowPropertyListener
@@ -599,9 +428,6 @@ function hide_canvas() {
currentProperty.addListener(LISTENER);
}
// 注册 SentinelProperty
public static void register2Property(SentinelProperty&lt;List&lt;FlowRule&gt;&gt; property) {
@@ -621,9 +447,6 @@ function hide_canvas() {
}
}
</code></pre>
<p>实现更新限流规则缓存的 FlowPropertyListener 是 FlowRuleManager 的一个内部类,其源码如下:</p>
@@ -665,9 +488,6 @@ function hide_canvas() {
}
}
</code></pre>
<p>PropertyListener 接口定义的两个方法:</p>
@@ -705,9 +525,6 @@ function hide_canvas() {
void close() throws Exception;
}
</code></pre>
<p>ReadableDataSource 是一个泛型接口,参数化类型 S 代表用于装载从数据源读取的配置的类型,参数化类型 T 代表对应 Sentinel 中的规则类型。例如,我们可以定义一个 FlowRuleProps 类,用于装载从 yml 配置文件中读取的限流规则配置,然后再将 FlowRuleProps 转为 FlowRule所以 S 可以替换为 FlowRulePropsT 可以替换为 <code>List&lt;FlowRule&gt;</code></p>
@@ -735,9 +552,6 @@ function hide_canvas() {
protected final Converter&lt;S, T&gt; parser;
protected final SentinelProperty&lt;T&gt; property;
public AbstractDataSource(Converter&lt;S, T&gt; parser) {
if (parser == null) {
@@ -751,9 +565,6 @@ function hide_canvas() {
this.property = new DynamicSentinelProperty&lt;T&gt;();
}
@Override
public T loadConfig() throws Exception {
@@ -761,9 +572,6 @@ function hide_canvas() {
return loadConfig(readSource());
}
public T loadConfig(S conf) throws Exception {
T value = parser.convert(conf);
@@ -771,9 +579,6 @@ function hide_canvas() {
return value;
}
@Override
public SentinelProperty&lt;T&gt; getProperty() {
@@ -783,9 +588,6 @@ function hide_canvas() {
}
}
</code></pre>
<p>从源码可以看出:</p>
@@ -807,9 +609,6 @@ function hide_canvas() {
T convert(S source);
}
</code></pre>
<ul>
@@ -837,17 +636,11 @@ public class FlowRuleProps{
//....省略
}
</code></pre>
<p>第二步:创建数据转换器,实现将 FlowRuleProps 转为 <code>List&lt;FlowRule&gt;</code>,如 FlowRuleConverter 所示。</p>
<pre><code class="language-java">public class FlowRuleConverter implements Converter&lt;FlowRuleProps, List&lt;FlowRule&gt;&gt;{
@Override
public List&lt;FlowRule&gt; convert(FlowRuleProps source){
@@ -857,9 +650,6 @@ public class FlowRuleProps{
}
}
</code></pre>
<p>第三步:创建 FlowRuleDataSource继承 AbstractDataSource实现 readSource 方法。readSource 方法只需要获取 FlowRuleProps 返回即可,代码如下。</p>
@@ -871,9 +661,6 @@ public class FlowRuleDataSource extends AbstractDataSource&lt;FlowRuleProps, Lis
@Autowired
private FlowRuleProps flowRuleProps;
public FlowRuleDataSource() {
super(new FlowRuleConverter());
@@ -895,9 +682,6 @@ public class FlowRuleDataSource extends AbstractDataSource&lt;FlowRuleProps, Lis
}
}
</code></pre>
<p>第四步:增强 FlowRuleDataSource让 FlowRuleDataSource 能够监听到 FlowRuleProps 配置改变。</p>
@@ -919,9 +703,6 @@ public class FlowRuleDataSource extends AbstractDataSource&lt;FlowRuleProps, Lis
getProperty().updateValue(loadConfig());
}
@Override
public void afterPropertiesSet() throws Exception {
@@ -931,9 +712,6 @@ public class FlowRuleDataSource extends AbstractDataSource&lt;FlowRuleProps, Lis
}
}
</code></pre>
<ul>
@@ -953,9 +731,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
@Autowired
private FlowRuleDataSource flowRuleDataSource;
@Override
public void run(ApplicationArguments args) throws Exception {
@@ -967,9 +742,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
}
}
</code></pre>
<p>在调用 FlowRuleManager#register2Property 方法将 FlowRuleDataSource 动态数据源的 SentinelProperty 注册给 FlowRuleManager 时FlowRuleManager 会自动给该 SentinelProperty 注册一个监听器FlowPropertyListener</p>
@@ -1017,9 +789,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
</div>
</div>
</div>
</div>
@@ -1027,9 +796,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
</div>
</div>
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>
@@ -1045,17 +811,11 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-NPSEEVD756');
@@ -1081,9 +841,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
setCookie("lastPath", path)
}
function setCookie(cname, cvalue) {
var d = new Date();
@@ -1095,9 +852,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
}
function getCookie(cname) {
var name = cname + "=";
@@ -1115,12 +869,6 @@ public class FlowRuleDataSourceConfiguration implements ApplicationRunner{
return "";
}
</script>
</html>