那些不懂hystrix的祕密

一 前言

springcloud系列文章已經出到hystrix,中間知識追尋者跑去學了其它知識,回來感受spingcloud系列出的也很多了;須要徹底理解這篇文章對於初學者須要有必定的基礎知識,若是看不懂請移步知識追尋者的springcloud專欄進行學習;學完本篇,你將得到學會使用Hstrix進行服務容錯,學會Hystrix熔斷監控;能學完本篇內容請留下贊呦!!!!java

二 Hystrix入門

首先須要搭建工程hystrix-client 引入eureka和 hystrix依賴git

2.1pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

2.2 service

在service層定義一個得到用戶的方法getUser()用於提供服務,方法內容對用戶名進行判斷,若是是zszxz, 返回回用戶說明,不然拋出異常;定義默認用戶方法defaultUser()用於容錯處理,在得到用戶方法上面添加註釋@HystrixCommand 而且將容錯回調指向defaultUser()方法;有關注釋詳細使用請讀者參考以下官方文檔;github

Hystrix@HystrixCommand配置web

/**
 * @Author lsc
 * <p> </p>
 */
@Component
public class UserService {

    // 爲getUser方法添加容錯回調處理
    @HystrixCommand(fallbackMethod = "defaultUser")
    public String getUser(String params){
        if (params.equals("zszxz")){
            return "the user is zszxz";
        }else {
            // 拋出異常時會直接調用fallbackMethod中指定的方法
            throw new RuntimeException();
        }
    }
    /* *
     * @Author lsc
     * <p> 出錯回調處理</p>
     * @Param [params]
     * @Return java.lang.String
     */
    public String defaultUser(String params){
        return "it is the user thar is not exist in project";
    }
}

2.3 controller

表現層直接調用服務層方法,參數爲用戶名usernamespring

/**
 * @Author lsc
 * <p> </p>
 */
@RestController
public class UserController {

    @Autowired
    UserService  userService;

    @GetMapping("user")
    public String getUser(String username){
        return userService.getUser(username);
    }
}

2.4application.yml

配置文件中指定端口8094 , 而且指定應用名稱爲 hystrix-client , 後面是向eureka-server進行服務註冊;app

server:
  port: 8094

spring:
  application:
    name: hystrix-client # 應用名稱

eureka:
  client:
    service-url:
      # 服務註冊地址
      defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/

2.5 Hsytrix啓動類

在啓動類上添加註釋@EnableHystrix ,表示啓用 hystrix功能;ide

/**
 * @Author lsc
 * <p> </p>
 */
@SpringBootApplication
@EnableHystrix // 啓用 hystrix
@EnableDiscoveryClient// 服務註冊發現
public class HystrixApp {

    public static void main(String[] args) {
        SpringApplication.run(HystrixApp.class, args);
    }
}

2.6 執行結果

啓動eureka-server, eureka-client, hystrix-client工程;訪問地址http://localhost:8094/user?user=zszxz;得出正確訪問結果以下;函數

修改用戶名參數不是zszxz 發現報的不是異常,而是defualtuser中默認的容錯處理,說明使用hystrix成功;spring-boot

三 Hstrix集成Feign

學會了簡單使用hystrix, 但遠遠不夠,一般Feign中自帶了hystrix功能,這邊須要使用Feign進行開啓hystrix,而後進行容錯處理;學習

3.1 eureka-client

在以前的eureka-client 表現層中添加一個 getFH() 方法用於Feign客戶端調用;

/* *
     * @Author lsc
     * <p> feign-hystix 集成測試</p>
     * @Param [username]
     * @Return java.lang.String
     */
    @GetMapping("zszxz/fh")
    public String getFH(String username){
        if (username.equals("zszxz")){
            return "the user is zszxz on fh project";
        }else {
            throw new RuntimeException();
        }

    }

3.2 pom.xml

新建工程feign-hystrix 用於集成測試,依賴引入 openfeign 便可;

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

3.3service

service層使用FeignClient進行遠程過程調用,而且指定 回調類,其內容在3.4節

/**
 * @Author lsc
 * <p> </p>
 */
@FeignClient( name = "eureka-client", value = "eureka-client", fallback = FHServiceFallback.class)
public interface FHService {

    @GetMapping("zszxz/fh")
    public String getFeign(String username);

}

3.4 fallback

回調函數實現FHService接口,而且在類上添加註解@Component 表示會被spirng IO容器掃描注入;

/**
 * @Author lsc
 * <p> </p>
 */
@Component
public class FHServiceFallback implements FHService {
    @Override
    public String getFeign(String username) {

        return "sorry feilure,you can try it again";
    }
}

3.5controller

controller層直接能夠調用service層接口;

/**
 * @Author lsc
 * <p> feign 表現層 </p>
 */
@RestController
public class FHController {

    @Autowired
    FHService fhService;

