在生产环境中使用 Sentinel
生活随笔
收集整理的這篇文章主要介紹了
在生产环境中使用 Sentinel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、安裝zookeeper
- 1. linux環境
- 2. windows環境
- 2. 安裝并啟動zkui
- 二、編譯打包
- 2.1. 拉取項目
- 2.2. 啟動
- 2.3. 登錄 sentinel
- 2.4. 登錄zkui
- 2.5. 重啟Sentinel
- 2.6. 刪除Sentinel的流控規則
- 三、將客戶端和zk打通
- 3.1. 引入依賴
- 3.2. 配置
- 3.3. 啟動springboot
- 3.4. sentinel控制臺+添加流控規則
- 3.5. 登錄zkui
- 四、測試
- 4.1. 請求
- 4.2. 重啟springboot項目
- 4.3. Sentinel控制臺
- 4.4. zkui
- 4.5. 分布式限流總結
分布式限流 Sentinel+Zookkeper
https://github.com/alibaba/Sentinel/wiki/在生產環境中使用-Sentinel
一、安裝zookeeper
1. linux環境
# 正確版本 docker run -p 2181:2181 --restart always -d zookeeper:3.5.8
注:這個zookeeper版本建議使用3.5.8,curator4.x對應zookeeper版本3.5.x
2. windows環境
將zoo_sample.cfg重命名zoo.cfg:
雙擊啟動zk
2. 安裝并啟動zkui
https://gblfy.blog.csdn.net/article/details/113803594
linux環境:
nohup java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar &windows環境
java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar >> msg.log
二、編譯打包
2.1. 拉取項目
git clone git@github.com:jiajiangnan/Sentinel.git cd Sentinel mvn clean install -DskipTests
2.2. 啟動
#進入編譯后的目錄 cd Sentinel-master\Sentinel-master\sentinel-dashboard\target# 第一種(推薦使用):sentinel啟動本地連接zk 沒任何問題 java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -Ddatasource.provider=zookeeper -Ddatasource.provider.zookeeper.server-addr=localhost:2181 -jar sentinel-dashboard-1.8.0-zookeeper.jar注:jar待補充
2.3. 登錄 sentinel
賬號/密碼:sentinel/sentinel
2.4. 登錄zkui
2.5. 重啟Sentinel
查看流控規則是否仍然存在
2.6. 刪除Sentinel的流控規則
因為流控規則是Sentinel控制臺同步zk的,預期效果,在Sentinel控制臺刪除流控規則后,zk的流控規則也會刪除
符合預期
三、將客戶端和zk打通
https://github.com/alibaba/Sentinel/wiki/動態規則擴展
3.1. 引入依賴
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-zookeeper</artifactId></dependency>3.2. 配置
package com.gblfy.distributedlimiter.config;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.context.annotation.Bean; import org.springframework.stereotype.Component;import java.util.List;@Component//添加到spring把容器讓spring管理 public class ZookeeperDataSourceConfig {@Bean//啟動加載此方法public void loadRules() {//zk服務端地址final String remoteAddress = "192.168.43.119:2181";//應用+keyNamefinal String path = "/limiter/sentinel-flow-rules";ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path,source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));FlowRuleManager.register2Property(flowRuleDataSource.getProperty());} }3.3. 啟動springboot
3.4. sentinel控制臺+添加流控規則
3.5. 登錄zkui
查看流控規則是否同步
四、測試
4.1. 請求
請求數量>1,預期會被限流
http://192.xxx.x.xxx:8082/sentinel4.2. 重啟springboot項目
lue
4.3. Sentinel控制臺
查看流控規則是否仍然存在
4.4. zkui
4.5. 分布式限流總結
對于是否是分布式限流,明確的就是限流規則是否儲存在外部的一個公用的存儲中心。之前講的Guava RateLimiter組件限流為什么不能做不到分布式呢?因為限流規則存在java應用內存里面的。
總結
以上是生活随笔為你收集整理的在生产环境中使用 Sentinel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Docker的Mysql主从复制搭建
- 下一篇: 发送http和https请求工具类 Js