微服务集成spring cloud sentinel

这篇具有很好参考价值的文章主要介绍了微服务集成spring cloud sentinel。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

1. sentinel使用场景

2.  sentinel组成

3. sentinel dashboard搭建

 4. sentinel客户端详细使用

4.1 引入依赖

4.2 application.properties增加dashboard注册地址

4.3 手动增加限流配置类

4.4 rest接口及service类

4.5 通过dashboard动态配置限流规则


1. sentinel使用场景

限流、熔断、监控、动态规则配置

2.  sentinel组成

由两部分组成,

第一个是dashboard监控仪表盘,单独的jar,官网下载后启动,可监控所有服务、动态发现服务、配置限流策略、熔断等;

第二个是sentinel的客户端核心包,供微服务引用,注册到dashboard仪表盘,引入相关pom及设置相关配置即可;

3. sentinel dashboard搭建

启动命令

java -Dserver.port=8400 -Dcsp.sentinel.dashboard.server=localhost:8400 -Dproject.name=hj-sentinel -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=sentinel -jar sentinel-dashboard-1.8.6.jar

启动成功,地址栏输入localhost:8400, 如图:

 微服务集成spring cloud sentinel,spring cloud,微服务,spring cloud,sentinel

 4. sentinel客户端详细使用

4.1 引入依赖

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
   <version>2.2.5.RELEASE</version>
</dependency>

4.2 application.properties增加dashboard注册地址

spring.cloud.sentinel.transport.dashboard=localhost:8400

4.3 手动增加限流配置类

package hj.example.sampleprovider.sample.config;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: sentinel 限流规则配置类
 **/
@Configuration
public class SentinelRulesConfig {
    @Bean
    public void initFlowQpsRules() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("sentinelTest");
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setCount(1);
        flowRule.setLimitApp("default");
        flowRule.setClusterMode(false);
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        rules.add(flowRule);

        FlowRule flowRule1 = new FlowRule();
        flowRule1.setResource("sayHello");
        flowRule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule1.setCount(1);
        flowRule1.setLimitApp("default");
        flowRule1.setClusterMode(false);
        flowRule1.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        rules.add(flowRule1);
        FlowRuleManager.loadRules(rules);
    }
}

4.4 rest接口及service类

其中sentinelTest为rest接口限流,sayHello为方法限流

package hj.example.sampleprovider.sample.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
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 com.alibaba.fastjson.JSONObject;
import hj.example.sampleprovider.sample.HelloServiceImpl;
import hj.example.sampleprovider.sample.config.SentinelRulesConfig;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 **/
@RestController
public class SentinelTestController {

    @Autowired
    private HelloServiceImpl helloService;

