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

SpringCloud?hystrix斷路器與全局解耦全面介紹

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

第七章中在ProductController 和OrderController 中都使用了局部服務(wù)降級(jí),但同時(shí)也導(dǎo)致兩個(gè)問(wèn)題, 通過(guò)觀察兩個(gè)局部降級(jí)的案例,可以發(fā)現(xiàn):

每個(gè)業(yè)務(wù)方法都對(duì)應(yīng)一個(gè)降級(jí)方法,會(huì)導(dǎo)致代碼膨脹業(yè)務(wù)邏輯方法和處理服務(wù)異常降級(jí)方法混在一起。

業(yè)務(wù)邏輯方法和處理服務(wù)異常降級(jí)方法混在一起,不便于維護(hù),為解決此問(wèn)題,可以使用注解 @FeignClient(value = "PRODUCT-SERVICE",fallback = xxx.class)在調(diào)用遠(yuǎn)端服務(wù)的接口上進(jìn)行指定服務(wù)降級(jí)方法解耦,并實(shí)現(xiàn)調(diào)用遠(yuǎn)端服務(wù)的接口的實(shí)現(xiàn)類(lèi),在實(shí)現(xiàn)類(lèi)中統(tǒng)計(jì)管理服務(wù)降級(jí)解耦的方法。

進(jìn)行全局解耦,以 訂單服務(wù) OrderController 調(diào)用 商品服務(wù)ProductController 為案例,過(guò)程如下:

1、訂單服務(wù) 和 商品服務(wù)引入依賴(lài)

訂單服務(wù)引入依賴(lài) openfegin , 商品服務(wù)分別引入依賴(lài) Hystrix

2、清除OrderController 和 ProductController 所有降級(jí)方法

清除OrderController 所有降級(jí)方法

import com.hwadee.springcloud.entity.Product;
import com.hwadee.springcloud.service.IOrderFeignService;
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}")
    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}")
    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);
        return product;
    }
}

清除 ProductController 所有降級(jí)方法

注意,若有全局或者專(zhuān)屬降級(jí)可以自己增加。此處為方便演示,不使用全局或者專(zhuān)屬。

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)前訪(fǎng)問(wèn)服務(wù)地址:" + ip + ":" + port + "  " + "查詢(xún)商品訂單,訂單號(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)前訪(fǎng)問(wèn)服務(wù)地址:" + ip + ":" + port + "  " + "從購(gòu)物車(chē)刪除訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測(cè)試異常熔斷
        System.out.println(10 / 0);
        return product;
    }
}

2、定義Feign接口的實(shí)現(xiàn)類(lèi)

因?yàn)橛唵畏?wù) OrderController中通過(guò) 接口IOrderFeignService和 注解@FeignClient 遠(yuǎn)程調(diào)用商品服務(wù)ProductController,因此當(dāng)訂單服務(wù) OrderController出現(xiàn)超時(shí)或異常,可以通過(guò)訂單服務(wù) OrderController中通過(guò) 接口IOrderFeignService的 實(shí)現(xiàn)類(lèi)OrderFeignServiceFallBack 將務(wù)邏輯的處理方法 和 降級(jí)方法進(jìn)行解耦。

import com.hwadee.springcloud.entity.Product;
import com.hwadee.springcloud.service.IOrderFeignService;
import org.springframework.stereotype.Component;
@Component
public class OrderFeignServiceFallBack implements IOrderFeignService {
    @Override
    public Product findOrderById(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訂單服務(wù)訪(fǎng)問(wèn)/order/buy/1 超時(shí):"+id);
        return product;
    }
    @Override
    public Product deleteOrderById(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當(dāng)前訂單服務(wù)訪(fǎng)問(wèn)/order/delete/1 10/0異常:"+id);
        return product;
    }
}

3、修改IOrderFeignService代碼

在 接口IOrderFeignService的注解@FeignClient中指定服務(wù)解耦的類(lèi)OrderFeignServiceFallBack 。

@Component // 讓 spring 可以識(shí)別,不加也行,但是在注入的時(shí)候 IDEA 會(huì)報(bào)錯(cuò),不會(huì)影響運(yùn)行,但有條紅線(xiàn)讓自己不舒服
@FeignClient(value = "PRODUCT-SERVICE",fallback = OrderFeignServiceFallBack.class)
public interface IOrderFeignService {
    @RequestMapping(value = "/product/buy/{id}")
    Product findOrderById(@PathVariable Long id);
    @RequestMapping(value = "/product/delete/{id}")
    Product deleteOrderById(@PathVariable Long id);
}

