欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

sentinel整合ribbon與fallback流程分步講解

 更新時(shí)間:2022年08月31日 09:55:16   作者:一個(gè)風(fēng)輕云淡  
這篇文章主要介紹了sentinel整合ribbon與fallback分步流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前期準(zhǔn)備:

啟動nacos和sentinel

提供者9003/9004(以9003為樣本)

新建cloudalibaba-provider-payment9003/9004

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mscloud03</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloudalibaba-provider-payment9003</artifactId>
    <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency><!-- 引入自己定義的api通用包,可以使用Payment支付Entity -->
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- SpringBoot整合Web組件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

yml文件

server:
  port: 9003
spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址
management:
  endpoints:
    web:
      exposure:
        include: '*'

配置9004的時(shí)候記得修改端口

主啟動類

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9003
{
    public static void main(String[] args) {
            SpringApplication.run(PaymentMain9003.class, args);
    }
}

業(yè)務(wù)類

@RestController
public class PaymentController
{
    @Value("${server.port}")
    private String serverPort;
    public static HashMap<Long,Payment> hashMap = new HashMap<>();
    static
    {
        hashMap.put(1L,new Payment(1L,"28a8c1e3bc2742d8848569891fb42181"));
        hashMap.put(2L,new Payment(2L,"bba8c1e3bc2742d8848569891ac32182"));
        hashMap.put(3L,new Payment(3L,"6ua8c1e3bc2742d8848569891xt92183"));
    }
    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id)
    {
        Payment payment = hashMap.get(id);
        CommonResult<Payment> result = new CommonResult(200,"from mysql,serverPort:  "+serverPort,payment);
        return result;
    }
}

測試1

啟動9003訪問localhost:9003/paymentSQL/1

消費(fèi)者84

新建cloudalibaba-consumer-nacos-order84

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mscloud03</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>cloudalibaba-consumer-nacos-order84</artifactId>
    <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- 引入自己定義的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- SpringBoot整合Web組件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

yml文件

server:
  port: 84
spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentinel dashboard地址
        dashboard: localhost:8080
        #默認(rèn)8719端口,假如被占用會自動從8719開始依次+1掃描,直至找到未被占用的端口
        port: 8719
#消費(fèi)者將要去訪問的微服務(wù)名稱(注冊成功進(jìn)nacos的微服務(wù)提供者)
service-url:
  nacos-user-service: http://nacos-payment-provider

主啟動

@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain84
{
    public static void main(String[] args) {
            SpringApplication.run(OrderNacosMain84.class, args);
    }
}

配置類

@Configuration
public class ApplicationContextConfig
{
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}

業(yè)務(wù)類1

@RestController
@Slf4j
public class CircleBreakerController
{
    public static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback") 
     public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
        if (id == 4) {
            throw new IllegalArgumentException ("IllegalArgumentException,非法參數(shù)異常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,該ID沒有對應(yīng)記錄,空指針異常");
        }
        return result;
    }
}

測試2

啟動9003 9004 84

訪問http://localhost:84/consumer/fallback/1

再訪問http://localhost:84/consumer/fallback/1

可以發(fā)現(xiàn)第一次是9004端口,第二次是9003端口,這里面的話,就可以就知道意見實(shí)現(xiàn)了ribbon中的輪訓(xùn)

測試3

訪問http://localhost:84/consumer/fallback/4

http://localhost:84/consumer/fallback/4

給客戶error頁面,不友好

修改業(yè)務(wù)類(只配置fallback)

@RestController
@Slf4j
public class CircleBreakerController
{
    public static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback負(fù)責(zé)業(yè)務(wù)異常
    public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
 
        if (id == 4) {
            throw new IllegalArgumentException ("IllegalArgumentException,非法參數(shù)異常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,該ID沒有對應(yīng)記錄,空指針異常");
        }
        return result;
    }
    public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(444,"兜底異常handlerFallback,exception內(nèi)容  "+e.getMessage(),payment);
    }
}

本例sentinel無配置

測試4

訪問http://localhost:84/consumer/fallback/4

這里可以發(fā)現(xiàn)沒有出現(xiàn)error界面,而是我們自己配置的兜底的處理方案

修改業(yè)務(wù)類(只配置blockHandler )

@RestController
@Slf4j
public class CircleBreakerController
{
    public static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping("/consumer/fallback/{id}")
     @SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler負(fù)責(zé)在sentinel里面配置的降級限流
    public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
        if (id == 4) {
            throw new IllegalArgumentException ("非法參數(shù)異常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,該ID沒有對應(yīng)記錄");
        }
        return result;
    }
    public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(444,"fallback,無此流水,exception  "+e.getMessage(),payment);
    }
    public CommonResult blockHandler(@PathVariable  Long id,BlockException blockException) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(445,"blockHandler-sentinel限流,無此流水: blockException  "+blockException.getMessage(),payment);
    }
}

配置sentinel

測試5

訪問2次以后http://localhost:84/consumer/fallback/4

修改業(yè)務(wù)類(fallback和blockHandler都配置)

