一、配置

1、依赖管理

gateway

   

   

       org.springframework.cloud

       spring-cloud-starter-gateway

   

   

   

       org.springframework.cloud

       spring-cloud-starter-netflix-eureka-client

   

2、启动类

通过 springboot 注解启动,添加服务中心的扫描注解。

@SpringBootApplication

@EnableDiscoveryClient

public class Application {

   public static void main(String[] args) {

       SpringApplication.run(Application.class, args);

  }

}

3、配置文件

server:

port: 8888

spring:

cloud:

  nacos:

    discovery:

      server-addr: 127.0.0.1:8848

  gateway:

    globalcors:

      corsConfigurations:

        '[/**]': # 匹配所有请求

          allowedOrigins: "*" #跨域处理 允许所有的域

          allowedHeaders: "*"

          allowedMethods: # 支持的方法

            - GET

            - POST

            - PUT

            - DELETE

    routes:

      - id: user-routes #唯一的标识 用户自定义

        uri: lb://user-server

        predicates:

        - Path=/user/**,/tr/**  #映射的web访问地址

        filters:

        - RewritePath=/api/(?.*), /$\{segment}

4、请求方式

指定可以访问的请求方式,可以指定多种。

allowedMethods:

- GET

- POST

- HEAD

5、负载均衡和熔断

 Gateway中默认就已经集成了 Ribbon 负载均衡和 Hystrix 熔断机制。但是所有的超时策略都是走的默认值,比如熔断超时时间只有1S,很容易就触发了。因此建议手动进行配置.

hystrix:

command:

  default:

    execution:

      isolation:

        thread:

timeoutInMilliseconds: 6000 #服务降级超时时间,默认1S

ribbon:

ConnectTimeout: 1000 # 连接超时时长

ReadTimeout: 2000 # 数据通信超时时长

MaxAutoRetries: 0 # 当前服务器的重试次数

MaxAutoRetriesNextServer: 0 # 重试多少次服务

好文链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: