• springCloud的使用04-----熔断器hystrix的使用


    1. restTemplate+ribbon使用hystrix

      1.1 引入依赖

    <!-- 配置hystrix断路器 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

      1.2 在需要熔断的方法上添加注解

    @Service
    public class HiService {
    
        @Autowired
        RestTemplate restTemplate;
        
        //需要熔断的方法
        @HystrixCommand(fallbackMethod="hiError")//熔断后执行的方法
        public String sayHi() {
            return restTemplate.getForObject("http://SERVICE-HI/info", String.class);
        }
        
        //熔断后执行的方法
        public String hiError() {
            return "sorry hi error";
        }
    }

      1.3 在启动类中声明使用hystrix

    @SpringBootApplication
    @EnableDiscoveryClient//向服务中心注册
    @RestController
    @EnableHystrix//启用熔断机制
    public class ConsumerRibbon {
     
        @Autowired
        private HiService hiService;
        
        public static void main(String[] args) {
            SpringApplication.run(ConsumerRibbon.class, args);
        }
        
        @Bean
        @LoadBalanced//使用这个restTemplate开启负载均衡
        RestTemplate initRestTemplate(){
            return new RestTemplate();
        }
        
        @RequestMapping("info")
        public String hiConsumer() {
            String response=hiService.sayHi();
            return response;
        }
    }

      1.4 启动注册中心和cloud-consumer-ribbon,访问http://localhost:8764/info 返回sorry hi error

        启动service-hi,访问http://localhost:8764/info 返回hello eureka client 8762

    2 feign使用hystrix

      2.1 feign自带熔断器,无需导入hystrix的依赖,但是需要导入以下依赖,否则回报java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect错误

    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-javanica</artifactId>
    </dependency>

      2.2 在配置文件中启用hystrix,默认是关闭的

    feign: 
     hystrix: 
      enabled: true

      2.3 指定熔断后要执行的类

    @FeignClient(value="service-hi",fallback=HiServiceHystric.class)//指定调用哪个服务提供者,指定熔断后的执行的类
    public interface IHiService {
        
        @RequestMapping(value="/info",method=RequestMethod.GET)//指定调用服务提供者的哪个接口
        String info();
        
        @RequestMapping(value="/info",method=RequestMethod.GET)//指定调用服务提供者的哪个接口
        String hi();
    }

      2.4 指定熔断后要执行对应的方法

    @Component
    public class HiServiceHystric implements IHiService {
    
        //熔断后执行相应的方法
        public String info() {
            return "sorry info feign";
        }
    
        public String hi() {
            return "sorry hi feign";
        }
    }

      2.5 在启动类中声明启动hystrix

    @EnableHystrix

      2.6 启动注册中心和cloud-consumer-feign,访问http://localhost:8765/info 返回sorry info feign

        启动service-hi,访问http://localhost:8765/info 返回hello eureka client 8762

    3 使用熔断器监控(hystrix dashboard)

      3.1 引入相应的jar依赖

    <!-- 配置hystrix断路器 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

      3.2 在启动类中声明启动hystrix dashboard

    @SpringBootApplication
    @EnableEurekaClient//向服务中心注册
    @EnableHystrix//启用熔断机制
    @EnableHystrixDashboard //启用熔断器监控页面
    public class ConsumerRibbonApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerRibbonApp.class, args);
        }
        
        @Bean
        @LoadBalanced//使用这个restTemplate开启负载均衡
        RestTemplate initRestTemplate(){
            return new RestTemplate();
        }
    }

      3.3 启动项目

        

        

    4 熔断器聚合监控

      如果多个项目都配置了hystrix和hystrix dashboard,想要在一个项目中的hystrix dashboard上看到其他项目的hystrix dashboard情况,就需要使用turbine进行聚合监控

      4.1 创建springboot项目,引入jar依赖

    <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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.beifeng.hadoop</groupId>
        <artifactId>beifeng-spring-cloud-turbine</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>beifeng-spring-cloud-turbine</name>
        <url>http://maven.apache.org</url>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.RC1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- 声明为web项目 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- 配置eureka -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            
            <!-- 引入hystrix turbine 熔断器聚合监控  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-turbine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-turbine</artifactId>
            </dependency>
        </dependencies>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

      4.2 在配置文件中配置那些项目要进行聚合监控

    spring:
     application: 
      name: service-turbine
    server: 
     port: 8770
    security:
     basic: 
      enabled: false
    turbine:
     aggregator: 
      clusterConfig: default #指定聚合那些集群,多个使用 , 分隔,默认为default
     appConfig: cloud-consumer-ribbon,cloud-consumer-feign #配置要监控的服务,多个使用 , 分隔
     clusterNameExpression: new String("default")
    eureka: 
     client: 
      serviceUrl: 
       defaultZone: http://peer1:8761/eureka/

      4.3. 在启动类中声明启动turbine

    @SpringBootApplication
    @EnableTurbine //开启turbine,该注解包含了@EnableDiscoveryClient
    public class TurbineApp {
        public static void main(String[] args) {
            SpringApplication.run(TurbineApp.class, args);
        }
    }

       4.4 启动项目,在任何一个配置了hystrix dashboard的项目中查看聚合监控

        

        

  • 相关阅读:
    上传文件大小问题
    中检测到有潜在危险的 Request.Form 值
    Session
    C#浏览器中在线操作文档
    数据导入
    文件上传(插件版)和下载
    Leetcode练习(Python):递归类:面试题10- I. 斐波那契数列:写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项。斐波那契数列的定义如下: F(0) = 0,&#160; &#160;F(1)&#160;= 1 F(N) = F(N
    Leetcode练习(Python):递归类:面试题07. 重建二叉树:输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
    Leetcode练习(Python):递归类:面试题 16.11. 跳水板:你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。 返回的长度需要从小到大排列。
    Leetcode练习(Python):递归类:面试题 08.06. 汉诺塔问题:在经典汉诺塔问题中,有 3 根柱子及 N 个不同大小的穿孔圆盘,盘子可以滑入任意一根柱子。一开始,所有盘子自上而下按升序依次套在第一根柱子上(即每一个盘子只能放在更大的盘子上面)。
  • 原文地址:https://www.cnblogs.com/lifeone/p/9010125.html
Copyright © 2020-2023  润新知