第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Sentinel(2)

標(biāo)簽:
Java

控制台相关配置:


在代码中配置Sentinel

定义一个资源名为test-sentinel-api的sentinel资源,当资源被降级或者被限流,会返回对应的字段

所定义的方法,返回值和入参需要一致

    @GetMapping("/test-sentinel-Resource")
    @SentinelResource(value = "test-sentinel-api"
            ,blockHandler = "block"
            ,fallback = "fallback"
            ,blockHandlerClass = TestControllerBlockHandlerClass.class)
    public String testSentinelResource(@RequestParam(required = false) String a){
        Entry entry = null;
        //被保护的逻辑
        if (StringUtils.isBlank(a)){
            throw new IllegalArgumentException("a cannot be null");
        }
            return a;
    }

    /**
     * 1.5:处理降级
     * sentinel1.6后,可以处理Throwable,任何异常都可以走到这里
     * @param a
     * @return
     */
    public String fallback( String a){
        log.warn("降级了fallback");
        return "降级了fallback";
    }
}

package com.itmuch.usercenter.sentineltest;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestControllerBlockHandlerClass {
    /**
     * 处理限流或者降级
     * @param a
     * @param e
     * @return
     */
    public static String block(String a , BlockException e) {
        log.warn("限流,或则被降级了block",e);
        return "限流,或则被降级了block";
    }


}

restTemplate整合sentinel

现在配置文件中开启整合sentinel

resttemplate:
  sentinel:
    enabled: true #开启/关闭@SentinelRestTemplate注解

在启动文件中的restTemplate的bean中添加注解@SentinelRestTemplate

public class ContentCenterApplication {

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

    //在spring容器中,创建一个对象,其类型为RestTemplate,名称&ID为restTemplate
    //<bean id="restTemplate" class="xxx.RestTemplate"/>
    @Bean
    @LoadBalanced//为restTemplate整合Ribbon
    @SentinelRestTemplate //为restTemplate整合sentinel
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

进入@SentinelRestTemplate注解中可以配置错误信息

https://img1.sycdn.imooc.com//60f178240001458c19201039.jpg

@SentinelResource

使用方式类似

https://img1.sycdn.imooc.com//60f178c70001a7d919201039.jpg

feign整合sentinel

先在配置文件中开启整合sentinel(没有提示)

feign:
  sentinel:
    enabled: true #feign整合sentinel


在feign调用的接口中,配置sentinel类

fallback:发生限流或降级会触发逻辑,但不会返回错误信息;

fallbackfactory:发生限流或降级会触发逻辑,并返回错误信息;

https://img1.sycdn.imooc.com//60f17623000169ee19201039.jpg

https://img1.sycdn.imooc.com//60f176730001761019201039.jpg

https://img1.sycdn.imooc.com//60f1767e0001b09b19201039.jpg

这里流控规则设置限流

https://img1.sycdn.imooc.com//60f176950001000e10600543.jpg

触发后

https://img1.sycdn.imooc.com//60f176b1000118bf07340580.jpg


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消