天天看点

(三)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

继续阅读