springcloud之openfeign集成sentinel限流 -乐鱼入口

`

springcloud之openfeign集成sentinel限流


1.先调用服务接口,才会在sentinel显示应用名称

2.sentinel 限流默认1秒内多少访问量

3.sentinel规则持久化,服务重启以后不用在配置

4.使用nacos进行sentinel规则持久化


1.配置
spring:
  application:
    name: nacos-loadbalancer-service
  cloud:
    nacos:
      discovery:
        server-addr: 49.234.12.67:8848
    loadbalancer:
      cache: # 负载均衡缓存配置
        enabled: true # 开启缓存
        ttl: 5s # 设置缓存时间
        capacity: 256 # 设置缓存大小
      retry: # 重试配置
        enabled: true
        max-retries-on-same-service-instance: 1
        max-retries-on-next-service-instance: 1
      zone: test
    sentinel:
      transport:
        dashboard: 49.234.12.67:8180
service-url:
  nacos-user-service: http://nacos-user-service
feign:
  sentinel:
    enabled: true
  client:
    config:
      default: # feign调用超时配置
        connecttimeout: 5000
        readtimeout: 5000

2.jar

            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
       


3.其它限流知识
a.blocked by sentinel (flow limiting)
sentinel自定义返回值处理
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.blockexceptionhandler;
import com.alibaba.csp.sentinel.slots.block.blockexception;
import com.alibaba.csp.sentinel.slots.block.authority.authorityexception;
import com.alibaba.csp.sentinel.slots.block.degrade.degradeexception;
import com.alibaba.csp.sentinel.slots.block.flow.flowexception;
import com.alibaba.csp.sentinel.slots.block.flow.param.paramflowexception;
import com.alibaba.csp.sentinel.slots.system.systemblockexception;
import com.fasterxml.jackson.databind.objectmapper;
import org.springframework.stereotype.component;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

@component
public class sentinelexceptionhandler implements blockexceptionhandler {
    @override
    public void handle(httpservletrequest request, httpservletresponse response, blockexception ex) throws exception {
            string msg = null;
            if (ex instanceof flowexception) {
                msg = "限流了";
            } else if (ex instanceof degradeexception) {
                msg = "降级了";
            } else if (ex instanceof paramflowexception) {
                msg = "热点参数限流";
            } else if (ex instanceof systemblockexception) {
                msg = "系统规则限流或降级";
            } else if (ex instanceof authorityexception) {
                msg = "授权规则不通过";
            }
            // http状态码
            response.setstatus(500);
            response.setcharacterencoding("utf-8");
            response.setheader("content-type", "application/json;charset=utf-8");
            response.setcontenttype("application/json;charset=utf-8");
            // spring mvc自带的json操作工具,叫jackson
            string path = request.getservletpath();
            if (path != null) {
                msg = string.format("接口[%s]%s", path, msg);
            }
            new objectmapper().writevalue(response.getwriter(), msg);
        }
}


b.@feignclient(value = "nacos-user-service", fallbackfactory = userservicefallbackfactory.class)
@component
public class userservicefallbackfactory implements fallbackfactory {
0
0
分享到:
评论

相关推荐

    springcloud整合openfeign完整代码,可以直接运行

    配合博客https://blog.csdn.net/qq_42017523/article/details/121648876使用

    赠送jar包:spring-cloud-alibaba-sentinel-gateway-2021.1.jar; 赠送原api文档:spring-cloud-alibaba-sentinel-gateway-2021.1-javadoc.jar; 赠送源代码:spring-cloud-alibaba-sentinel-gateway-2021.1-sources...

    spring cloud alibaba限流sentinel开发工具

    配合博客https://blog.csdn.net/qq_42017523/article/details/121519054使用

    后端采用spring boot、spring cloud & alibaba。注册中心、配置中心选型nacos,权限认证使用redis。流量控制框架选型sentinel,分布式事务选型seata。提供了技术栈(vue3 element plus vite)版本ruoyi-cloud-vue3

    springcloud alibaba sentinel组件,用于限流降级,此demo为限流,并且配合了jmeter来进行并发测试,内容包含自己手写demo,开源源码,jemeter工具,readme文件包含实现的功能及怎么使用。

    主要介绍了spring cloud gateway整合sentinel实现网关限流,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    赠送jar包:spring-cloud-alibaba-sentinel-datasource-2021.1.jar; 赠送原api文档:spring-cloud-alibaba-sentinel-datasource-2021.1-javadoc.jar; 赠送源代码:spring-cloud-alibaba-sentinel-datasource-2021....

    赠送jar包:spring-cloud-alibaba-sentinel-datasource-2021.1.jar; 赠送原api文档:spring-cloud-alibaba-sentinel-datasource-2021.1-javadoc.jar; 赠送源代码:spring-cloud-alibaba-sentinel-datasource-2021....

    赠送jar包:spring-cloud-alibaba-sentinel-gateway-2021.1.jar; 赠送原api文档:spring-cloud-alibaba-sentinel-gateway-2021.1-javadoc.jar; 赠送源代码:spring-cloud-alibaba-sentinel-gateway-2021.1-sources...

    (代码)springcloud第08讲:使用sentinel实现微服务容错

    一篇很好的springcloud学习的思维导读,详细的介绍了,springcloud的搭建步骤以及各组件的说明讲解 涵盖 ...springcloud alibaba sentinel 实现熔断与限流 springcloud alibaba seata 处理分布式事务

    很多人可能会问,有了spring cloud这个微服务的框架,为什么又要使用spring cloud alibaba这个框架了?最重要的原因在于spring cloud中的几乎所有的组件都使用netflix公司的产品,然后在其基础上做了一层封装。然而...

    赠送jar包:spring-cloud-circuitbreaker-sentinel-2021.1.jar; 赠送原api文档:spring-cloud-circuitbreaker-sentinel-2021.1-javadoc.jar; 赠送源代码:spring-cloud-circuitbreaker-sentinel-2021.1-sources....

    17spring cloud alibaba:sentinel实现熔断与限流1

    springcloud入门 nacos 、sentinel、rocketmq、dubbo、

    基于spring cloud alibaba sentinel实现的服务限流详解

    赠送jar包:spring-cloud-starter-alibaba-sentinel-2021.1.jar; 赠送原api文档:spring-cloud-starter-alibaba-sentinel-2021.1-javadoc.jar; 赠送源代码:spring-cloud-starter-alibaba-sentinel-2021.1-sources...

    springcloud alibaba使用(二) - sentinel fallback整合ribbon openfeign 服务熔断功能 规则持久化.zip

global site tag (gtag.js) - google analytics
网站地图