1. 程式人生 > 其它 >GateWay的作用和兩種配置方式

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程式碼的配置是優先於配置檔案的