菜鸟的springcloud学习总结(五):服务网关

说明

更新时间:2020/10/02 23:09,更新到了Gateway

本文主要对springcloud中的服务网关进行学习与记录,主要偏向于实战,本文会持续更新,不断地扩充

本文仅为记录学习轨迹,如有侵权,联系删除

一、服务网关

在这里插入图片描述
按照上面的图进行学习,现在进行到了服务网关这一个模块,主要重点学gateway

二、Gateway

概述
​ SpringCloud Gateaway 是spring Coud的一个全新项目,基于Spring5.0+springBoot 2.0 和Project 、Reactor等技术开发的网关。它旨在为微服务架构提供一种简单有效的同意的API路由管理方式。

SpringCloud Gatteway作为springcloud生态系统中的网关,目标是替代zuul,在spring Cloud2.0的版本中,没有zuul2.0以上的高性能的版本进行集成,仍然还是采用zull1.x非Reactor模式的老版本。而为了提升网关的性能,spring cloud Gateway 是基于WebFlux框架实现的,而WebFlux框架底层是使用了高性能的Reactor模式通信框架Netty。

spring cloud Gateway 的目标是提供统一的额路由方式且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/指标和限流等。

特性
在这里插入图片描述

(1)配置文件配置路由

创建服务网关模块cloud-gateway-gateway9527,引入pom坐标依赖,因为这个要注册在eureka服务中心,所以还需要引入eureka

    <dependencies>

        <!--引入公共包-->
        <dependency>
            <groupId>com.zsc</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

然后是主启动类,该加的注解直接加上去
在这里插入图片描述

重点是eureka和gateway依赖,然后是配置yml文件

server:
  port: 9527
spring:
  application:
    name: cloud-gateway-gateway9527
  cloud:
    gateway:
      routes:
        # 这里如果访问http://localhost:9527/payment/paymentInfoOk/**
        # 实际跟访问http://localhost:8003/payment/paymentInfoOk/**一样
        # 达到隐藏原来服务器和端口号的目的
        - id: payment_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8003   #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/paymentInfoOk/**   #断言,路径相匹配的进行路由

        # 这里如果访问http://localhost:9527/payment/hello
        # 实际上跟访问http://localhost:8003/payment/hello一样
        # 达到隐藏原来服务器和端口号的目的
        - id: payment_routh2
          uri: http://localhost:8003
          predicates:
            - Path=/payment/hello   #断言,路径相匹配的进行路由




#服务中心注册
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:7001/eureka

上面重点是routes配置,这里如果访问ttp://localhost:9527/payment/paymentInfoOk/ 实际跟访问http://localhost:8003/payment/paymentInfoOk/**一样,这样达到隐藏原来服务器和端口号的目的

这样可以测试了,开启项目,这里需要开启3个项目
在这里插入图片描述
在这里插入图片描述

(2)动态路由

当服务是集群的时候,我们可以用动态路由的方式进行路由的转发,同时实现负载均衡

修改cloud-gateway-gateway9527配置文件

server:
  port: 9527
spring:
  application:
    name: cloud-gateway-gateway9527
  cloud:
    gateway:
      #动态路由配置
      discovery:
        locator:
          enabled: true  #开启从注册中心动态创建路由的功能,利用微服务名进行路由

        
      routes:
            #动态路由配置
         - id: payment_routh3
          uri: lb://cloud-provider-payment
          predicates:
            - Path=/payment/** #断言,路径相匹配的进行路由

#服务中心注册
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:7001/eureka


启动项目如下
在这里插入图片描述
在这里插入图片描述
同时实现了负载均衡的效果。

(3)Pridicate断言

上面的项目我们用到了pridecate里面的path关键字,实际上还有其他的关键字
在这里插入图片描述
其余关键字
在这里插入图片描述
After,Before,Between
After后面加时间串,时间串可以用ZonedDateTime.now()获取,表示只有过了这个设置的时间后才可以访问,Before表示某个时间之前,Between则是再某一个时间段
在这里插入图片描述

在这里插入图片描述

Cookie
Cookie表示只有带有cookie,且键值对为username为zs的才可以访问
在这里插入图片描述
带cookie访问,由于浏览器无法带cookie,这里用curl进行访问,访问成功
在这里插入图片描述

Header
表示只有带有相应的请求头才能访问
在这里插入图片描述
成功访问
在这里插入图片描述
还有其他的可以自己百度

(4)filter

Gatewa三个核心功能:路由、断言和过滤链(Filter),现在是最后一个核心功能,过滤链Filter,功能跟之前学过的过滤链一样的,在请求进来的时候进行拦截,进行处理,从而进行过滤。

实现的方式也简单,只要创建一个全局的过滤类即可
在这里插入图片描述
代码如下:

@Component
public class GatewayFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("****************come in GatewayFilter: " + "****************come in GatewayFilter:"+new Date());
        String username = exchange.getRequest().getQueryParams().getFirst("username");
        if (username==null) {
            System.out.println("**************用户名为null");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }


        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

上面配置类中拦截,并且判断是否带有username参数,没有的话无法访问,配置完就可以直接启动了
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值