gateway小回忆--基础

gateway
路由 由id 目标URI 一系列断言和过滤器组成
断言 根据请求头和请求参数来确定是否连接
过滤器 可以在请求在路由前或者路由后进行修改,添加自己定义的操作
客户端向gateway发送请求,gateway中找到匹配的路由,找到后发送到gateway web handler,handler在通过指定的过滤器来讲i请求发送到我们实际的服务执行业务逻辑,然后返回。
流程为,在路由中包含相关的信息,断言中有这些信息是否为true,若全为true,则看filter中执行自己添加的动作,执行后发往后端

我的理解在真实端口上套了一层马甲
那么开始一系列操作
1建moudle,gateway-test 。
加依赖

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

2写yaml配置

server:
  port: 8514

spring:
  application:
    name: service-user-gateway-test
  main:
    web-application-type: reactive
  cloud:
    nacos:
      discovery:
        server-addr:  localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true  #为true代表 我们可以通过微服务名称调用,不推荐
      enabled: true #是否开启网关

3主启动类

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class,args);
    }
}

启动

已经入住nacos
现在试一试如何做网关映射,即不想暴露真实地址,套一层网关
同样的建moudle 加依赖,写 写主启动类,写服务进行测试

依赖没啥需要额外添加的
主启动

@EnableDiscoveryClient
@SpringBootApplication
public class GateApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateApplication.class,args);
    }
}

服务层

@RestController
public class TestController {
    @GetMapping("little/gateway/test")
    public String abb(){
        return "success";
    }
    @GetMapping("little/gateway/test1")
    public String aee(){
        return "success1";
    }
    @GetMapping("little/gateway/test2")
    public String aff(){
        return "success2";
    }
}

开始写application.yaml

server:
  port: 8901


spring:
 application:
    name: nacos-gateway-pro-test
 main:
    web-application-type: reactive
 cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
改gateway-test的yaml配置
server:
  port: 8900

spring:
  application:
    name: service-user-gateway-test
  main:
    web-application-type: reactive
  cloud:
    nacos:
      discovery:
        server-addr:  http://192.168.150.1:8848
    gateway:
      discovery:
        locator:
          enabled: true  #为true代表 我们可以通过微服务名称调用,不推荐
      enabled: true #是否开启网关
      routes: #因为controller中有两个方法所以配两套 id uri predicates
        #在server-user.controller.UserInfoController中有两个方法,且端口为8512,现在我们要求暴露的端口是8514,所以进行以下操作
        - id: test_route1 #路由ID,全局唯一,没有固定要求
          #uri: http://localhost:8901 #目标微服务的请求地址和端口  我们服务交给了nacos 如果有集群,直接指定IP加端口是不可取的 上面的8514是真是地址这里的8020是马甲,是对外暴露的地址
          uri: lb:// nacos-gateway-pro-test
          predicates:
            - Path=/little/gateway/test/** #断言,路径相匹配的进行路由,就是你的@GetMapping("little/gateway/test"),下面同理
           # - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问

        - id: test_route2 #路由ID,全局唯一
          #uri: http://localhost: 8901 #目标微服务的请求地址和端口  我们服务交给了nacos 如果有集群,直接指定IP加端口是不可取的
          uri: lb:// nacos-gateway-pro-test
          predicates:
            - Path=/little/gateway/test2/**
           # - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问

        - id: test_route3 #路由ID,全局唯一
          #uri: http://localhost: 8901 #目标微服务的请求地址和端口  我们服务交给了nacos 如果有集群,直接指定IP加端口是不可取的
          uri: lb:// nacos-gateway-pro-test
          predicates:
            - Path=/little/gateway/test3/**
            # - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问

可以试一下localhost:8900/little/gateway/test 和 localhost:8901/little/gateway/test 效果一致,等于是下访问的8900 然后找,找到8901的URI进行访问
这里如果用到openfeign则1可以将其中的

@FeignClient(value = "service-user-test")
//改为@FeignClient(value = "service-user-gateway-test")

类内的方法改为
TestController类内的方法

public interface TestFeign {
    @PostMapping("register")
    public String register(@RequestBody String userRegisterDto);
}

这样相当于先访问openfeign的端口,这个端口展gateway的马甲端口,马甲端口找真实端口,

端口写死不适合后面的更改,所以改为微服务名uri: lb:// 微服务名

断言 两种shortcut ,fully expand 取比较常见的

predicates:
  - Path=/little/gateway/test/** #断言,路径相匹配的进行路由,就是你的@GetMapping("little/gateway/test"),下面同理
 # - After=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间后才可以访问,之前不可访问
  #- Before=2022-05-22T16:35:05.794+08:00[Asia/Shanghai] #指定时间之前才可以访问,超过不可访问
#  - Between=2022-05-22T16:35:05.794+08:00[Asia/Shanghai],2022-05-23T16:35:05.794+08:00[Asia/Shanghai] #指定时间之间才可以访问
  - Cookie=username,zzbds #两个参数 Cookie name,正则表达式,匹配上执行,否则不执行,username 要等于zzbds
  - Header=X-Request-Id,\d+ #X-Request-Id为整数才能访问
  - Host=**.some.org,**.someHost.org #可以有多个**为模糊匹配,写清楚的必须符合,即符合前面两个的主机可以访问本服务
  - Query=green,\d+ #带请求参数才能访问,参数名为green 且是整数才能访问,不确定是否是必须呆在浏览器中
  - RemoteAddr=192,168.3.150/24 #外部访问的IP限制

自定义断言和过滤器后面会讲

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想禁用Spring Cloud Gateway中的Swagger资源,可以在Gateway中添加一个过滤器来实现。 首先,在你的Gateway应用程序中,你需要创建一个过滤器类。这个过滤器将使用Spring Cloud Gateway中的RouteLocator来查找Swagger资源,然后将它们过滤掉。下面是一个示例过滤器类: ```java @Component public class SwaggerResourceFilter implements GlobalFilter, Ordered { @Autowired private RouteLocator routeLocator; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String requestPath = request.getPath().toString(); // 获取所有的路由 List<Route> routes = routeLocator.getRoutes().collectList().block(); // 遍历路由,查找是否包含Swagger资源 for (Route route : routes) { String routePath = route.getUri().toString(); if (requestPath.startsWith(routePath) && route.getMetadata().containsKey("swagger")) { // 如果请求路径包含Swagger资源,直接返回 return Mono.empty(); } } // 如果请求路径不包含Swagger资源,继续执行过滤器链 return chain.filter(exchange); } @Override public int getOrder() { return -1; } } ``` 在这个过滤器类中,我们首先使用RouteLocator获取所有的路由,然后遍历这些路由,查找是否包含Swagger资源。如果请求路径包含Swagger资源,直接返回,否则继续执行过滤器链。 接下来,在你的Gateway配置文件中,添加以下代码来注册这个过滤器类: ```yaml spring: cloud: gateway: default-filters: - SwaggerResourceFilter ``` 这将会在Gateway启动时自动注册这个过滤器类。当你访问Swagger资源时,Gateway将会过滤掉这些资源,从而禁用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值