Sentinel的使用,sentinel结合feign的使用

第一步首先要下载sentinel在服务器部署

https://github.com/alibaba/Sentinel/releases/download/1.7.2/sentinel-dashboard-1.7.2.jar

然后上传到服务器上使用命令运行,这个jar包是一个可运行的spring boot程序
然后运行这个命令运行

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

在这里插入图片描述
来到sentinel的控制台

java那边
<!-- https://mvnrepository.com/artifact/com.alibaba.csp/sentinel-transport-simple-http -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>spring-boot-starter-ahas-sentinel-client</artifactId>
            <version>1.4.0</version>
        </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>
        </dependency>

在消费者那边可以加入这两个依赖

feign:
  # 使用okhttp的通信
  okhttp:
    enabled: true
    #开启Gzip压缩
  client:
    config:
      default:
        connect-timeout: 20000
        read-timeout: 20000
        execution:
          timeout:
            enable: false
      compression:
        request:
          enable: true
        response:
          enable: true
  sentinel:
    # 开启Sentinel对Feign的支持
    enabled: true

配置文件开启支持就行,这里是结合feign做服务降级,限流在后台设置规则
然后在定义一个服务提供者接口的实现类
实现他的方法

package com.smart.consumer.server.fallback;

import com.smart.consumer.server.common.RespCode;
import com.smart.consumer.server.common.RespEntity;
import com.smart.consumer.server.domian.vo.AddressVo;
import com.smart.consumer.server.service.ProviderData;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Administrator
 */
@Component
public class SentinelServiceFallBack implements ProviderData {

    @Override
    public RespEntity<List<AddressVo>> findAll(int mId, int size) {
        return RespEntity.error(RespCode.SYSTEM_ERROR);
    }

    @Override
    public String fileupload(MultipartFile file) {
        return "服务降级";
    }

    @Override
    public String test() {

        return "网络出现异常,请稍后再试";
    }

    @Override
    public String uploadFile(MultipartFile file, String fileType) {
        return null;
    }

}



然后在消费者那个定义服务提供者接口的接口上面使用

@FeignClient(value = "server-provider-nacos", path = "address", fallback = SentinelServiceFallBack.class)

就是fallback这个属性,参数是实现类.class
然后设定实现类的规则就行,服务挂了之后,发生降级就会走这个实现类里面指定的返回规则,列如网络不好,请稍后再试

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值