    @GetMapping("zszxz/feign")
    public String getFeign(String username){
        // 調用 getFeign方法
        return fhService.getFeign(username);
    }
}

3.6 application.yml

配置文件中指定端口,應用名稱,關鍵是要開啓feign自帶的hystix 功能

server:
  port: 8095

spring:
  application:
    name: hystrix-feign # 應用名稱

eureka:
  client:
    service-url:
      # 服務註冊地址
      defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/

# 開啓hystix
feign:
  hystrix:
    enabled: true

3.7FH啓動類

啓動類中使用註解 @EnableFeignClients 開啓Feign功能;

/**
 * @Author lsc
 * <p> </p>
 */
@SpringBootApplication
@EnableFeignClients
public class FeignHystrixApp {

    public static void main(String[] args) {
        SpringApplication.run(FeignHystrixApp.class,args);
    }
}

3.8 執行結果

正常訪問結果正確,結果以下

輸入用戶名非zszxz,進行容錯處理友好提示以下,說明feign集成使用hystix成功;

四 Hystrix Dashbord監控熔斷器

學會使用hystix仍是皮毛中的皮毛,咱們還須要對hystrix進行容錯監控;

4.1 feign-hystrix工程添加內容

feign-hystrix 工程中添加 actuator , hystrix 依賴;

pom.xml

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

appication.yml配置文件中指定暴露端點;

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

啓動類中添加@EnableHystrix 註解表示開啓Hystrix

/**
 * @Author lsc
 * <p> </p>
 */
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class FeignHystrixApp {

    public static void main(String[] args) {
        SpringApplication.run(FeignHystrixApp.class,args);
    }
}

4.2 hystrix-dashboard

新建工程hystrix-dashboard 用於專門的容錯監控;引入依賴以下;

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>

application.yml 指定當前應用名稱和端口;

server:
  port: 8096

spring:
  application:
    name: hystrix-dashboard # 應用名稱

eureka:
  client:
    service-url:
      # 服務註冊地址
      defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/

啓動類上表名註解 @EnableHystrixDashboard 表示啓動容錯監控;

/**
 * @Author lsc
 * <p> </p>
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard//開啓監控
public class HystixDashboard {

    public static void main(String[] args) {
        SpringApplication.run(HystixDashboard.class,args);
    }
}

訪問 http://localhost:8096/hystrix 出現一頭豪豬圖頁面表示初步配置成功;

頁面中的輸入url有以下三種:

  1. 集羣應用:http://turbine-hostname:port/turbine.stream
  2. 單個集羣應用:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
  3. 單體應用:http://hystrix-app:port/actuator/hystrix.stream

在頁面輸入地址 http://localhost:8095/actuator/hystrix.stream 訪問單體應用,頁面會處於loading狀態

當咱們訪問 http://localhost:8095/zszxz/feign 會出現儀表盤,其具體的監控指標內容意義請讀者參考官方文檔,再也不詳細說明。

五 turbine聚合監控

在第四節中讀者已經學會如何使用hystix dashboard進行容錯監控;可是缺點也很明顯,那就是一個單體應用須要開啓一個dashboard 頁面;若是使用turbine進行集成管理,那麼全部hystix應用都會顯示在一個 dashboard上,方便咱們查閱,排查;

5.1 hystrix-feign2

新建 hystrix-feign2 工程;修改配置文件端口爲8098,應用名稱爲hystrix-feign2,其它內容跟 hystrix-feign 工程同樣;

5.2 turbine-monitor

新建turbine-monitor工程,添加新依賴 turbine

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
    </dependencies>

application.xml 中內容以下 指定了監控的hystrix應用是hystrix-feignhystrix-feign , 而且採用默認名稱方式;

server:
  port: 8097

spring:
  application:
    name: turbine-monitor # 應用名稱

eureka:
  client:
    service-url:
      # 服務註冊地址
      defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/


turbine:
  appConfig: hystrix-feign,hystrix-feign2
  clusterNameExpression: "'default'"

啓動類中添加新的註解@EnableTurbine 表示開啓turbine 功能;

/**
 * @Author lsc
 * <p> </p>
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApp {

    public static void main(String[] args) {
        SpringApplication.run(TurbineApp.class, args);
    }
}

5.3啓動工程

  1. 訪問 http://localhost:8097/hystrix 出現hystrix界面
  2. 在界面中輸入http://localhost:8097/turbine.stream 訪問進入loding狀態
  3. 訪問 http://localhost:8095/zszxz/feign 得出 ‘sorry feilure,you can try it again’
  4. 訪問 http://localhost:8098/zszxz/feign 得出 ‘sorry feilure,you can try it again’
  5. 最後得出結果以下,發現兩個應用的hystrix 都集中在一個頁面上;

5.4 源碼

關於源碼請移步專欄說明便可得到知識追尋者springcloud所有學習內容;

相關文章
相關標籤/搜索