4、定義 訂單服務(wù) 和 商品服務(wù)主啟動(dòng)類(lèi)

由于訂單服務(wù)中引入的spring-cloud-starter-openfeign依賴(lài)中已經(jīng)集成了feign-hystrix,有了對(duì) Hystrix 的支持,所以不需要額外引入依賴(lài)項(xiàng)。如果需要開(kāi)啟 訂單服務(wù)端的hystrix服務(wù),只需要在訂單服務(wù)的配置文件配置feign-hystrix 進(jìn)行激活Hystrix,無(wú)需在主啟動(dòng)類(lèi)中添加注解 @EnableHystrix 或 @EnableHystrix 來(lái)開(kāi)啟Hystrix服務(wù)。訂單服務(wù) 和 商品服務(wù)主啟動(dòng)類(lèi)如下:

訂單服務(wù)主啟動(dòng)類(lèi) OrderServerApplication

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

商品服務(wù)主啟動(dòng)類(lèi) ProductServerApplication

@SpringBootApplication
@EnableEurekaClient// 啟動(dòng) eureka 客戶(hù)端
public class ProductServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServerApplication.class, args);
    }
}

4、開(kāi)啟openfeign在調(diào)用服務(wù)過(guò)程中開(kāi)啟hystrix支持

由于前面引入的spring-cloud-starter-openfeign依賴(lài)中已經(jīng)集成了feign-hystrix,有了對(duì) Hystrix 的支持,所以不需要額外引入依賴(lài)項(xiàng)。只需要在訂單服務(wù)的 application.yml 配置文件中開(kāi)啟openfeign在調(diào)用服務(wù)過(guò)程中開(kāi)啟hystrix支持。

server:
  port: 9000
spring:
  application:
    name: order-service # 為當(dāng)前訂單服務(wù)命名為 order-service

# 配置eureka客戶(hù)端信息
eureka:
  client:
    service-url:
      # 配置eureka客戶(hù)端服務(wù)order-service的注冊(cè)地址,與 eureka-server 中暴露地址要保持一致
      defaultZone: http://localhost:8000/eureka/
  instance:
    prefer-ip-address: true # 是否使用 IP 地址注冊(cè),默認(rèn) false
    # instance-id: order-service  # 實(shí)例 id,服務(wù)的唯一標(biāo)識(shí),會(huì)自動(dòng)的找到order-service的ip和端口
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # 如果想在控制頁(yè)面看到服務(wù)地址與端口,可以將 instance-id 這樣配置

feign:
  hystrix:
    enabled: true

5、修改訂單服務(wù)配置文件

由于訂單服務(wù)中引入的spring-cloud-starter-openfeign依賴(lài)中已經(jīng)集成了feign-hystrix,有了對(duì) Hystrix 的支持,所以不需要額外引入依賴(lài)項(xiàng)。如果需要開(kāi)啟 訂單服務(wù)端的hystrix服務(wù),只需要在訂單服務(wù)的配置文件配置feign-hystrix 進(jìn)行激活Hystrix,修改application.yml如下:

server:
  port: 9000
spring:
  application:
    name: order-service # 為當(dāng)前訂單服務(wù)命名為 order-service

# 配置eureka客戶(hù)端信息
eureka:
  client:
    service-url:
      # 配置eureka客戶(hù)端服務(wù)order-service的注冊(cè)地址,與 eureka-server 中暴露地址要保持一致
      defaultZone: http://localhost:8000/eureka/
  instance:
    prefer-ip-address: true # 是否使用 IP 地址注冊(cè),默認(rèn) false
    # instance-id: order-service  # 實(shí)例 id,服務(wù)的唯一標(biāo)識(shí),會(huì)自動(dòng)的找到order-service的ip和端口
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # 如果想在控制頁(yè)面看到服務(wù)地址與端口,可以將 instance-id 這樣配置

feign:
  hystrix:
    enabled: true

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

分別訪(fǎng)問(wèn) http://localhost:9000/order/buy/1http://localhost:9000/order/delete/1 查看瀏覽器結(jié)果如下:

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

相關(guān)文章

  • JDK?version和class?file?version(Class編譯版本號(hào))對(duì)應(yīng)關(guān)系解讀

    JDK?version和class?file?version(Class編譯版本號(hào))對(duì)應(yīng)關(guān)系解讀

    這篇文章主要介紹了JDK?version和class?file?version(Class編譯版本號(hào))對(duì)應(yīng)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 如何在Spring?Boot框架中使用攔截器實(shí)現(xiàn)URL限制

    如何在Spring?Boot框架中使用攔截器實(shí)現(xiàn)URL限制

    在Spring?Boot框架中,您可以使用攔截器(Interceptor)來(lái)控制限制URL列表,本文通過(guò)一個(gè)簡(jiǎn)單的示例給大家介紹Spring?Boot?攔截器實(shí)現(xiàn)URL限制的操作方法,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • Java中資源加載的方法及Spring的ResourceLoader應(yīng)用小結(jié)

    Java中資源加載的方法及Spring的ResourceLoader應(yīng)用小結(jié)

    在Java開(kāi)發(fā)中,資源加載是一個(gè)基礎(chǔ)而重要的操作,這篇文章主要介紹了深入理解Java中資源加載的方法及Spring的ResourceLoader應(yīng)用,本文通過(guò)實(shí)例代碼演示了通過(guò)ClassLoader和Class獲取資源的內(nèi)容,以及使用Spring的ResourceLoader加載多個(gè)資源的過(guò)程,需要的朋友可以參考下
    2024-01-01
  • 探究Java常量本質(zhì)及三種常量池(小結(jié))

    探究Java常量本質(zhì)及三種常量池(小結(jié))

    這篇文章主要介紹了探究Java常量本質(zhì)及三種常量池(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例

    Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例

    這篇文章主要介紹了Spring?Boot?集成Redisson實(shí)現(xiàn)分布式鎖詳細(xì)案例,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • Mybatis order by 動(dòng)態(tài)傳參出現(xiàn)的問(wèn)題及解決方法

    Mybatis order by 動(dòng)態(tài)傳參出現(xiàn)的問(wèn)題及解決方法

    今天,我正在愉快地CRUD,突然發(fā)現(xiàn)出現(xiàn)一個(gè)Bug,我們來(lái)看看是怎么回事吧!接下來(lái)通過(guò)本文給大家介紹Mybatis order by 動(dòng)態(tài)傳參出現(xiàn)的一個(gè)小bug,需要的朋友可以參考下
    2021-07-07
  • 使用idea的database模塊繪制數(shù)據(jù)庫(kù)er圖的方法

    使用idea的database模塊繪制數(shù)據(jù)庫(kù)er圖的方法

    這篇文章主要介紹了使用idea的database模塊繪制數(shù)據(jù)庫(kù)er圖,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • Java實(shí)現(xiàn)IP地址到二進(jìn)制的轉(zhuǎn)換

    Java實(shí)現(xiàn)IP地址到二進(jìn)制的轉(zhuǎn)換

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)IP地址到二進(jìn)制的轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java程序測(cè)試上傳Maven工程代碼示例解析

    Java程序測(cè)試上傳Maven工程代碼示例解析

    這篇文章主要介紹了Java程序測(cè)試上傳Maven工程代碼示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Spring Security獲取用戶(hù)認(rèn)證信息的實(shí)現(xiàn)流程

    Spring Security獲取用戶(hù)認(rèn)證信息的實(shí)現(xiàn)流程

    Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪(fǎng)問(wèn)控制解決方案的安全框架。它提供了一組可以在Spring應(yīng)用上下文中配置的Bean,充分利用了Spring IoC,DI和AOP功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪(fǎng)問(wèn)控制功能
    2022-12-12

最新評(píng)論

沂水县| 叙永县| 莱阳市| 正蓝旗| 海原县| 林口县| 南和县| 宣恩县| 巴林左旗| 德庆县| 潍坊市| 忻城县| 明光市| 榆中县| 天台县| 渝中区| 达拉特旗| 大化| 金门县| 昌宁县| 额济纳旗| 清远市| 图木舒克市| 重庆市| 内江市| 股票| 嘉义市| 亳州市| 永丰县| 交城县| 绍兴市| 饶河县| 土默特右旗| 吉木萨尔县| 策勒县| 弋阳县| 库尔勒市| 黔南| 山东省| 永昌县| 武乡县|