• Hystrix整合Gateway


    Hystrix整合Gateway

    一、引入依赖

    核心依赖:

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

    完整依赖

     <dependencies>
            <!-- 网关 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
            <!-- Feign -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!-- 服务注册/发现-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!-- 配置中心-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
            <!-- 整合hystrix -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
            <!-- jwt -->
            <dependency>
                <groupId>com.auth0</groupId>
                <artifactId>java-jwt</artifactId>
            </dependency>
            <!-- lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
    

    二、在启动器添加注解

    如果需要熔断器,需要添加此注解到启动器@EnableCircuitBreaker

    三、修改yml

    如果需要添加某个服务的熔断,可以在某个route中配置

          routes:
            - id: hgk-admin
              uri: lb://hgk-admin
              predicates:
                - Path=/api/admin/**  #访问 /api/thirdparty 这个路径就路由到
              filters:                    #路径重写 成一下 这里中间逗号, 前面是截取的路径,后面是替换的路径
                #也就是你看以为你实际访问是 /api/thirdparty/oss/policy,实际上它是吧 /api//api/thirdparty这一段给丢了,只要只要oss/policy
                #
                - RewritePath=/api/(?<segment>.*),/${segment}
                - name: Hystrix
                  args:
                    name: fallbackcmd
                    fallbackUri: forward:/fallback  #返回路径
    

    如果要添加全局配置

          default-filters:
            - name: Hystrix
              args:
                name: fallbackcmd
                fallbackUri: forward:/fallback  #返回路径
    

    配置熔断:

    #hystrix配置
    hystrix:
      command:
        fallbackcmd:
          execution:
            isolation:
              thread:
                #断路器的超时时间ms,默认1000
                timeoutInMilliseconds: 2000
          circuitBreaker:
            #是否启动熔断器,默认为true,false表示不要引入Hystrix。
            enabled: true
            #当在配置时间窗口内达到此数量的失败后,进行短路
            requestVolumeThreshold: 20
            #出错百分比阈值,当达到此阈值后,开始短路。默认50%)
            errorThresholdPercentage: 50%
            #短路多久以后开始尝试是否恢复,默认5s)-单位ms
            sleepWindowInMilliseconds: 30000
    

    四、添加熔断时调用的接口

    api代码:

    package com.sinosoft.api;
    
    import com.sinosoft.entity.Result;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author rayfoo@qq.com
     * @version 1.0
     * <p></p>
     * @date 2021/1/19 12:35
     */
    @RestController
    public class DefaultHystrixController{
    
        @RequestMapping(value = "/fallback",method = RequestMethod.GET)
        public Result fallback(){
            System.out.println("fallback****************Gateway");
            return Result.builder().code(500).msg("您访问的接口超时。。。").build();
        }
    
    }
    

    result代码:

    package com.sinosoft.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    import org.springframework.http.HttpStatus;
    
    
    /**
     * @author rayfoo@qq.com
     * @date 2020年8月6日
     */
    @Data
    @Builder
    @Accessors(chain = true)
    @NoArgsConstructor
    @AllArgsConstructor
    public class Result {
    
        /**
         * 状态码
         */
        private Integer code;
    
        /**
         * 提示信息
         */
        private String  msg;
    
        /**
         * 数据记录
         */
        private Object data;
    
        /**
         * 构造方法
         * @param code 状态码
         * @param msg 提示信息
         */
        public Result(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        /**
         * 默认的成功方法
         */
        public static Result success(){
            return Result.builder().code(HttpStatus.OK.value()).msg(HttpStatus.OK.name()).build();
        }
    
        /**
         * 默认的成功方法
         * @param msg 成功时提示的信息
         */
        public static Result success(String msg){
            return Result.builder().code(HttpStatus.OK.value()).msg(msg).build();
        }
    
        /**
         * 默认的失败方法
         */
        public static Result error(){
            return Result.builder().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).msg(HttpStatus.INTERNAL_SERVER_ERROR.name()).build();
        }
    
        /**
         * 默认的失败方法
         * @param msg 失败时提示的信息
         */
        public static Result error(String msg){
            return Result.builder().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).msg(msg).build();
        }
    
    }
    
  • 相关阅读:
    模板
    2019牛客暑期多校训练营(第五场)
    Codeforces
    Codeforces
    SCUT
    模板
    Codeforces
    2019 Multi-University Training Contest 4
    Codeforces
    Codeforces
  • 原文地址:https://www.cnblogs.com/zhangruifeng/p/14299372.html
Copyright © 2020-2023  润新知