SpringCloud GateWay 使用 网关路由

简介: SpringCloud GateWay 使用 网关路由

GateWay之路由转发和过滤

在一个Gateway项目(配置了eureka等组件)中进行配置

server:
  port: 9006
spring:
  application:
    name: zhao-service-gateway
  cloud:
    gateway:
      routes:
       - id: service-autodeliver-router
         #uri: http://127.0.0.1:8091
         uri: lb://zhao-service-autodeliver
         predicates:
          - Path= /autodeliver/**
       - id: service-resume-router
         #uri: http://127.0.0.1:8081
         uri: lb://zhao-service-resume
         predicates:
           - Path=/resume/**
         filters:
           - StripPrefix=1

通过第一个服务hao-service-autodeliver的配置形式,使用固定ip和服务名均可正常通过网关项目访问到服务,但是固定ip的方式不太灵活,而 lb://zhao-service-autodeliver可以实现随机的负载均衡,且不用填写固定ip也避免了不要的麻烦

第二个服务配置中 filters:- StripPrefix=1这个配置会过滤掉第一个路径配置,所以我们在最后访问的时候,除了需要加上第一个过滤掉的配置,还需要加上原本的配置。访问形式如下

GateWay断言

上述针对路径的配置即是断言predicates的配置,而Gateway还内置了以下几种断言

基本上上述断言都是基于请求携带的信息进行过滤的,在实际操作过程中可以综合使用这些信息来达到我们想要的操作

GateWay自定义全局过滤器

@Component
@Slf4j
public class BlackListFilter implements GlobalFilter, Ordered{
    private  static final List<String> blackList=new ArrayList<>();
    static {
        blackList.add("0:0:0:0:0:0:0:1");//模拟本机ip地址 fhadmin.cn
    }
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response =exchange.getResponse();
        String clientIp = request.getRemoteAddress().getHostString();
        if (blackList.contains(clientIp)){
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            log.error(clientIp+"在黑名单中,拒绝访问");
            String data = "request be denied";
            DataBuffer wrap = response.bufferFactory().wrap(data.getBytes());
            return response.writeWith(Mono.just(wrap));
        }
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return 0;
    }
}

通过该过滤器拦截了黑名单中的请求(该操作在实际中可借助mysql或redis等数据存储实现),实现效果

GateWay的高可用

⽹关作为⾮常核⼼的⼀个部件,如果挂掉,那么所有请求都可能⽆法路由处理,因此我们需要做GateWay的⾼可⽤。GateWay的⾼可⽤很简单:可以启动多个GateWay实例来实现⾼可⽤,在GateWay的上游使⽤Nginx等负载均衡设备进⾏负载转发以达到⾼可⽤的⽬的。启动多个GateWay实例(假如说两个,⼀个端⼝9002,⼀个端⼝9003),剩下的就是使⽤Nginx等完成负载代理即可。

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
15天前
|
算法 NoSQL API
SpringCloud&Gateway网关限流
SpringCloud&Gateway网关限流
39 7
|
1月前
|
负载均衡 Nacos 数据安全/隐私保护
SpringCloud GateWay 使用
SpringCloud GateWay 使用
23 0
|
1天前
|
负载均衡 Java 网络架构
在SpringCloud2023中快速集成SpringCloudGateway网关
本文主要简单介绍SpringCloud2023实战中SpringCoudGateway的搭建。后续的文章将会介绍在微服务中使用熔断Sentinel、鉴权OAuth2、SSO等技术。
21 2
在SpringCloud2023中快速集成SpringCloudGateway网关
|
15天前
|
前端开发 Java 应用服务中间件
Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)
Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)
|
15天前
|
Java 微服务 Spring
SpringCloud&Gateway全局过滤器
SpringCloud&Gateway全局过滤器
10 1
|
16天前
|
监控 Java API
第七章 Spring Cloud 之 GateWay
第七章 Spring Cloud 之 GateWay
19 0
|
1月前
|
JSON 安全 关系型数据库
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
|
1月前
|
Java Nacos 网络架构
SpringCloud Gateway的使用 + Nacos动态路由
SpringCloud Gateway的使用 + Nacos动态路由
|
1月前
|
缓存
SpringCloud Gateway 网关的请求体body的读取和修改
SpringCloud Gateway 框架中,为了处理请求体body,实现多次读取与修改,创建了一个名为`RequestParamGlobalFilter`的全局过滤器。这个过滤器使用`@Component`和`@Slf4j`注解,实现了`GlobalFilter`和`Ordered`接口,设置最高优先级以首先读取body。它通过缓存请求体并创建装饰过的`ServerHttpRequest`来实现body的动态获取。
57 4
|
2月前
|
缓存 Java API
【云原生】Spring Cloud Gateway的底层原理与实践方法探究
【云原生】Spring Cloud Gateway的底层原理与实践方法探究