SpringCloud: 利用sentinel实现服务降级(不使用控制台的方式)

1.pom中增加spring cloud alibaba相关依赖。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.edu.tju</groupId>
    <artifactId>spring-boot-sentinel-degrade</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>



            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.0.0.RELEASE</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>
        


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2.配置application.properties:

server.port=8819
spring.application.name=myDegrade
spring.datasource.url=jdbc:mysql://139.198.xx.xx:3306/test
spring.datasource.username=root
spring.datasource.password=xx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
management.endpoints.web.exposure.include=*


3.创建降级规则配置类:

package cn.edu.tju.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class DegradeConfig {
    @PostConstruct
    public void initDegradeRule(){
        List<DegradeRule> degradeRuleList=new ArrayList<>();
        //定义降级规则
        DegradeRule degradeRule=new DegradeRule();
        //设置降级规则所对应的资源名称
        degradeRule.setResource("helloWorld");
        //根据发生异常的次数进行降级
        degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
        //至少要有两次请求才会降级,第一次请求时发生异常也不会降级
        degradeRule.setMinRequestAmount(2);
        //发生异常的次数达到1后开始降级
        degradeRule.setCount(1);
        //降级的时间窗口,在10秒内,所有的请求都直接降级
        degradeRule.setTimeWindow(10);

        //把降级规则添加到降级规则列表
        degradeRuleList.add(degradeRule);
        //把降级列表添加到降级规则管理器
        DegradeRuleManager.loadRules(degradeRuleList);
    }
}

4.编写controller类,并对相应的接口使用@SentinelResource注解进行相关配置

package cn.edu.tju.controller;

import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class Demo {
    @RequestMapping("/hi")
    //value制定资源名称,brockHandler属性指定降级之后调用的方法
    @SentinelResource(value="helloWorld",entryType = EntryType.IN,blockHandler = "myBlockHandler")
    public String hi(){
        int i=1/0;
        return "hi:"+new Date().toLocaleString();
    }

    //降级之后调用的方法,参数和返回类型与源方法一致,但参数要增加一个BlockException类型的参数
    public String myBlockHandler(BlockException ex){
        return "服务被降级了";
    }
}

5.创建启动类,并启动程序

package cn.edu.tju;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class,args);
    }
}

6.在浏览器中快速刷新访问:http://localhost:8819/hi,结果如下:
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值