zoukankan      html  css  js  c++  java
  • Spring Cloud 微服务笔记(六)Spring Cloud Hystrix

    Spring Cloud Hystrix

    Hystrix是一个延迟和容错库,旨在隔离远程系统、服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力。

    一、快速入门

    1)依赖:

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

    2)启用断路模式

    @SpringBootApplication
    @EnableHystrix
    @EnableDiscoveryClient
    public class ClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(ClientApplication.class, args);
        }
    }

    3)降级

    @Component
    public class UserService implements IUserService {
        @Override
        @HystrixCommand(fallbackMethod = "defaultUser")
        public String getUser(String username) throws Exception {
            if (username.equals("spring")) {
                return "Right user.";
            } else {
                throw new Exception();
            }
        }
        /**
        * 调用该方法返回预设友好错误
        * */
        public String defaultUser(String username) {
            return "Use the default user.";
        }
    }

    在Feign中默认是自带Hystrix功能的,在老版本中是默认打开的,从最近几个版本中开始默认

    关闭了,所以需要通过配置文件打开它。

    二、Hystrix Dashboard

    Hystrix Dashboard仪表盘是根据系统一段时间内发生的请求情况来展示的可视化面板,

    这些信息是每个HystrixCommand执行过程中的信息。

    依赖:

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

    启用:

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

    三、Hystrix异常处理机制

    Hystrix的异常处理中,有5种出错的情况会被fallback所截获:

    1)FAILURE:执行失败,抛出异常。

    2)TIMEOUT:执行超时。

    3)SHORT_CIRCUITED:断路器打开。

    4)THREAD_POOL_REJECTED:线程池拒绝。

    5)SEMAPPHORE_REJECTED:信号量拒绝。

  • 相关阅读:
    Java的赋值、浅克隆和深度克隆的区别
    String 字符串中含有 Unicode 编码时,转为UTF-8
    http 请求类
    springboot情操陶冶-jmx解析
    springboot情操陶冶-@Conditional和@AutoConfigureAfter注解解析
    springboot情操陶冶-@SpringBootApplication注解解析
    springboot情操陶冶-@Configuration注解解析
    springboot情操陶冶-SpringApplication(二)
    springboot情操陶冶-SpringApplication(一)
    springboot情操陶冶-初识springboot
  • 原文地址:https://www.cnblogs.com/Shadowplay/p/10608617.html
Copyright © 2011-2022 走看看