前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Gateway的简单使用

Gateway的简单使用

作者头像
CBeann
发布2023-12-25 17:44:41
960
发布2023-12-25 17:44:41
举报
文章被收录于专栏:CBeann的博客CBeann的博客

构建项目

provider-8001

provider-8002

gateway-9527

eureka-7001

依赖,注意不要加web依赖

代码语言:javascript
复制
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

application.yml

代码语言:javascript
复制
server:
  port: 9527
 
 
spring:
  application:
    name: gateway-9527

路由规则小案例

静态路由

配置yml方式
代码语言:javascript
复制
server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
      - id: payment_routh #没有固定的规则但是要求唯一,建议配合服务名
        uri: http://localhost:8001 #匹配后提供的路由地址
        predicates:
        - Path=/payment/get/**  #断言,路径相匹配的进行路由

此处设置的路由规则是 http://localhost:9527/payment/get/** 会路由到 http://localhost:8001/payment/get/**

而不是 http://localhost:9527/payment/get/* 路由到 http://localhost:8001/*

此处我修改上面的代码把

代码语言:javascript
复制
uri: http://localhost:8001 #匹配后提供的路由地址

修改为

代码语言:javascript
复制
uri: http://localhost:8001/abc #匹配后提供的路由地址

访问 http://localhost:9527/payment/get/1 仍然能 路由到 http://localhost:8001/payment/get/1

说明后面的/abc配置没有作用,而且官网里的小案例就是 只有 ipd地址或者 服务名称

编码方式
代码语言:javascript
复制
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){

        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("payment_routh"
                , r->r.path("/payment/get/**").uri("http://localhost:8001"))
                .build();
        return routes.build();
    }

等价于

代码语言:javascript
复制
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
      - id: payment_routh #没有固定的规则但是要求唯一,建议配合服务名
        uri: http://localhost:8001 #匹配后提供的路由地址
        predicates:
        - Path=/payment/get/**  #断言,路径相匹配的进行路由

动态路由

以Eureka为注册中心为例

代码语言:javascript
复制
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

修改启动类

代码语言:javascript
复制
@EnableEurekaClient

修改yml配置文件

代码语言:javascript
复制
server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true  #开启注册中心路由功能
      routes:
      - id: payment_routh #没有固定的规则但是要求唯一,建议配合服务名
        uri: lb://CLOUD-PROVIDER-PAYMENT #此处如果有问题,请注意依赖spring-cloud-starter-netflix-eureka-client依赖不能错
        #uri: http://localhost:8001 #匹配后提供的路由地址
        predicates:
        - Path=/payment/get/**  #断言,路径相匹配的进行路由

Route Predicate路由断言

就是下图中的箭头处下面的规则

下面是官网提供的配置规则

Spring Cloud Gateway

GateWay的Filter

官网地址

Spring Cloud Gateway

Spring Cloud Gateway

下面是复制官网上的一段代码,添加请求头 key=X-request-red,value=biue

代码语言:javascript
复制
spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        filters:
        - AddRequestHeader=X-Request-red, blue

自定义过滤器

代码语言:javascript
复制
@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");//每次进来后判断带不带uname这个key
        if(uname == null){
            log.info("*********用户名为null ,非法用户,o(╥﹏╥)o");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);    //uname为null非法用户
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return 0;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 构建项目
  • 路由规则小案例
    • 静态路由
      • 配置yml方式
      • 编码方式
    • 动态路由
    • Route Predicate路由断言
    • GateWay的Filter
    相关产品与服务
    微服务引擎 TSE
    微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档