SpringCloud中Sentinel的使用

SpringCloud中Sentinel的使用

父工程pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>MyParentCloud</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>MyService</module>
    <module>MyConsumer</module>
  </modules>
  <packaging>pom</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>0.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>


  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <configuration>
          <locales>en,fr</locales>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

bootstrap.yml的配置
server:
  port: 80

spring:
  application:
    name: my-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
  profiles:
    active: dev
application.properties的配置
#Sentinel 
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true

feign.sentinel.enabled: true

# nacos 
spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP

spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow

pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>MyParentCloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>MyService</artifactId>

    <dependencies>
<!--        Sentinel持久化-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>1.5.2</version>
        </dependency>

        <!--        系统自动添加的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

这些配置完成之后,下载sentinel-dashboard.jar,然后使用java -jar 运行起来,打开浏览器输入8080端口访问,便可以在里面配置相应的限流和降级配置了

Spring Cloud Alibaba Sentinel 是一个流量控制、熔断降级框架,主要用于保护微服务应用程序的稳定性和可靠性。下面是 Sentinel 使用的简单步骤: 1. 在项目的 pom.xml 文件添加 Sentinel 的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 2. 在启动类上添加 @EnableDiscoveryClient、@EnableFeignClients、@EnableCircuitBreaker、@EnableHystrixDashboard、@EnableSentinelDashboard 注解。 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker @EnableHystrixDashboard @EnableSentinelDashboard public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 在 application.yml 配置 Sentinel: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 ``` 其 `localhost:8080` 是 Sentinel Dashboard 的地址。 4. 在 Controller 或 Service 的方法上添加 Sentinel 注解,例如: ```java @GetMapping("/hello") @SentinelResource(value = "hello", blockHandler = "blockHandler") public String hello() { return "Hello World"; } public String blockHandler(BlockException ex) { return "Blocked by Sentinel"; } ``` 其 `@SentinelResource` 注解用于定义资源名称,并且可以指定 blockHandler 方法,当资源被限流或降级时,将会调用该方法。 5. 启动 Sentinel Dashboard,使用浏览器访问 `http://localhost:8080` 即可查看 Sentinel 监控面板。 以上是 Spring Cloud Alibaba Sentinel 的简单使用步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值