Spring Cloud GateWay 使用教程

Spring Cloud Gateway

使用步骤

新建项目依赖如下所示

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

yml

server:
  port: 5000
spring:
  application:
    name: cloud_gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        group: microservices

启动后可以在注册中心看到服务
在这里插入图片描述
下一步我们来尝试配置路由

从官网4.1节拷贝这段代码

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Cookie=mycookie,mycookievalue

稍作修改启动

    gateway:
      routes:
        - id: baidu
          uri: https://www.baidu.com

在这里插入图片描述
提示 predicates 不能为空,那么这个字段是什么意思呢?

从配置文件点击去看一下实现

	public void setRoutes(List<RouteDefinition> routes) {
		this.routes = routes;
		if (routes != null && routes.size() > 0 && logger.isDebugEnabled()) {
			logger.debug("Routes supplied from Gateway Properties: " + routes);
		}
	}

稍作修改先启动成功,后面遇到了再详细说

    gateway:
      routes:
        - id: baidu
          uri: https://www.baidu.com
          predicates:
            - Cookie=mycookie,mycookievalue

官网介绍了另一种predicates写法

          predicates:
            - Cookie=mycookie,mycookievalue
          
          predicates:
            - name: Cookie
              args:
               name: mycookie
               regexp: mycookievalue

官方文档第5部分有非常多的Route Predicate Factories,翻译过来是路由谓词工厂。

7.3. The ReactiveLoadBalancerClientFilter

这有一个服务接口 http://localhost:8952/user

        - id: openfeign-8952-user
          uri: http://localhost:8952
          predicates:
            - Path=/user/**
          filters:
            - StripPrefix=1

由于网关在 5000端口,所以当访问5000/user/user 的时候会匹配到http://localhost:8952,但是拼接完的路径是 localhost:8952/user/user,所以我们应该根据过滤器去掉1级所以StripPrefix=1。

如果我们想开启两台进行负载均衡应该怎么办呢 ?
在这里插入图片描述
在url这里就不能写上固定的8952端口了。

          uri: lb://openfeign-8952-user

添加负载均衡依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

连续访问 http://localhost:5000/user/user
可以看到 user8952 和 user8953 交替返回已经具有了负载均衡的能力。

7.3. The ReactiveLoadBalancerClientFilter

By default, when a service instance cannot be found by the ReactorLoadBalancer, a 503 is returned. You can configure the gateway to return a 404 by setting spring.cloud.gateway.loadbalancer.use404=true.

下面我们来演示效果

默认情况返回503
在这里插入图片描述
配置属性

spring:
  cloud:
    gateway:
      loadbalancer:
        use404: true

返回404
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值