SpringCloud Gateway配置

SpringCloud Gateway 配置

最近项目中需要升级网关,替代之前的zuul.

SpringCloud Gateway简介

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

SpringCloud Gateway基本配置

首先是pom


	<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.1.RELEASEversion>
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <tomcat.version>9.0.12tomcat.version>
        <spring-cloud.version>Greenwich.M3spring-cloud.version>
    properties>
    <dependencies/>
 		
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
		
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-ribbonartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
     <dependencies/>        

yml配置

server:
  port: 8100

spring:
  application:
    name: gateway-service
  redis:
    database: 0
    host: 192.168.0.1
    port: 8200
    password: 000000
    timeout: 10000ms
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 1
        max-wait: 5000ms
  cache:
    type: REDIS
  cloud:
    gateway:
      discovery: #是否与服务发现组件进行结合,通过 serviceId(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。
        locator: #路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。
          enabled: true
      routes:
      - id: config
        uri: lb://config-service
        predicates:
        - Path=/api/config-service/**
        filters:
        - StripPrefix=2
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback
      - id: authorization
        uri: lb://authorization-service
        predicates:
        - Path=/api/authorization-service/**
        filters:
        - StripPrefix=2 # 比如,请求/name/bar/foo,去除掉前面两个前缀之后,最后转发到目标服务的路径为/foo
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback
        args:
        name: fallbackcmd
        fallbackUri: forward:/fallback
      
ribbon:
  ReadTimeout:  5000
  ConnectTimeout: 5000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 0
  OkToRetryOnAllOperations: false

hystrix:
  command:
    fallbackcmd:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000
  circuitBreaker:
    sleepWindowInMilliseconds:  5000

logging:
  config: classpath:spring-logback.xml

eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.1.1:8000/eureka
    registryFetchIntervalSeconds: 5
  instance:
    prefer-ip-address: true

id:路由的id,参数配置不要重复,如不配置,gateway会使用生成一个uuid代替。
uri:路由的目标地址。注意:uri地址后面不要加 " / "
Path:配置路由的路径。

你可能感兴趣的:(springcloud)