zoukankan      html  css  js  c++  java
  • Hystrix快速搭建

    Hystrix快速整合:

    ​ Hystrix在服务端和客户端都可以进行降级、熔断、限流

    • POM依赖
            <!--netflix-hystrix-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
    
    • YML

      如果通过OpenFeign进行调用时,要开启Hystrix

    feign:
      hystrix:
        enabled: true   # 在Feign中开启Hystix
    
    • 主启动

    @EnableCircuitBreaker或@EnableHystrix

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableEurekaClient
    @EnableCircuitBreaker
    public class PaymentHystrixMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentHystrixMain8001.class, args);
        }
    
        /**
         * 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
         * ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream"
         * 只要在自己的项目里配置上下面的servlet就可以了
         *
         * @return
         */
        @Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
    }
    

    服务端

    • 业务类

      服务端进行处理: @HystrixCommand

        //===== 服务熔断
        @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
                @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//开启断路器
                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//请求次数
                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),//时间窗口期
                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")//失败率达到多少后跳闸
        })
        public String paymentCircuitBreaker(Integer id) {
            if (id < 0) {
                throw new RuntimeException("**** id 不能为负数 ***");
            }
            String serialNumber = IdUtil.simpleUUID();
            return Thread.currentThread().getName() + "	" + "调用成功,流水号:" + serialNumber;
        }
        public String paymentCircuitBreaker_fallback(Integer id) {
            return "id 不能为负数,请稍后再试,id:" + id;
        }
    

    客户端

    结合feign进行客户端处理

    @Service
    @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
    public interface PaymentHystrixService {
    

    ​ Controller进行全局处理

    //@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
    public class OrderHystrixController {
        //下面是全局fallback方法
        public String payment_Global_FallbackMethod() {
            return "Global异常处理信息,请稍后重试";
        }
    }
    

    服务监控Hystrix Dashboard

    搭建cloud-consumer-hystrix-dashboard9001

    • POM
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>springcloud</artifactId>
            <groupId>com.example</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
    
        <dependencies>
            <!--hystrix-dashboard 仪表盘图形化监控界面-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--图形化监控-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!--devtools-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
        </dependencies>
    </project>
    
    • yml
    server:
      port: 9001
    
    • 主启动
    package com.example;
    
    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;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableHystrixDashboard
    public class HystrixDashboardMain9001 {
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashboardMain9001.class, args);
        }
    }
    

    9001监控8001

    • 注意:新版本Hystrix需要在主启动类MainApplication中指定监控路径
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableEurekaClient
    @EnableCircuitBreaker
    public class PaymentHystrixMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentHystrixMain8001.class, args);
        }
    
        /**
         * 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
         * ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream"
         * 只要在自己的项目里配置上下面的servlet就可以了
         *
         * @return
         */
        @Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
    }
    
  • 相关阅读:
    流式布局思想 js函数的几种简写方式 面向对象 js vue引入bootstrap和jQuery环境
    04--CBV源码分析 Django的settings源码分析 模板层
    C#使用委托和事件来重写串口的接收数据方法DataReceived方法完成数据的接收处理
    自定义控件的封装、常用的鼠标事件的重载、定时器的使用、event事件以及事件过滤器、文件操作以及文本流和数据流的使用
    QT的优点、项目文件目录、main函数、QpushButton、对象树、Qt中坐标系、Qt中信号和槽、自定义信号和槽、信号和槽的拓展、Qt4版本中的信号和槽的缺点、Lambda表达式
    Stylet框架显示自定义界面的步骤
    MahApps.Metro使用
    async和await的使用
    第一个WPF程序(串口调试)
    结构、类、属性:以及面向对象的各种特性继承、封装、多态
  • 原文地址:https://www.cnblogs.com/luliang888/p/13252756.html
Copyright © 2011-2022 走看看