天天看點

(三)Hystrix服務容錯處理

Hystrix:可以簡單認為是,服務的調用者(消費者)調用服務提供者提供的服務時的異常處理。

Feign整合Hystrix服務容錯

基本環境

參考:https://blog.csdn.net/admin_15082037343/article/details/107071725

  • Fallback方式
  1. demo-client添加依賴
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
           
  1. 啟動類新增注解@EnableHystrix
package com.demo.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
    
}
           
  1. application.yml啟動斷路器
feign:
  hystrix:
    enabled: true
           
  1. 新增類UserClientHystrix
package com.demo.client.fallback;

import com.demo.client.api.UserClient;
import com.demo.client.entity.User;
import org.springframework.stereotype.Component;

@Component
public class UserClientHystrix implements UserClient {

    @Override
    public Object findUser(Integer pageNo, Integer size) {
        return "{}";
    }

    @Override
    public User save(User user) {
        return new User();
    }

    @Override
    public User findById(String id) {
        return new User();
    }

    @Override
    public String deleteById(String id) {
        return "-1";
    }

}
           

這裡可以了解為調用服務失敗傳回預設值

  1. @FeignClient指定fallback
  1. 驗證

此時啟動注冊中心和服務消費者,不啟動服務提供者,通路接口,不會報錯

(三)Hystrix服務容錯處理

{},就是在UserClientHystrix 的findUser失敗是傳回的預設值。

  • FallbackFactory方式
  1. 新增類UserClientFallbackFactory
package com.demo.client.fallback;

import com.demo.client.api.UserClient;
import com.demo.client.entity.User;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class UserClientFallbackFactory implements FallbackFactory<UserClient> {

    @Override
    public UserClient create(Throwable throwable) {
        throwable.printStackTrace();
        return new UserClient() {
            @Override
            public Object findUser(Integer pageNo, Integer size) {
                return "{}";
            }

            @Override
            public User save(User user) {
                return new User();
            }

            @Override
            public User findById(String id) {
                return new User();
            }

            @Override
            public String deleteById(String id) {
                return "-1";
            }
        };
    }

}
           
  1. @FeignClient更新

這裡用fallbackFactory 而不是fallback

Hystrix監控

application.yml新增配置

management:
  endpoints:
    web:
      exposure:
        include: health,info,hystrix.stream
           

浏覽器通路:http://127.0.0.1:8082/actuator/hystrix.stream

(三)Hystrix服務容錯處理

整合Dashboard檢視監控資料

  1. 這裡建立一個子產品demo-dashboard,并添加依賴
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
           
  1. 啟動類
package com.demo.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class DashboardApplication {

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

}
           
  1. application.yml
server:
  port: 9011

spring:
  application:
    name: demo-dashboard
           

這裡沒有注冊到注冊中心

  1. 浏覽器通路:http://127.0.0.1:9011/hystrix
    (三)Hystrix服務容錯處理
    這裡的位址為http://127.0.0.1:8082/actuator/hystrix.stream
(三)Hystrix服務容錯處理

Turbine聚合叢集資料

  1. demo-dashboard中添加依賴
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
           
  1. 啟動類新增注解
package com.demo.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableHystrixDashboard
@EnableDiscoveryClient
@EnableTurbine
public class DashboardApplication {

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

}
           
  1. application.yml
eureka:
  client:
    service-url:
      defaultZone: http://admin:[email protected]:8761/eureka/
turbine:
  app-config: demo-client
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
           

turbine.app-config:需要監控的應用

這裡需要注冊到注冊中心

  1. 啟動,浏覽器通路:http://127.0.0.1:9011/turbine.stream
    (三)Hystrix服務容錯處理
  2. 浏覽器通路: http://127.0.0.1:9011/hystrix
    (三)Hystrix服務容錯處理
    填寫路徑
(三)Hystrix服務容錯處理
源碼參考

https://github.com/phone15082037343/demo.git

繼續閱讀