spring-cloud(十三)GateWay 跨域、HTTP 超时配置


本文spring-cloud 版本为:hoxton.sr6

本文spring-boot版本为:2.2.x-2.3.x

在实际开发中,会涉及到跨域问题,请求时间超时设置等等,接下来,咱们就进行研究学习…

一、gateway 跨域问题解决

(1)java代码解决跨域问题

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;
/**
 * @author lei
 * @version 1.0
 * @date 2020/10/21 21:18
 * @desc 代码设置 解决跨域
 */
@Configuration
public class GwCorsFilter {

    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
		// 允许cookies跨域
        config.setAllowCredentials(true); 
        // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedOrigin("*");
        // #允许访问的头信息,*表示全部
        config.addAllowedHeader("*");
        // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.setMaxAge(18000L);
        //允许的方法 可设置* 即允许全部http请求方法类型
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

(2)yml配置解决跨域

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
          	# 允许向该服务器提交请求的URI
            allowedOrigins:
              - http://192.168.124.17
            # 允许跨域的方法
            allowedMethods:
              - GET
              - POST
              - DELETE
            # 预检请求的缓存时间(秒),即在这个时间段里对于相同的跨域请求不会再预检
            maxAge: 180

懒人版简写配置,一律写*

spring:
  application:
    #服务名配置
    name: app-gateway
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            #是否允许cookie跨域  true允许
            allowCredentials: true
            #允许跨域的url * 则允许全部
            allowedOrigins: '*'
            # 允许跨域的方法 * 则全部
            allowedMethods: '*'
            # 跨域预检时间
            maxAge: 180

二、HTTP超时时间设置

超时时间设置即响应和连接时间设置…

(1)对具体路由做http超时配置

spring:
  application:
    #服务名配置
    name: app-gateway
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            #是否允许cookie跨域  true允许
            allowCredentials: true
            #允许跨域的url * 则允许全部
            allowedOrigins:
              - http://192.168.124.17
            # 允许跨域的方法 * 则全部
            allowedMethods:
              - GET
              - POST
              - DELETE
            # 跨域预检时间
            maxAge: 180
#      default-filters:
#        - AddResponseHeader=X-Response-Default, panghu
#        - PrefixPath=/lei-app
      routes:
        #简单的路由
        - id: easy-order
          uri: http://localhost:9002/
          predicates:
            - Path=/order/**
        #根据服务名路由
        - id: server-order
          uri: lb://demo-order
          predicates:
            - Path=/order/**
          # 针对于当前路由的http超时时间设置
          metadata:
            response-timeout: 2000
            connect-timeout: 2000
        - id: server-product
          uri: lb://demo-product
          predicates:
            - Path=/lei-app/product/**,/zs/{aa}
          filters:
            - StripPrefix=1
     	 # 针对于当前路由的http超时时间设置
          metadata:
            response-timeout: 2000
            connect-timeout: 2000

因为我们得demo-product服务启动了两个,且有一个服务的接口线程睡眠了4秒,那么按照上方配置http 连接以及响应时间均为两秒的话,则有一个可正常访问,有一个服务则会报出异常

image-20201021214357794

image-20201021214417304

image-20201021214508873

如果对每个服务Http 超时设置不需要做那些细微的区别,我们则可以对整个服务群配置全局http超时:

(2)对全局路由超时

对我们所有的路由做一个全局的http连接 超时时间设置…

spring:
  application:
  cloud:
    gateway:
      httpclient:
        #默认为毫秒单位 connect-timeout 默认为45s
        connect-timeout: 1000
        #点进源码可设置不同单位,我这里设置为5s
        response-timeout: 5s

那么,问题来了,如果我的服务,即设置了全局http时间,又对某个别路由设置了特定的http设置,那么生效的是哪一个呢?咱们来测试一下…

假设,咱们现在的配置是这样

spring:
  cloud:
    gateway:
      httpclient:
        #默认为毫秒单位,connect-timeout 默认时长为45s
        connect-timeout: 5000
        #点进源码可设置不同单位,我这里设置为5s
        response-timeout: 5s
      routes:
        - id: server-product
          uri: lb://demo-product
          predicates:
            - Path=/lei-app/product/**,/zs/{aa}
          filters:
            - StripPrefix=1
          # 针对于当前路由的http超时时间设置
          metadata:
            response-timeout: 2000
            connect-timeout: 2000

image-20201021215916450

所以呢!!当我们针对某个路由的HTTP超时时间设置后以及又做了全局HTTP超时时间,其路由仍然是以自己路由超时时间设置为准!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