当前位置: 首页 > news >正文 个人网站备案 照片高级网站开发工程师 news 2025/10/28 0:49:36 个人网站备案 照片,高级网站开发工程师,网站建设公司厦门有哪些,如何用html制作网站在 Spring Boot 中使用 Sentinel 非常方便#xff0c;Spring Cloud Alibaba 提供了 spring-cloud-starter-alibaba-sentinel 组件#xff0c;可以快速将 Sentinel 集成到你的 Spring Boot 应用中#xff0c;并利用其强大的流量控制和容错能力。 下面是一个详细的步骤指南 …在 Spring Boot 中使用 Sentinel 非常方便Spring Cloud Alibaba 提供了 spring-cloud-starter-alibaba-sentinel 组件可以快速将 Sentinel 集成到你的 Spring Boot 应用中并利用其强大的流量控制和容错能力。 下面是一个详细的步骤指南 步骤 1: 添加 Sentinel 依赖 首先需要在你的 pom.xml 文件中添加 Spring Cloud Alibaba Sentinel 的依赖。确保你已经配置了 Spring Cloud Alibaba 的依赖管理 (spring-cloud-alibaba-dependencies)。 dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId /dependency如果你需要使用 Sentinel 的持久化功能例如将规则持久化到 Nacos 配置中心还需要添加相应的依赖例如 dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId /dependency dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId /dependency步骤 2: 配置 Sentinel 在 application.properties 或 application.yml 文件中配置 Sentinel 的基本信息。 至少需要配置 Sentinel 控制台的地址以便你可以通过控制台查看监控数据和管理规则。 spring:application:name: your-spring-boot-app # 应用名称Sentinel 控制台中会显示cloud:sentinel:transport:dashboard: localhost:8080 # Sentinel 控制台的地址 (默认端口 8080)port: 8719 # Sentinel 客户端与控制台通信的端口默认 8719可以自定义避免冲突# 如果需要持久化规则到 Nacos还需要配置 Nacos 相关信息 # nacos: # config: # server-addr: your-nacos-server-address:8848注意: 你需要先启动 Sentinel 控制台才能在控制台中看到你的 Spring Boot 应用的监控数据和配置规则。 你可以从 Sentinel 的 GitHub 仓库下载 Sentinel 控制台的 JAR 包并启动。 步骤 3: 定义受保护的资源 在 Spring Boot 中你可以使用以下方式来定义需要 Sentinel 保护的资源 方式一使用 SentinelResource 注解 这是最常用的方式通过在方法上添加 SentinelResource 注解可以声明该方法为一个受保护的资源。 import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.springframework.stereotype.Service;Service public class MyService {SentinelResource(value myResource, blockHandler handleBlock)public String doSomething() {// 业务逻辑return Success;}// BlockHandler 方法用于处理 BlockException即限流、熔断等 block 情况public String handleBlock(BlockException e) {return Blocked by Sentinel: e.getClass().getSimpleName(); // 返回被 Sentinel 拦截的提示信息} }SentinelResource(value myResource): value 属性指定资源的名称在 Sentinel 控制台中会显示这个名称。blockHandler handleBlock: blockHandler 属性指定了当资源被限流、熔断等 block 时应该调用的 blockHandler 方法。 handleBlock 方法的签名需要和被保护的方法一致但最后一个参数必须是 BlockException 类型。 blockHandler 方法负责处理 block 逻辑例如返回友好的提示信息或执行降级操作。 方式二编程式 API 定义资源 除了注解你也可以使用 Sentinel 提供的编程式 API 来定义资源这种方式更加灵活可以更细粒度地控制资源的入口和出口。 import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.Tracer; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.springframework.stereotype.Service;Service public class MyService {public String doSomethingProgrammatically() {Entry entry null;try {entry SphU.entry(myProgrammaticResource); // 定义资源名// 被保护的业务逻辑return Success from Programmatic Resource;} catch (BlockException e) {// 处理 BlockException例如限流、熔断等return Blocked by Sentinel (Programmatic): e.getClass().getSimpleName();} catch (Exception ex) {// 业务逻辑异常需要记录到 Sentinel 的异常统计中Tracer.traceEntry(entry, ex);throw ex;} finally {if (entry ! null) {entry.exit(); // 保证 exit() 被调用否则会导致统计数据不准确}}} }SphU.entry(myProgrammaticResource): SphU.entry() 方法用于定义一个资源入口参数为资源名称。 它会返回一个 Entry 对象用于标记资源的入口。entry.exit(): 在 finally 块中调用 entry.exit()表示资源调用结束。 必须确保 entry.exit() 被调用否则会导致 Sentinel 的统计数据不准确。BlockException 捕获: 需要捕获 BlockException 异常并处理限流、熔断等 block 情况。Tracer.traceEntry(entry, ex): 如果业务逻辑发生异常需要使用 Tracer.traceEntry() 方法将异常信息记录到 Sentinel 的异常统计中。 步骤 4: 配置流控规则 定义了受保护的资源后你需要配置流控规则告诉 Sentinel 如何对这些资源进行保护。 你可以通过以下方式配置规则 方式一在 Sentinel 控制台配置 这是最推荐的方式通过 Sentinel 控制台你可以可视化地配置和管理规则实时生效无需重启应用。 访问 Sentinel 控制台: 访问你在 application.properties 中配置的 spring.cloud.sentinel.transport.dashboard 地址默认 http://localhost:8080。找到你的应用: 在控制台的 “簇点链路” 或 “机器列表” 中找到你的 Spring Boot 应用。配置规则: 在 “流控规则”、“降级规则”、“热点规则” 等菜单中为你的资源配置相应的规则。 例如为 “myResource” 配置一个 QPS 为 2 的流控规则。按如下操作 添加“流控”规则 添加“熔断”机制 方式二在代码中编程式配置规则 你也可以在代码中编程式地配置规则这种方式更适合自动化配置或单元测试。 import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component;import java.util.ArrayList; import java.util.List;Component public class FlowRuleConfig implements CommandLineRunner {Overridepublic void run(String... args) throws Exception {initFlowRules();}private void initFlowRules() {ListFlowRule rules new ArrayList();FlowRule rule new FlowRule();rule.setResource(myResource); // 资源名称与 SentinelResource 注解中的 value 对应rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 流控类型QPSrule.setCount(2); // QPS 阈值为 2rules.add(rule);FlowRuleManager.loadRules(rules); // 加载规则} }FlowRule: FlowRule 对象表示一个流控规则。rule.setResource(myResource): 指定规则应用于哪个资源需要与 SentinelResource 注解或编程式 API 中定义的资源名称一致。rule.setGrade(RuleConstant.FLOW_GRADE_QPS): 设置流控类型为 QPS (每秒请求数)。rule.setCount(2): 设置 QPS 阈值为 2即每秒钟只允许 2 个请求通过。FlowRuleManager.loadRules(rules): 加载配置好的规则列表。 步骤 5: 启动 Sentinel 控制台和 Spring Boot 应用 启动 Sentinel 控制台: 下载 Sentinel 控制台的 JAR 包并使用 java -jar sentinel-dashboard.jar 命令启动。 默认访问地址为 http://localhost:8080默认用户名密码都是 sentinel。启动 Spring Boot 应用: 启动你的 Spring Boot 应用。 步骤 6: 测试和监控 访问受保护的接口: 访问你应用中被 SentinelResource 或编程式 API 保护的接口。观察 Sentinel 控制台: 在 Sentinel 控制台中你可以看到你的应用的监控数据例如 “簇点链路” 中会显示资源的请求量、通过量、拒绝量、平均响应时间等指标。测试流控效果: 尝试以超过你配置的流控阈值的速率访问受保护的接口你会看到部分请求被 Sentinel 拦截并返回你在 blockHandler 方法中定义的提示信息。 高级特性 (可选) Sentinel 还提供了很多高级特性你可以根据实际需求进行探索和使用例如 降级规则 (Degrade Rule): 配置熔断降级规则当服务的错误率或响应时间超过阈值时自动熔断防止故障扩散。热点参数限流 (Hot-spot Param Flow Control): 根据请求的热点参数进行限流例如根据用户 ID、商品 ID 等。系统规则 (System Rule): 从系统全局负载角度进行保护例如根据 CPU 使用率、Load、内存使用率等进行自适应限流。授权规则 (Authority Rule): 进行黑白名单授权控制限制特定来源的请求访问。持久化规则 (Rule Persistence): 将规则持久化到 Nacos、ZooKeeper、Redis 等配置中心实现规则的动态更新和集群共享。自定义 BlockHandler 和 Fallback: 更灵活地处理 BlockException 和业务异常实现更精细的降级策略。集群流控 (Cluster Flow Control): 对集群中的多个实例进行统一的流量控制。 总结 使用 Spring Cloud Alibaba Sentinel 在 Spring Boot 中实现限流、熔断降级是非常简单的。 通过添加依赖、配置 Sentinel 控制台地址、定义受保护的资源、配置规则你就可以快速为你的 Spring Boot 应用增加一层强大的保护屏障提升系统的稳定性和容错能力。 Sentinel 提供的可视化控制台和丰富的特性也使得流量控制和容错管理更加便捷高效。 文章转载自: http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.flqkp.cn.gov.cn.flqkp.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.mpbgy.cn.gov.cn.mpbgy.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.hrzky.cn.gov.cn.hrzky.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.gchqy.cn.gov.cn.gchqy.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.rqwwm.cn.gov.cn.rqwwm.cn http://www.morning.frmmp.cn.gov.cn.frmmp.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.hsksm.cn.gov.cn.hsksm.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.tcxzn.cn.gov.cn.tcxzn.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.zsrdp.cn.gov.cn.zsrdp.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.ypwlb.cn.gov.cn.ypwlb.cn http://www.morning.npxcc.cn.gov.cn.npxcc.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn 查看全文 http://www.tj-hxxt.cn/news/255280.html 相关文章: 公司网站链接怎么弄安卓app制作开发 网站负责人核验现场拍摄照片电子件南京 高端网站建设 营销型网站建设的费用报价单免费域名空间网站 怎么修改网站图标做搜狗网站排名首页 广西建设网站培训做lol数据的网站有哪些 怎么建淘宝优惠券网站做推广wordpress 图片压缩插件 企业网站用什么cms比较好网站开发外包 价格 网站源码开发石家庄做网站 如何做购物网站的教程电影网站app怎么做 辽宁省住建厅建设网站wordpress设置超链接 彩票网站自己可以做吗公司 做网站 天津 网站优化巨野县建设局网站 做网站的外部链接关系分析的工具在猪八戒上做网站要注意什么 网站留言板模板百度云搭建wordpress 信用卡在哪些网站上做推广网站建设工单系统 成都房地产网站建设网站建设技术可行性 新品发布会策划昆明做整站优化 网站建设全网推广小程序装潢设计培训班学费多少钱 徐州seo建站网站引导页下载 浏览不良网站会被网警抓吗北京美的网站 诸暨广川建设公司网站app下载量排名 旅游网站建设方案两百字建筑用模板是什么板材 做公司网站的多少钱网站建设合作分成合同 网站分析报告怎么写友情链接的作用大不大 电子商务网站建设 期末考试试卷以及答案wordpress 删除 加载中 提交网站到谷歌嘉兴网站关键词优化 宫免费网站天津建设工程信息网评标专家 终审 网站建设要学会什么ppt下载免费完整版 新网站建设流程软文推广的作用 云主机网站配置洛阳市建设规划局网站