溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringCloud?hystrix斷路器與全局解耦怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2022-10-26 09:52:55 來源:億速云 閱讀:156 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“SpringCloud hystrix斷路器與全局解耦怎么實(shí)現(xiàn)”,在日常操作中,相信很多人在SpringCloud hystrix斷路器與全局解耦怎么實(shí)現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”SpringCloud hystrix斷路器與全局解耦怎么實(shí)現(xiàn)”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

在ProductController 和OrderController 中都使用了局部服務(wù)降級(jí),但同時(shí)也導(dǎo)致兩個(gè)問題, 通過觀察兩個(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ù),為解決此問題,可以使用注解 @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)類,在實(shí)現(xiàn)類中統(tǒng)計(jì)管理服務(wù)降級(jí)解耦的方法。

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

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

訂單服務(wù)引入依賴 openfegin , 商品服務(wù)分別引入依賴 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í)方法

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

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);
        // 后面需要測試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問服務(wù)地址:" + ip + ":" + port + "  " + "查詢商品訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測試超時(shí)熔斷
        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);
        // 后面需要測試負(fù)載均衡,所以返回 ip 地址及端口號(hào)
        product.setName("當(dāng)前訪問服務(wù)地址:" + ip + ":" + port + "  " + "從購物車刪除訂單,訂單號(hào):" + id);
        product.setPrice(new BigDecimal(10000.0));
        System.out.println(product);
        //測試異常熔斷
        System.out.println(10 / 0);
        return product;
    }
}

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

因?yàn)橛唵畏?wù) OrderController中通過 接口IOrderFeignService和 注解@FeignClient 遠(yuǎn)程調(diào)用商品服務(wù)ProductController,因此當(dāng)訂單服務(wù) OrderController出現(xiàn)超時(shí)或異常,可以通過訂單服務(wù) OrderController中通過 接口IOrderFeignService的 實(shí)現(xiàn)類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ù)訪問/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ù)訪問/order/delete/1 10/0異常:"+id);
        return product;
    }
}

3、修改IOrderFeignService代碼

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

@Component // 讓 spring 可以識(shí)別,不加也行,但是在注入的時(shí)候 IDEA 會(huì)報(bào)錯(cuò),不會(huì)影響運(yù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)類

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

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

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

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

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

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

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

# 配置eureka客戶端信息
eureka:
  client:
    service-url:
      # 配置eureka客戶端服務(wù)order-service的注冊地址,與 eureka-server 中暴露地址要保持一致
      defaultZone: http://localhost:8000/eureka/
  instance:
    prefer-ip-address: true # 是否使用 IP 地址注冊,默認(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} # 如果想在控制頁面看到服務(wù)地址與端口,可以將 instance-id 這樣配置

feign:
  hystrix:
    enabled: true

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

由于訂單服務(wù)中引入的spring-cloud-starter-openfeign依賴中已經(jīng)集成了feign-hystrix,有了對(duì) Hystrix 的支持,所以不需要額外引入依賴項(xiàng)。如果需要開啟 訂單服務(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客戶端信息
eureka:
  client:
    service-url:
      # 配置eureka客戶端服務(wù)order-service的注冊地址,與 eureka-server 中暴露地址要保持一致
      defaultZone: http://localhost:8000/eureka/
  instance:
    prefer-ip-address: true # 是否使用 IP 地址注冊,默認(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} # 如果想在控制頁面看到服務(wù)地址與端口,可以將 instance-id 這樣配置

feign:
  hystrix:
    enabled: true

6、進(jìn)行測試

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

SpringCloud?hystrix斷路器與全局解耦怎么實(shí)現(xiàn)

SpringCloud?hystrix斷路器與全局解耦怎么實(shí)現(xiàn)

到此,關(guān)于“SpringCloud hystrix斷路器與全局解耦怎么實(shí)現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI