springCloud-Feign 整合 sentinel最基本入门Demo-2,接入到sintinel web控制台,并以控制台方式配置限流规则

前提:SpringCloud-Feign消费者调用生产者,工程环境可参考如下博文
https://blog.csdn.net/qq_41712271/article/details/104757725

假如需求:订单服务 调用 产品服务

1 在 订单工程 的项目中,添加依赖

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

2 修改 订单工程 的 application.yml文件,设置控制台地址和开启sentinel对feign的支持

spring:
  application:
    name: huawei-order-service
  cloud:
    sentinel:
      transport:
        # 配置Sentinel 控制台 地址
        dashboard: 127.0.0.1:8850
        # 应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer
        # 默认8719端口,假如端口被占用,依次+1,直到找到未被占用端口
        port: 8720

# 开启sentinel对feign的支持
feign:
  sentinel:
    enabled: true

3 在 订单工程 的项目中,添加流控降级的回调类,被降级就执行这里

package cn.huawei.service;
import cn.huawei.domain.Video;
import cn.huawei.interfaces.OpenFeignClientTest_order;
import org.springframework.stereotype.Component;

@Component
public class FallBackService  implements OpenFeignClientTest_order {
    @Override
    public Video findById_diao(int videoId) {
        return new Video("haha","haha","haha",0,null,0D,"haha");
    }

    @Override
    public int save_diao(Video video) {
        return -1;
    }
}

4 在 订单工程 的项目中,修改 Feign接口调用,在 @FeignClient(fallback=xxx),如下
注意:注释的位置,故意换种写法,否则报错 Ambiguous mapping. Cannot map,可能是sentinel的bug

package cn.huawei.interfaces;
import cn.huawei.domain.Video;
import cn.huawei.service.FallBackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "huawei-video-service",fallback = FallBackService.class)
//@RequestMapping("api/v1/video")
public interface OpenFeignClientTest_order {

    @RequestMapping("api/v1/video/find_by_id")
    //@RequestMapping("find_by_id")
     Video findById_diao(@RequestParam("videoId") int videoId);

    @PostMapping("api/v1/video/save")
    //@PostMapping("save")
     int save_diao(@RequestBody Video video);
}

5 打开 sentinel的控制台,添加 流控降级规则
注意:资源名书写的技巧,直接打开簇点链路,或是
打开 sentinel的日志,(项目启动后看idea的输出,这里为 C:\Users\need\logs\csp),copy过来

6 访问 订单工程 controller,开始测试 

另外需要注意1 订单工程 和 sentinel控制台 最好要在一个机器上,否则控制台添加规则会报错,解决方法,自行解决
                            2 就算在控制台不配置流控规则,如果 feign调用超时,也会走 被降级的方法

                               https://blog.csdn.net/qq_41712271/article/details/104759808104759808

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值