springCloud gateway 配置

1. 引入的依赖jar

<!--eureka相关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <!--熔断jar-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!-- 健康检测 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <!-- 路由 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

2. 启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;

/**
 * description: GateWay2Application <br>
 * date: 2021/3/29 16:10 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@EnableDiscoveryClient
@EnableEurekaClient
@ComponentScan("com.cn")
@SpringBootApplication
public class GateWay2Application {

    public static void main(String[] args) {
        SpringApplication.run(GateWay2Application.class, args);
        System.out.println("路由服务已启动");
    }

}

3. yaml配置路由

server:
  port: 9006
  tomcat:
    uri-encoding: utf-8

spring:
  application:
    name: gate2-way
  cloud:
    gateway:
      # 请求超时时间,如下,超过5秒钟后端服务没有响应,将返回给客户端
      httpclient:
        connect-timeout: 1000
        response-timeout: 5s
      discovery:
        locator:
          # 是否和服务注册与发现组件结合,设置为 true 后可以直接使用应用名称调用服务
          enabled: true
      # 路由(routes:路由,它由唯一标识(ID)、目标服务地址(uri)、一组断言(predicates)和一组过滤器组成(filters)。filters 不是必需参数。)
      routes:
        # 路由标识(id:标识,具有唯一性)
        - id: api-test1
          # 目标服务地址(uri:地址,请求转发后的地址)
          uri: lb://api-test1
          # 路由条件(predicates:断言,匹配 HTTP 请求内容)
          predicates:
            ## 转发地址格式为 http://ip/admin/demo/test,StripPrefix=1 的话,那么最终转发到服务的地址是:/demo/test
            - Path=/api1/**
        # =====================================
        - id: api-test2
          uri: lb://api-test2
          predicates:
            - Path=/api2/**
      #默认的拦截器(主要处理熔断)
      default-filters:
        - StripPrefix=0
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback
        # 当请求失败的时候,重试次数,至少1次
        #- name: Retry
        #  args:
        #      retries: 1
        #      statuses: BAD_GATEWAY

# 连接注册中心
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.3.129:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90


# 以下配置针对gateway 请求超时的
hystrix:
  command:
    default:  #default全局有效,service id指定应用有效
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 5000
          # 以下参数默认为10,不调整的话大并发的时候会出错
          #semaphore:
          #  maxConcurrentRequests: 500
          
  threadpool:
    default:
      coreSize: 100
      maxQueueSize: 1500
      queueSizeRejectionThreshold: 1000


4. Fallback:

package com.cn.fallback;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * description: Fallback <br>
 * date: 2020/6/10 10:21 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@RestController
public class Fallback {

    @GetMapping(value = "/fallback")
    public Object fallback() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 500);
        result.put("success", false);
        result.put("obj", null);
        result.put("msg", "未知错误");
        return result;
    }

}

5.关于超时配置说明

  • gateway httpclient 的超时设置:
    spring:
      cloud:
        gateway:
          httpclient:
            connect-timeout: 1000
            response-timeout: 5s

    以上配置中,connect是连接超时,response是后端服务超过指定的时间没有响应后超时,例如:调用用户详情查询接口,超过5秒的时间,后端服务没有响应,gateway路由将会端口请求,直接返回给调用端。

  •  hystrix 熔断超时设置:
    # 以下配置针对gateway 请求超时的
    hystrix:
      command:
        default:  #default全局有效,service id指定应用有效
          execution:
            timeout:
              enabled: true
            isolation:
              thread:
                timeoutInMilliseconds: 5000
              # 以下参数默认为10,不调整的话大并发的时候会出错
              #semaphore:
              #  maxConcurrentRequests: 500
              
      threadpool:
        default:
          coreSize: 100
          maxQueueSize: 1500
          queueSizeRejectionThreshold: 1000

    # 以上配置中,当 timeout.enable =true 的时候,超时会生效。 
    # 注意: hystrix  超时配置中,timeoutInMilliseconds <= response-timeouttimeoutInMilliseconds 才会生效。

    1.  httpcient.response-timeout=3s, 而 hystrix 的 timeoutInMilliseconds=9000,那么当接口请求超过3秒的时候,hystrix 不生效。

    2.  httpcient.response-timeout=9s, 而 hystrix 的 timeoutInMilliseconds=3000,那么当接口请求超过3秒的时候,hystrix 在生效。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值