@RestController
@Slf4j
public class CircleBreakerController
{
    public static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping("/consumer/fallback/{id}")
     @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler")
    public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
        if (id == 4) {
            throw new IllegalArgumentException ("非法參數(shù)異常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,該ID沒有對應(yīng)記錄");
        }
        return result;
    }
    public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(444,"fallback,無此流水,exception  "+e.getMessage(),payment);
    }
    public CommonResult blockHandler(@PathVariable  Long id,BlockException blockException) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(445,"blockHandler-sentinel限流,無此流水: blockException  "+blockException.getMessage(),payment);
    }
}

測試 6

訪問http://localhost:84/consumer/fallback/4

再訪問的時(shí)候,那就意味著這個(gè)時(shí)候會被限流處理

若 blockHandler 和 fallback 都進(jìn)行了配置,則被限流降級而拋出 BlockException 時(shí)只會進(jìn)入 blockHandler 處理邏輯。

修改業(yè)務(wù)類(忽略屬性.......)

@RestController
@Slf4j
public class CircleBreakerController
{
    public static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;
    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler",
            exceptionsToIgnore = {IllegalArgumentException.class})
    public CommonResult<Payment> fallback(@PathVariable Long id)
    {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
        if (id == 4) {
            throw new IllegalArgumentException ("非法參數(shù)異常....");
        }else if (result.getData() == null) {
            throw new NullPointerException ("NullPointerException,該ID沒有對應(yīng)記錄");
        }
        return result;
    }
    public CommonResult handlerFallback(@PathVariable  Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(444,"fallback,無此流水,exception  "+e.getMessage(),payment);
    }
    public CommonResult blockHandler(@PathVariable  Long id,BlockException blockException) {
        Payment payment = new Payment(id,"null");
        return new CommonResult<>(445,"blockHandler-sentinel限流,無此流水: blockException  "+blockException.getMessage(),payment);
    }
}

測試7

訪問http://localhost:84/consumer/fallback/4

到此這篇關(guān)于sentinel整合ribbon與fallback流程分步講解的文章就介紹到這了,更多相關(guān)sentinel整合ribbon與fallback內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Java中Properties類的詳細(xì)使用

    淺談Java中Properties類的詳細(xì)使用

    properties類繼承自hashtable,通常和io流結(jié)合使用。它最突出的特點(diǎn)是將key/value作為配置屬性寫入到配置文件中以實(shí)現(xiàn)配置持久化,或從配置文件中讀取這些屬性。它的這些配置文件的規(guī)范后綴名為".properties"。表示了一個(gè)持久的屬性集
    2021-06-06
  • 一篇看懂Java中的Unsafe類

    一篇看懂Java中的Unsafe類

    在閱讀AtomicInteger的源碼時(shí),看到了這個(gè)類:sum.msic.Unsafe,之前從沒見過。所以花了點(diǎn)時(shí)間研究了下,下面這篇文章主要給大家介紹了關(guān)于Java中Unsafe類的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • IDEA啟動Tomcat時(shí)控制臺出現(xiàn)亂碼問題及解決

    IDEA啟動Tomcat時(shí)控制臺出現(xiàn)亂碼問題及解決

    這篇文章主要介紹了IDEA啟動Tomcat時(shí)控制臺出現(xiàn)亂碼問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Java動態(tài)代理模式的深入揭秘

    Java動態(tài)代理模式的深入揭秘

    這篇文章主要給大家介紹了關(guān)于Java動態(tài)代理模式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離

    Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離

    這篇文章主要介紹了Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離,涉及了部分編程坐標(biāo)的問題,具有一定參考價(jià)值,需要的朋友可以了解下
    2017-09-09
  • java?Date和SimpleDateFormat時(shí)間類詳解

    java?Date和SimpleDateFormat時(shí)間類詳解

    這篇文章主要介紹了java?Date和SimpleDateFormat時(shí)間類詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Netty學(xué)習(xí)教程之基礎(chǔ)使用篇

    Netty學(xué)習(xí)教程之基礎(chǔ)使用篇

    Netty是由JBOSS提供的一個(gè)Java開源框架。Netty提供異步的、事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用程序框架和工具,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡(luò)服務(wù)器和客戶端程序。下面這篇文章主要給大家介紹了關(guān)于Netty基礎(chǔ)使用的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05
  • 詳解java中的6種單例寫法及優(yōu)缺點(diǎn)

    詳解java中的6種單例寫法及優(yōu)缺點(diǎn)

    在java中,單例有很多種寫法,面試時(shí),手寫代碼環(huán)節(jié),除了寫算法題,有時(shí)候也會讓手寫單例模式,這里記錄一下單例的幾種寫法和優(yōu)缺點(diǎn)。需要的朋友可以參考下
    2018-11-11
  • IDEA中HTML通過servlet3.0注解名提交表單到servlet類找不到頁面的問題

    IDEA中HTML通過servlet3.0注解名提交表單到servlet類找不到頁面的問題

    這篇文章主要介紹了IDEA中HTML通過servlet3.0注解名提交表單到servlet類找不到頁面的問題,本文通過場景描述及問題解析,給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java實(shí)現(xiàn)的決策樹算法完整實(shí)例

    Java實(shí)現(xiàn)的決策樹算法完整實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)的決策樹算法,簡單描述了決策樹的概念、原理,并結(jié)合完整實(shí)例形式分析了java實(shí)現(xiàn)決策樹算法的相關(guān)操作技巧,代碼中備有較為詳盡的注釋便于理解,需要的朋友可以參考下
    2017-11-11

最新評論