GateWay的作用和两种配置方式

1.网关

gateway使用webflux 底层使用异步非阻塞IO模型

2.作用

1.统一微服务的入口
2.实现请求的负载均衡
3.过滤处理

gateway = filter + router

3.两种配置方式

1.配置文件
server:
  port: 8088

spring:
  application:
    name: GATEWAY
  cloud:
    consul:
      host: localhost
      port: 8500
    gateway:
      routes:
        - id: category_route							# 指定路由唯一标识
          uri: http://localhost:8085 # 指定路由服务的地址
          predicates:
            - Path=/category					  # 指定路由规则

        - id: product_route
          uri: http://localhost:8084
          predicates:
            - Path=/product

2.java代码的配置类
@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("order_route", r -> r.path("/order/**")
                        .uri("http://localhost:9999"))
                .build();
    }
}

java代码的配置是优先于配置文件的

posted @ 2021-10-01 21:07  code-G  阅读(1169)  评论(0编辑  收藏  举报