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

SpringCloud Hystrix的使用

 更新時間:2021年04月16日 10:32:11   作者:遇見_line  
這篇文章主要介紹了SpringCloud Hystrix的使用,幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下

簡介

在分布式系統(tǒng)中,服務(wù)與服務(wù)之間依賴錯綜復(fù)雜,一種不可避免的情況就是某些服務(wù)將會出現(xiàn)失敗。Hystrix是一個庫,它提供了服務(wù)與服務(wù)之間的容錯功能,主要體現(xiàn)在延遲容錯和容錯,從而做到控制分布式系統(tǒng)中的聯(lián)動故障。Hystrix通過隔離服務(wù)的訪問點,阻止聯(lián)動故障,并提供故障的解決方案,從而提高了這個分布式系統(tǒng)的彈性。

面對的問題: 一個應(yīng)用一般會依賴多個服務(wù),每個服務(wù)由于網(wǎng)絡(luò)不可靠,機房的不可靠等等不穩(wěn)定的因素,總會導(dǎo)致服務(wù)的故障,如果我們不對這些故障做處理,就會進而導(dǎo)致整個系統(tǒng)的不可用。

服務(wù)雪崩:

一個正常的用戶進入調(diào)用微服務(wù)A然后調(diào)用B在調(diào)用C然后離開,而當(dāng)其中微服務(wù)C出現(xiàn)故障,導(dǎo)致用戶停留
B,越來越多的用戶進入,請求,停留B在最終導(dǎo)致B的資源被耗盡,不可用,進而A也慢慢不可用。這一系
列鏈?zhǔn)椒磻?yīng)就像雪崩一樣影響越來越大,稱為"服務(wù)雪崩"

服務(wù)熔斷

參考:www.dbjr.com.cn/article/166784.htm

應(yīng)對雪崩效應(yīng)的一種微服務(wù)鏈路保護機制

當(dāng)調(diào)用鏈路的某個微服務(wù)不可用或者響應(yīng)時間太長時,會進行服務(wù)熔斷,不再有該節(jié)點微服務(wù)的調(diào)用,快速返回錯誤的響應(yīng)信息。當(dāng)檢測到該節(jié)點微服務(wù)調(diào)用響應(yīng)正常后,恢復(fù)調(diào)用鏈路。

在Spring Cloud中通過Hystrix實現(xiàn)。Hystrix會監(jiān)控微服務(wù)間調(diào)用的狀況,當(dāng)失敗的調(diào)用到一定閾值,缺省是5秒內(nèi)20次調(diào)用失敗,就會啟動熔斷機制。

服務(wù)熔斷解決如下問題:

  1. 當(dāng)所依賴的對象不穩(wěn)定時,能夠起到快速失敗的目的;
  2. 快速失敗后,能夠根據(jù)一定的算法動態(tài)試探所依賴對象是否恢復(fù)

實踐

項目搭建

根據(jù) 實驗環(huán)境搭建中的項目copy建立一個新模塊名為springcloud-provider-dept-hystrix。

導(dǎo)入依賴

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix</artifactId>
     <version>1.4.6.RELEASE</version>
</dependency>

使用

在Controller層,使用@HystrixCommand來實現(xiàn)熔斷

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {
    String groupKey() default "";
    String commandKey() default "";
    String threadPoolKey() default "";  
    String fallbackMethod() default "";
    HystrixProperty[] commandProperties() default {};
    HystrixProperty[] threadPoolProperties() default {};
    Class<? extends Throwable>[] ignoreExceptions() default {};
    ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;
    HystrixException[] raiseHystrixExceptions() default {};
    String defaultFallback() default "";
}


在@HystrixCommand中有 defaultFallback() 指定默認的備用方法(default ""), fallbackMethod() 指定失敗后進行的備用方法(default "")

當(dāng)正常的方法調(diào)用失敗后(5秒內(nèi)20次調(diào)用失敗),認為是出故障了就進行熔斷,快速返回錯誤信息(調(diào)用備選方法)。