    @RequestMapping("/testClean/{id}")
    public ResponseEntity<Object> testClean(@PathVariable("id") String id) {
        String resultStr = String.format("test clean id: %s [%s]", id, DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss ssss"));
        return new ResponseEntity<>(resultStr , HttpStatus.OK);
    }

    @RequestMapping("/testSentinelDynamicDashboard")
    public ResponseEntity<Object> testSentinelDynamicDashboard() {
        String resultStr = String.format("testSentinelDynamicDashboard [%s]", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss sss"));
        return new ResponseEntity<>(resultStr, HttpStatus.OK);
    }

    @RequestMapping("/sayHello")
    public ResponseEntity<Object> sayHelloTest() {
        String helloStr = helloService.sayHello("sayHelloTest");
        return new ResponseEntity<>(helloStr, HttpStatus.OK);
    }

    @SentinelResource(value = "sentinelTest", blockHandler = "sentinelTestHandler")
    @RequestMapping("/sentinelTest")
    public ResponseEntity<Object> sentinelTest() {
        System.out.println(" sentinelTest :" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss sss"));
        return new ResponseEntity<>("sentinelTest ", HttpStatus.OK);
    }
    public ResponseEntity<Object>  sentinelTestHandler(BlockException e) {
        System.out.println("被限流了");
        return new ResponseEntity<>("========sentinelTestHandler 被限流了:" + JSONObject.toJSONString(e), HttpStatus.OK);
    }
}
package hj.example.sampleprovider.sample;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import hj.example.sample.IHelloService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import test.SentinelTest;

import javax.xml.bind.ValidationException;
import java.util.Date;

@DubboService
public class HelloServiceImpl implements IHelloService {

    @Value("${dubbo.application.name}")
    private String serviceName;

    @SentinelResource(value = "sayHello", blockHandler = "sayHelloBlockHandler")
    public String sayHello(String name) {
        System.out.printf("[%s]: Hello, %s%n", serviceName, name);
        return String.format("[%s]: Hello, %s", serviceName, name);
    }

    public String sayHelloBlockHandler(String name, BlockException e) throws ValidationException {
        System.out.println("sayHello 被限流了。name:" + name + ",被限流了:" + JSON.toJSONString(e));
        return "sayHello 被限流了。name:" + name + ",被限流了:" + JSON.toJSONString(e);
    }

    public void sentinelTestMethod() {
        try(Entry entry = SphU.entry("sentinelTestMethod")) {
            System.out.println("hello sentinel : " + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss sss"));
        }catch (BlockException e) {
            e.printStackTrace();
        }
    }

    public void sentinelTestHandler(BlockException e) throws ValidationException {
        e.printStackTrace();
        throw new ValidationException(e.getMessage());
    }
}

4.5 通过dashboard动态配置限流规则

微服务集成spring cloud sentinel,spring cloud,微服务,spring cloud,sentinel文章来源地址https://www.toymoban.com/news/detail-665542.html

到了这里,关于微服务集成spring cloud sentinel的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring Cloud Alibaba】Sentinel 服务熔断与流量控制

    目录 前言 一、Sentinel 入门 1.1 什么是 Sentinel ? 1.2 微服务集成 Sentinel  1.3 安装Sentinel控制台 二、Jmeter 压力测试工具 2.1 Jmeter 介绍  2.2 Jmeter 安装 2.3 接口测试 三、Sentinel 使用 3.1 限流规则 3.1.1 warm up(预热模式) 3.1.2 排队等待 3.1.3 关联 3.1.4 链路 3.2 熔断规则 3.3 服务降级     

    2024年02月01日
    浏览(54)
  • 【Spring Cloud】服务容错中间件Sentinel进阶——五大规则

    我们在上一篇文章中对 Sentinel 已经有了基本的了解,接下来,我们一起对它的进阶进行学习吧! 资源 所谓资源就是 Sentinel 要保护的东西,资源是 Sentinel 的关键概念。它可以是Java应用程序中的任何内容,可以是一个服务,也可以是一个方法,甚至可以是一段代码。 我们入门

    2024年04月26日
    浏览(27)
  • 【springcloud 微服务】Spring Cloud Alibaba Sentinel使用详解

    目录 一、前言 二、分布式系统遇到的问题 2.1 服务可用性问题 2.1.1  单点故障

    2024年01月16日
    浏览(41)
  • 【springcloud 微服务】Spring Cloud Alibaba整合Sentinel详解

    目录 一、前言 二、环境准备 2.1 部署sentinel管控台 2.1.1 官网下载sentinel的jar包 2.1.2 启动控制台

    2023年04月09日
    浏览(39)
  • 微服务之Spring Cloud Alibaba Sentinel介绍与下载(详细方法)

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、流量路由、熔断降级、系统自适应过载保护、热点流量防护等多个维度保护服务的稳定性。 2012 年,Sentinel 诞生,主要功能为入口流量控制。 2013-2017 年,Sentinel 在阿里巴巴

    2024年02月11日
    浏览(37)
  • 【SpringCloud】11、Spring Cloud Gateway使用Sentinel实现服务限流

    1、关于 Sentinel Sentinel 是阿里巴巴开源的一个流量防卫防护组件,可以为微服务架构提供强大的流量防卫能力,包括流量控制、熔断降级等功能。Spring Cloud Gateway 与 Sentinel 结合,可以实现强大的限流功能。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近

    2024年02月01日
    浏览(43)
  • Spring Cloud Alibaba全家桶(六)——微服务组件Sentinel介绍与使用

    本文小新为大家带来 微服务组件Sentinel介绍与使用 相关知识,具体内容包括 分布式系统存在的问题 , 分布式系统问题的解决方案 , Sentinel介绍 , Sentinel快速开始 (包括: API实现Sentinel资源保护 , @SentinelResource注解实现资源保护 ), Sentinel控制台 , Spring Cloud Alibaba整合

    2024年01月17日
    浏览(35)
  • Sentinel nacos spring cloud 持久化配置---分布式/微服务流量控制

    下载地址:https://github.com/alibaba/Sentinel/releases 本次版本:1.8.6 上一篇文章已介绍 我们先说目标,为各位看官节省不匹配的时间 0、使用sentinel流控中心 1、使用nacos做配置中心 5、使用spring-cloud-starter-alibaba-sentinel做持久化配置 https://github.com/OrderDong/microservice-boot 分支:microserv

    2024年02月16日
    浏览(38)
  • Spring Cloud集成Nacos实现服务配置中心 | Spring Cloud 7

    先我们来看一下,微服务架构下关于配置文件的一些问题: 配置文件相对分散。在一个微服务架构下,配置文件会随着微服务的增多变的越来越多,而且分散在各个微服务中,不好统一配置和管理。 配置文件无法区分环境,开发环境、测试环境、线上环境。微服务项目可能会

    2024年02月14日
    浏览(33)
  • Spring Cloud Gateway集成SpringDoc,集中管理微服务API

    Spring Cloud微服务集成SpringDoc,在Spring Cloud Gateway中统一管理微服务的API,微服务上下线时自动刷新SwaggerUi中的group组。 框架 版本 Spring Boot 3.1.5 Spring Cloud 2022.0.4 Spring Cloud Alibaba 2022.0.0.0 Spring Doc 2.2.0 Nacos Server 2.2.3 公共模块里的配置是之前文章中提到的内容,加了一个webmvc和we

    2024年04月28日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包