SpringCloud-网关GateWay

  一、添加依赖
 <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
 二、路由转发
server:
    port: 9527

spring:
    application:
        name: cloud-gateway
    cloud:
        gateway:
            discovery:
                locator:
                    enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
            routes:
                - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#                  uri: http://localhost:8001          #匹配后提供服务的路由地址
                  uri: lb://cloud-payment-service #匹配后提供服务的路由地址
                  predicates:
                      - Path=/payment/get/**         # 断言,路径相匹配的进行路由

                - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#                  uri: http://localhost:8001          #匹配后提供服务的路由地址
                  uri: lb://cloud-payment-service #匹配后提供服务的路由地址
                  predicates:
                      - Path=/payment/lb/**         # 断言,路径相匹配的进行路由


eureka:
    instance:
        hostname: cloud-gateway-service
    client: #服务提供者provider注册进eureka服务列表内
        service-url:
            register-with-eureka: true
            fetch-registry: true
            defaultZone: http://eureka7001.com:7001/eureka
   三、Predicate
server:
    port: 9527

spring:
    application:
        name: cloud-gateway
    cloud:
        gateway:
            discovery:
                locator:
                    enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
            routes:
                - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#                  uri: http://localhost:8001          #匹配后提供服务的路由地址
                  uri: lb://cloud-payment-service #匹配后提供服务的路由地址
                  predicates:
                      - Path=/payment/get/**         # 断言,路径相匹配的进行路由

                - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
#                  uri: http://localhost:8001          #匹配后提供服务的路由地址
                  uri: lb://cloud-payment-service #匹配后提供服务的路由地址
                  predicates:
                      - Path=/payment/lb/**         # 断言,路径相匹配的进行路由
                      - After=2023-02-16T14:17:33.916+08:00[Asia/Shanghai]
                      - Cookie=username,zhangsan    # 请求时需携带cookie
                      - Header=X-Request-Id, \d+  # 请求头要有X-Request-Id属性并且值为整数的正则表达式

eureka:
    instance:
        hostname: cloud-gateway-service
    client: #服务提供者provider注册进eureka服务列表内
        service-url:
            register-with-eureka: true
            fetch-registry: true
            defaultZone: http://eureka7001.com:7001/eureka
    四、filter

          自定义全局过滤器

@Component
@Slf4j
public class MyLogGatewayFilter implements GlobalFilter,Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("*********come in MyLogGatewayFilter "+new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if(uname == null){
            log.info("********用户名为null 非法用户");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    /**
     * 加载过滤器顺序 越小越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值