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

### 如何在Spring Cloud项目中集成并配置SentinelFeign 为了实现SentinelFeignSpring Cloud项目的集成,需遵循特定的步骤来确保两个组件能够协同工作。这不仅涉及到依赖项的引入,还包括必要的配置文件调整以及可能的应用程序代码修改。 #### 添加Maven依赖 首先,在`pom.xml`文件中加入所需的依赖库,以便支持Feign客户端和服务限流降级框架Sentinel的功能: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel</artifactId> </dependency> ``` 这些依赖允许应用程序利用Feign作为HTTP客户端发起远程调用的同时,通过Sentinel实施流量控制策略[^1]。 #### 配置application.yml 接着更新`src/main/resources/application.yml`以激活相应的特性,并指定一些基本参数用于初始化Sentinel环境: ```yaml spring: application: name: demo-service feign: sentinel: enabled: true sentinel: transport: dashboard: localhost:8080 # Sentinel 控制台地址 ``` 此部分设置启用了Feign对于Sentinel的支持,并指定了连接到本地运行的Sentinel仪表板的方式[^2]。 #### 创建FeignClient接口 定义一个或多个Feign Client接口,它们代表对外部微服务API的抽象访问层。例如: ```java @FeignClient(name = "exampleService", url = "${service.url}") public interface ExampleServiceApi { @GetMapping("/api/example") String getExample(); } ``` 这里假设存在名为`exampleService`的服务实例监听于由属性`${service.url}`所指向的位置上;而方法签名则描述了一个简单的GET请求映射关系[^3]。 #### 实现自定义逻辑处理器(可选) 如果希望进一步定制错误处理机制或者其他行为,则可以考虑编写自己的拦截器类继承自`AbstractFallbackFactory<T>`或者直接重写`BlockExceptionHandler`中的相应方法来捕获异常情况下的响应数据[^4]。 完成以上操作之后就可以尝试启动应用并通过发送实际网络请求测试整个链路是否正常运作了——此时应该能够在日志输出里看到来自Sentinel的日志条目说明当前资源已被纳入监控范围之内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值