美文网首页
spring boot gateway 搭建使用

spring boot gateway 搭建使用

作者: 楚长铭 | 来源:发表于2019-08-23 14:38 被阅读0次
    • 网关在微服务架构里面充当入口的角色,有路由转发,负载均衡等许多作用,在这里不多加累述,本文搭建的gateway网关基于webflux,依赖如下:
        implementation 'org.springframework.boot:spring-boot-starter-webflux'
        implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
        implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
        implementation 'org.springframework.cloud:spring-cloud-starter-consul-config'
        implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'io.projectreactor:reactor-test'
    
    • 其中 redis是为了做限流而配置,注册中心用了consul,开发者可以酌情配置
    • 网关主要使用yml文件进行配置,配置文件如下:
     spring:
      redis:
        host: localhost
        port: 6378
        timeout: 36000
        database: 0
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true #服务中心根据 service id 创建路由
    
          default-filters: #全局默认的filters配置
            - name: RequestRateLimiter # 限流配置 依赖于redis 限流返回状态码429
              args:
                key-resolver: '#{@ipKeyResolver}'
                redis-rate-limiter.replenishRate: 1 #令牌桶每秒填充平均速度
                redis-rate-limiter.burstCapacity: 1 #令牌桶总容量
    
    
          routes: # 负载均衡 路由代理
    
            - id: oauth2-resource-spr
              uri: lb://oauth2-resource-spr
              predicates:
                - Path=/oauth2resource/**  #请求路径带上该前缀
                - Method=GET,POST
              filters:
                - StripPrefix=1 #与path配饰使用,转发去除前缀
    
            - id: oauth2-client-sqr
              uri: lb://oauth2-client-sqr
              predicates:
                - Path=/oauth2client/**  #请求路径带上该前缀
                - Method=GET,POST
              filters:
                - StripPrefix=1 #与path配饰使用,转发去除前缀
    
            - id: consul-test-sqr  # 服务name
              uri: lb://consul-test-sqr # lb:// 注册中心上的服务name
              predicates:
                - Path=/consultest/**  #请求路径带上该前缀
                - Method=GET,POST
              filters:
                - StripPrefix=1 #与path配饰使用,转发去除前缀
                - name: GatewayDemoFilterFactory # 自定义过滤器
                - name: Retry # 重试配置
                  args:
                    retries: 3 #重试次数
                    statuses: BAD_GATEWAY #状态返回码
                - name: Hystrix # 熔断器配置 需要添加熔断器依赖
                  args:
                    name: fallbackcmd
                    fallbackUri: forward:/gatewayHystrix
          #            - name: RequestRateLimiter # 限流配置 依赖于redis 限流返回状态码429
          #              args:
          #                key-resolver: '#{@ipKeyResolver}'
          #                redis-rate-limiter.replenishRate: 1 #令牌桶每秒填充平均速度
          #                redis-rate-limiter.burstCapacity: 1 #令牌桶总容量
    
    • 多数配置的说明已经在注释中有些,其中过滤器可以全局配置也可以单独配置,本文中配置了限流的过滤器,实现根据ip地址限流
    @Component
    public class RateLimiterConfig {
    
        /**
         * 根据请求参数中user字段来限流
         *
         * @return
         */
    //    @Bean
    //    public KeyResolver userKeyResolver() {
    //        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getQueryParams().getFirst("user")));
    //    }
    
        /**
         * 根据ip地址限流
         *
         * @return
         */
        @Bean
        public KeyResolver ipKeyResolver() {
            return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostName());
        }
    
    }
    
    • 配置文件中的熔断器的设置也需要加了入熔断器的依赖,自定义熔断接口为:
    @RestController
    public class Controller {
    
        @GetMapping("/gatewayHystrix")
        public String gatewayHystrix(){
            return "熔断器熔断";
        }
    
    }
    

    相关文章

      网友评论

          本文标题:spring boot gateway 搭建使用

          本文链接:https://www.haomeiwen.com/subject/wottectx.html