亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

每個業(yè)務方法都對應一個降級方法,會導致代碼膨脹業(yè)務邏輯方法和處理服務異常降級方法混在一起。

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

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

1、訂單服務 和 商品服務引入依賴

訂單服務引入依賴 openfegin , 商品服務分別引入依賴 Hystrix

2、清除OrderController 和 ProductController 所有降級方法

清除OrderController 所有降級方法

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("進入OrderController的buy方法, orderFeignService 準備調(diào)用遠端接口 findById");
        Product product = orderFeignService.findOrderById(id);
        return product;
    }
    @RequestMapping(value = "/delete/{id}")
    public Product deleteOrderById(@PathVariable Long id) {
        System.out.println("進入OrderController的deleteOrderById方法, orderFeignService 準備調(diào)用遠端接口deleteOrderById");
        Product product = orderFeignService.deleteOrderById(id);
        return product;
    }
}

清除 ProductController 所有降級方法

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

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 {
    //方便后面講負載均衡,查看ip,此處獲取配置中的端口號和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);
        // 后面需要測試負載均衡,所以返回 ip 地址及端口號
        product.setName("當前訪問服務地址:" + ip + ":" + port + "  " + "查詢商品訂單,訂單號:" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測試超時熔斷
        try {
            Thread.sleep(5000);
            //測試并發(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);
        // 后面需要測試負載均衡,所以返回 ip 地址及端口號
        product.setName("當前訪問服務地址:" + ip + ":" + port + "  " + "從購物車刪除訂單,訂單號:" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測試異常熔斷
        System.out.println(10 / 0);
        return product;
    }
}

2、定義Feign接口的實現(xiàn)類

因為訂單服務 OrderController中通過 接口IOrderFeignService和 注解@FeignClient 遠程調(diào)用商品服務ProductController,因此當訂單服務 OrderController出現(xiàn)超時或異常,可以通過訂單服務 OrderController中通過 接口IOrderFeignService的 實現(xiàn)類OrderFeignServiceFallBack 將務邏輯的處理方法 和 降級方法進行解耦。

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("當前訂單服務訪問/order/buy/1 超時:"+id);
        return product;
    }
    @Override
    public Product deleteOrderById(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("當前訂單服務訪問/order/delete/1 10/0異常:"+id);
        return product;
    }
}

3、修改IOrderFeignService代碼

在 接口IOrderFeignService的注解@FeignClient中指定服務解耦的類OrderFeignServiceFallBack 。

@Component // 讓 spring 可以識別,不加也行,但是在注入的時候 IDEA 會報錯,不會影響運行,但有條紅線讓自己不舒服
@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、定義 訂單服務 和 商品服務主啟動類

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

訂單服務主啟動類 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// 啟動 eureka 客戶端
@EnableFeignClients  // 啟動 feign
public class OrderServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServerApplication.class, args);
    }
}

商品服務主啟動類 ProductServerApplication

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

4、開啟openfeign在調(diào)用服務過程中開啟hystrix支持

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

server:
  port: 9000
spring:
  application:
    name: order-service # 為當前訂單服務命名為 order-service

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

feign:
  hystrix:
    enabled: true

5、修改訂單服務配置文件

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

server:
  port: 9000
spring:
  application:
    name: order-service # 為當前訂單服務命名為 order-service

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

feign:
  hystrix:
    enabled: true

6、進行測試

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

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

相關文章

  • Java多線程基礎——Lock類

    Java多線程基礎——Lock類

    Lock類是Java類來提供的功能,豐富的api使得Lock類的同步功能比synchronized的同步更強大。本文對此進行詳細介紹,下面跟著小編一起來看下吧
    2017-02-02
  • 使用springboot單例模式與線程安全問題踩的坑

    使用springboot單例模式與線程安全問題踩的坑

    這篇文章主要介紹了使用springboot單例模式與線程安全問題踩的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • jar包沖突常用的解決方案

    jar包沖突常用的解決方案

    引言在使用java語言開發(fā),maven做項目管理時,我們經(jīng)常遇到一個頭疼的問題就是jar包沖突,這篇文章主要給大家介紹了關于jar包沖突常用的解決方案,需要的朋友可以參考下
    2023-12-12
  • SpringBoot如何訪問jsp頁面

    SpringBoot如何訪問jsp頁面

    本文介紹了如何在Spring Boot項目中進行Web開發(fā),包括創(chuàng)建項目、配置文件、添加依賴、控制層修改、測試效果以及在IDEA中進行配置的詳細步驟
    2025-01-01
  • Java設計模式之適配器模式簡介

    Java設計模式之適配器模式簡介

    這篇文章主要介紹了Java設計模式之適配器模式,需要的朋友可以參考下
    2014-07-07
  • springboot?使用clickhouse實時大數(shù)據(jù)分析引擎(使用方式)

    springboot?使用clickhouse實時大數(shù)據(jù)分析引擎(使用方式)

    這篇文章主要介紹了springboot?使用clickhouse實時大數(shù)據(jù)分析引擎的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2022-02-02
  • JDK線程池和Spring線程池的使用實例解析

    JDK線程池和Spring線程池的使用實例解析

    這篇文章主要介紹了JDK線程池和Spring線程池的使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • 對arraylist中元素進行排序?qū)嵗a

    對arraylist中元素進行排序?qū)嵗a

    這篇文章主要介紹了對arraylist中元素進行排序?qū)嵗a,還是比較不錯的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • 簡單了解spring cloud 網(wǎng)關服務

    簡單了解spring cloud 網(wǎng)關服務

    這篇文章主要介紹了簡單了解spring cloud 網(wǎng)關服務,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • 使用mockito編寫測試用例教程

    使用mockito編寫測試用例教程

    這篇文章主要為大家介紹了使用mockito編寫測試用例教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08

最新評論