@RestController
public class DeptController {
    @Autowired
    private DeptImpl deptimpl;
    @RequestMapping("/dev/{id}")
    @HystrixCommand(fallbackMethod = "HystrixGet")//指明備用方法
    public Dept DeptqueryByID(@PathVariable("id") Long id) {
        Dept dept = deptimpl.queryByID(id);
        System.out.println(dept);
        if (dept==null) {
            throw new RuntimeException("id--->" + id + "用戶不存在");
        }
        return dept;
    }
    public Dept HystrixGet(@PathVariable("id") Long id) {
        Dept dept=new Dept();
        dept.setDeptnumber(id.intValue());
        dept.setDname("id"+id+"用戶不存在");
        dept.setD_source("no~~~~~~");
        return dept;
    }

}

在啟動類上添加@EnableCircuitBreaker開啟熔斷支持

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//開啟熔斷支持
public class HApplication {
    public static void main(String[] args) {
        SpringApplication.run(HApplication.class,args);
    }    
}

服務(wù)降級

即在服務(wù)器壓力劇增的情況下,關(guān)閉一些很少被調(diào)用的服務(wù),騰出一些資源,保證正常運行。

如淘寶雙十一關(guān)閉退款通道。

實踐

在原本的FeignClient指明fallbackFactory

@FeignClient(value = "PROVIDER-NAME",fallbackFactory = SerciceFallbackFactory.class)
public interface DeptClientService {
    @RequestMapping(value = "/dev/add")
    boolean add(Dept dept);

    @RequestMapping(value = "/dev/{id}")
    Dept queryByID(@PathVariable("id") Long id );

    @PostMapping(value = "/dev/list")
    List<Dept> queryAll();
}

定義自己的FallbackFactory

報錯注意import feign.hystrix.FallbackFactory;

import feign.hystrix.FallbackFactory;
@Component
public class SerciceFallbackFactory implements FallbackFactory {

    public DeptClientService create(Throwable cause) {
        return new DeptClientService() {
            public boolean add(Dept dept) {
                return false;
            }
            //定義返回的錯誤信息
            public Dept queryByID(Long id) {
                Dept dept = new Dept();
                dept.setD_source("服務(wù)降級");
                dept.setDname("fail");
                dept.setDeptnumber(-1);
                return dept;
            }

            public List<Dept> queryAll() {
                return null;
            }
        };
    }
}

在客戶端的配置文件中添加

#開啟降級
feign:
  hystrix:
    enabled: true

結(jié)果:在我們關(guān)閉服務(wù)端后再次訪問服務(wù)時

服務(wù)熔斷與服務(wù)降級的區(qū)別

  • 服務(wù)熔斷是在服務(wù)端進行的,而服務(wù)降級是在客戶端進行的
  • 服務(wù)熔斷的原因:發(fā)生故障,服務(wù)降級:為整體負荷考慮,保證核心業(yè)務(wù)的運行

服務(wù)監(jiān)控 Dashboard

建立項目

導(dǎo)入依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

配置文件

server:
  port: 9001
hystrix:
  dashboard:
# Hystrix Dashboard會通過proxyUrl解析到host部分,然后通過配置的proxyStreamAllowList。判定是否允許被訪問
    proxy-stream-allow-list: "localhost" 
    

開啟監(jiān)控支持

@SpringBootApplication
@EnableHystrixDashboard//開啟
@EnableDiscoveryClient
public class DashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(DashboardApplication.class,args);
    }
}

運行后訪問:http://localhost:9001/hystrix

根據(jù)提示在spingcloud-provider-dept-hystrix服務(wù)端添加bean

@Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean servlet = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        servlet.addUrlMappings("/actuator/hystrix.stream");
        return servlet;
    }

運行后訪問 http://localhost:8081/actuator/hystrix.stream 可以獲得一些服務(wù)的信息

注意: 需要調(diào)用一次標(biāo)注有 @HystrixCommand 方法才會有數(shù)據(jù)顯示,只會監(jiān)控有 @HystrixCommand 的方法

我們也可以通過在http://localhost:9001/hystrix 輸入

按下按鈕開啟對該服務(wù)的監(jiān)控

以上就是SpringCloud Hystrix的使用的詳細內(nèi)容,更多關(guān)于SpringCloud Hystrix的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論