最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud?hystrix斷路器與局部降級(jí)全面介紹

 更新時(shí)間:2022年10月24日 16:46:23   作者:愛吃面的貓  
什么是服務(wù)降級(jí)?當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實(shí)際業(yè)務(wù)情況及流量,對(duì)一些服務(wù)和頁(yè)面有策略的不處理或換種簡(jiǎn)單的方式處理,從而釋放服務(wù)器資源以保證核心交易正常運(yùn)作或高效運(yùn)作

服務(wù)降級(jí)

服務(wù)壓力劇增的時(shí)候,根據(jù)當(dāng)前的業(yè)務(wù)情況及流量對(duì)一些服務(wù)和頁(yè)面有策略的降級(jí),以此緩解服務(wù)器的壓力,以保證核心任務(wù)的進(jìn)行。同時(shí)保證部分甚至大分客戶能得到正確的響應(yīng)。也就是當(dāng)前的請(qǐng)求處理不了或者出錯(cuò)了,給一個(gè)默認(rèn)的返回。例如:雙11降級(jí)產(chǎn)品評(píng)價(jià)等非核心功能,保證支持和訂單的核心任務(wù)進(jìn)行。

服務(wù)熔斷

就是防止服務(wù)雪崩現(xiàn)象出現(xiàn),是服務(wù)降級(jí)的一種特殊情況。

一、Hystrix的服務(wù)使用前的問題

1、ProductController 中方法異常和超時(shí)

在商品服務(wù) ProductController 中的方法中增加異常和超時(shí),ProductController 中方法修改如下:

package com.hwadee.springcloud.controller;
import com.hwadee.springcloud.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
@RestController
@RequestMapping("/product")
public class ProductController {
    //方便后面講負(fù)載均衡,查看ip,此處獲取配置中的端口號(hào)和ip
    @Value("${server.port}")
    private String port;
    @Value("${spring.cloud.client.ip-address}")
    private String ip;
    @RequestMapping("/buy/{id}")
    public Product findById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要測(cè)試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port+"  "+"查詢商品訂單,訂單號(hào):"+id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測(cè)試超時(shí)熔斷
        try {
            Thread.sleep(5000);
            //測(cè)試并發(fā)熔斷
            //Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return product;
    }
    @RequestMapping(value = "/delete/{id}")
    public Product deleteOrderById(@PathVariable Long id){
        Product product = new Product();
        product.setId(id);
        // 后面需要測(cè)試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port+"  "+"從購(gòu)物車刪除訂單,訂單號(hào):"+id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測(cè)試異常熔斷
        System.out.println(10/0);
        return product;
    }
}

2、訪問查看效果

訪問http://localhost:9000/order/buy/1

訪問http://localhost:9000/order/delete/1

訂單服務(wù)中OrderController 中 調(diào)用商品服務(wù) ProductController 中方法時(shí),ProductController 中方法出現(xiàn)異常和超時(shí),訪問瀏覽器的異常結(jié)果和訂單服務(wù)中的內(nèi)部異常如下:

?

?

?

?

3、問題分析

在微服務(wù)架構(gòu)中,我們將系統(tǒng)拆分成了一個(gè)個(gè)的服務(wù)單元,各單元應(yīng)用間通過服務(wù)注冊(cè)與訂閱的方式互相依賴。由于每個(gè)單元都在不同的進(jìn)程中運(yùn)行,依賴通過遠(yuǎn)程調(diào)用的方式執(zhí)行,這樣就有可能因?yàn)榫W(wǎng)絡(luò)原因或是依賴服務(wù)自身問題出現(xiàn)調(diào)用故障或延遲,而這些問題會(huì)直接導(dǎo)致調(diào)用方的對(duì)外服務(wù)也出現(xiàn)延遲,若此時(shí)調(diào)用方的請(qǐng)求不斷增加,最后就會(huì)出現(xiàn)因等待出現(xiàn)故障的依賴方響應(yīng)而形成任務(wù)積壓,線程資源無(wú)法釋放,最終導(dǎo)致自身服務(wù)的癱瘓,進(jìn)一步甚至出現(xiàn)故障的蔓延最終導(dǎo)致整個(gè)系統(tǒng)的癱瘓。如果這樣的架構(gòu)存在如此嚴(yán)重的隱患,那么相較傳統(tǒng)架構(gòu)就更加的不穩(wěn)定,如下圖。

?

二、 商品服務(wù) Hystrix的 局部降級(jí)

1、降級(jí)配置

注解 @HistrixCommand

在商品服務(wù) ProductController 中的 findById( )和deleteOrderById( ) 方法上增加降級(jí)配置的注解 @HistrixCommand

—旦調(diào)用服務(wù)方法失敗并拋出了超時(shí)或錯(cuò)誤信息后,會(huì)自動(dòng)調(diào)用@HystrixCommand標(biāo)注好的fallbackMethod調(diào)用類中的指定方法。

在@HistrixCommand 指定超時(shí)或異常要回調(diào)的方法,同時(shí)可以指定配置參數(shù),例如使用@HistrixCommand 中的屬性commandProperties 指定默認(rèn)超時(shí)時(shí)間,如:

fallbackMethod:指定回調(diào)方法是findByIdTimeout
commandProperties: 指定@HystrixProperty??? ?的默認(rèn)值是默認(rèn)超時(shí)時(shí)間是3s

@HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})

2、回調(diào)(兜底降級(jí))方法

在商品服務(wù)ProductController 中增加回調(diào)方法findByIdTimeout( ) 和deleteOrderByIdException( ),當(dāng)訪問ProductController 中的 findById( )和deleteOrderById( ) 發(fā)生超時(shí) 或 異常時(shí),會(huì)調(diào)用回調(diào)方法。

3、具體代碼

降級(jí)配置 和回調(diào)(兜底降級(jí))方法的具體代碼如下:

import com.hwadee.springcloud.entity.Product;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
@RestController
@RequestMapping("/product")
public class ProductController {
    //方便后面講負(fù)載均衡,查看ip,此處獲取配置中的端口號(hào)和ip
    @Value("${server.port}")
    private String port;
    @Value("${spring.cloud.client.ip-address}")
    private String ip;
    @RequestMapping("/buy/{id}")
    @HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public Product findById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要測(cè)試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "查詢商品訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(50000.0));
        System.out.println(product);
        //測(cè)試超時(shí)熔斷
        try {
            Thread.sleep(5000);
            //測(cè)試并發(fā)熔斷
            //Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return product;
    }
    @RequestMapping(value = "/delete/{id}")
    @HystrixCommand(fallbackMethod = "deleteOrderByIdException")
    public Product deleteOrderById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要測(cè)試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "從購(gòu)物車刪除訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測(cè)試異常熔斷
        System.out.println(10 / 0);
        return product;
    }
    public Product findByIdTimeout(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "訪問 超時(shí) 進(jìn)行降級(jí)服務(wù)");
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        return product;
    }
    public Product deleteOrderByIdException(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "訪問 異常 進(jìn)行降級(jí)服務(wù)");
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        return product;
    }
}

4、主啟動(dòng)類激活Hstrix

在商品服務(wù)的主啟動(dòng)類 ProductServerApplication 中使用注解@EnableCircuitBreaker 進(jìn)行激活Hystrix,代碼如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient// 啟動(dòng) eureka 客戶端
@EnableCircuitBreaker // 主啟動(dòng)類激活 Hystrix
public class ProductServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServerApplication.class, args);
    }
}

5、進(jìn)行測(cè)試

將商品服務(wù)中ProductController 和 ProductServerApplication復(fù)制到 其他所有 商品服務(wù)中,進(jìn)行更新代碼。

分別訪問:http://localhost:9001/product/buy/1 http://localhost:9001/product/delete/1 查看超時(shí)和異常服務(wù)降級(jí)??礊g覽器和內(nèi)部結(jié)果。

?

?

三、 訂單服務(wù) Hystrix的 局部降級(jí)

1、降級(jí)配置

注解 @HistrixCommand

在訂單服務(wù) OrderController 中的方法上增加降級(jí)配置的注解 @HistrixCommand

2、回調(diào)(兜底降級(jí))方法

在訂單服務(wù) OrderController 定義 回調(diào)方法 buyTimeout( Long id) 、deleteOrderException( Long id),并設(shè)置超時(shí)服務(wù),超時(shí)服務(wù)要比將商品服務(wù)ProductController 中的 findById( ) 超時(shí)時(shí)間短。例如:ProductController 中的 findById( ) 超時(shí)時(shí)間是 3s ,則訂單服務(wù) OrderController 定義 回調(diào)方法 buyTimeout( ) 設(shè)定的超時(shí)時(shí)間應(yīng)是 <3s 。

3、具體代碼

降級(jí)配置 和回調(diào)(兜底降級(jí))方法的具體代碼如下:

import com.hwadee.springcloud.entity.Product;
import com.hwadee.springcloud.service.IOrderFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    IOrderFeignService orderFeignService;
    @RequestMapping("/buy/{id}")
    @HystrixCommand(fallbackMethod = "buyTimeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public Product buy(@PathVariable Long id) {
        System.out.println("進(jìn)入OrderController的buy方法, orderFeignService 準(zhǔn)備調(diào)用遠(yuǎn)端接口 findById");
        Product product = orderFeignService.findOrderById(id);
        return product;
    }
    @RequestMapping(value = "/delete/{id}")
    @HystrixCommand(fallbackMethod = "deleteOrderException")
    public Product deleteOrderById(@PathVariable Long id) {
        System.out.println("進(jìn)入OrderController的deleteOrderById方法, orderFeignService 準(zhǔn)備調(diào)用遠(yuǎn)端接口deleteOrderById");
        Product product = orderFeignService.deleteOrderById(id);
        int i =10/0;
        return product;
    }
    public Product buyTimeout( Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訂單服務(wù)訪問/order/buy/1 超時(shí):"+id);
        return product;
    }
    public Product deleteOrderException( Long id) {
        Product product = orderFeignService.deleteOrderById(id);
        product.setName("當(dāng)前訂單服務(wù)訪問/order/delete/1 10/0異常:"+id);
        return product;
    }
}

4、將商品服務(wù)中的超時(shí)時(shí)間為正常

將商品服務(wù)ProductController 中的 findById( ) 超時(shí)時(shí)間為正常,即程序超時(shí)時(shí)間是2s,熔斷等待時(shí)間是3s,并且要比 訂單服務(wù) OrderController 定義 回調(diào)方法 buyTimeout( ) 中的熔斷時(shí)間大,代碼如下:

@HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {

// 正常超時(shí)時(shí)間是 3s 比OrderController 中定義的超時(shí)時(shí)間長(zhǎng)。

@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") }) public Product findById(@PathVariable Long id) { 。。。。。

// 超時(shí)時(shí)間 2s

Thread.sleep(2000); 。。。。。

return product;

}

完整代碼如下:

import com.hwadee.springcloud.entity.Product;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
@RestController
@RequestMapping("/product")
public class ProductController {
    //方便后面講負(fù)載均衡,查看ip,此處獲取配置中的端口號(hào)和ip
    @Value("${server.port}")
    private String port;
    @Value("${spring.cloud.client.ip-address}")
    private String ip;
    @RequestMapping("/buy/{id}")
    @HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public Product findById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要測(cè)試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "查詢商品訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測(cè)試超時(shí)熔斷
        try {
            Thread.sleep(5000);
            //測(cè)試并發(fā)熔斷
            //Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return product;
    }
    @RequestMapping(value = "/delete/{id}")
    @HystrixCommand(fallbackMethod = "deleteOrderByIdException")
    public Product deleteOrderById(@PathVariable Long id) {
        Product product = new Product();
        product.setId(id);
        // 后面需要測(cè)試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "從購(gòu)物車刪除訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測(cè)試異常熔斷
        System.out.println(10 / 0);
        return product;
    }
    public Product findByIdTimeout(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "訪問 超時(shí) 進(jìn)行降級(jí)服務(wù)");
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        return product;
    }
    public Product deleteOrderByIdException(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訪問商品服務(wù)地址:" + ip + ":" + port + "  " + "訪問 異常 進(jìn)行降級(jí)服務(wù)");
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        return product;
    }
}

5、主啟動(dòng)類激活Hstrix

在訂單服務(wù)的主啟動(dòng)類 OrderServerApplication 中使用注解@EnableCircuitBreaker 或 注解@EnableHystrix進(jìn)行激活Hystrix,注意:@EnableHystrix包括了注解@EnableCircuitBreaker ,代碼如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient// 啟動(dòng) eureka 客戶端
@EnableFeignClients  // 啟動(dòng) feign
@EnableCircuitBreaker // 或 @EnableHystrix  啟動(dòng) Hystrix
public class OrderServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServerApplication.class, args);
    }
}

6、進(jìn)行測(cè)試

分別訪問:http://localhost:9000/order/buy/1 http://localhost:9000/order/delete/1 查看超時(shí)和異常服務(wù)降級(jí)。

?

?

到此這篇關(guān)于SpringCloud hystrix斷路器與服務(wù)降級(jí)全面介紹的文章就介紹到這了,更多相關(guān)SpringCloud hystrix斷路器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

嘉义县| 湘潭县| 聂荣县| 济宁市| 泸西县| 黄龙县| 陇西县| 白玉县| 九江县| 铁岭市| 福海县| 宁国市| 柏乡县| 霍林郭勒市| 古丈县| 当涂县| 沽源县| 渑池县| 亚东县| 顺昌县| 武安市| 周宁县| 淳化县| 舟山市| 沂水县| 保山市| 视频| 义乌市| 民县| 无棣县| 洛阳市| 漳平市| 新泰市| 汝州市| 宁海县| 襄汾县| 道真| 英吉沙县| 无锡市| 抚宁县| 城口县|