springcloud学习-14 gateway新一代网关【周阳springcloud2020学习笔记】

Spring Cloud Gateway 是 Spring Cloud 新推出的网关框架,之前是 Netflix Zuul。
网关通常在项目中为了简化前端的调用逻辑,同时也简化内部服务之间互相调用的复杂度;
具体作用就是转发服务,接收并转发所有内外部的客户端调用;
其他常见的功能还有权限认证,限流控制等等。

Spring Cloud Gateway使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架

作用:
1.反向代理
2.鉴权
3.流量控制
4.熔断
5.日志监控

三大核心概念

  • Route(路由)
    路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
  • Predicate(断言)
    参考的是java8的java.util.function.Predicate
    开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
  • Filter(过滤)
    指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。

Gateway工作流程:路由转发+执行过滤器链


入门配置

1.建module:gateway-gateway9527
2.pom

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>demo2020artifactId>
            <groupId>cn.chen.demogroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>

        <artifactId>gateway-gateway9527artifactId>

        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-gatewayartifactId>
            dependency>

            
            <dependency>
                <groupId>cn.chen.demogroupId>
                <artifactId>api-commonartifactId>
                <version>${project.version}version>
            dependency>

            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>

            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>

        dependencies>

    project>

3.yml

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

            - id: payment_routh2
              uri: http://localhost:8001
              predicates:
                - Path=/payment/lb/**   #断言,路径相匹配的进行路由

    eureka:
      instance:
        hostname: cloud-gateway-service
      client:
        service-url:
          register-with-eureka: true
          fetch-registry: true
          defaultZone: http://eureka7001.com:7001/eureka

4.主启动

    @SpringBootApplication
    @EnableEurekaClient
    public class GateWayApplication9527 {

    	public static void main(String[] args) {
    		SpringApplication.run(GateWayApplication9527.class,args);
    	}
    }

5.业务类:无

6.测试
启动eureka
启动8001
启动9527
访问 http://localhost:8001/payment/get/1
访问 http://localhost:9527/payment/get/1


Gateway网关路由有两种配置方式
1.yml文件中配置 : 见上述demo

2.自己写配置类: 代码中注入RouteLocator的Bean

参考
    import org.springframework.cloud.gateway.route.RouteLocator;
    import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    public class GateWayConfig {

        @Bean
        public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
            RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
            routes.route("path_rote_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
            return routes.build();
        }
    }

通过微服务名实现动态路由
默认情况下Gateway会根据注册中心的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能

启动:一个eureka7001+两个服务提供者8001/8002

yml:
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri
yml修改:uri: lb://payment-service


Predicate的使用

  • 常用的Route Predicate
    Loaded RoutePredicateFactory [After]
    Loaded RoutePredicateFactory [Before]
    Loaded RoutePredicateFactory [Between]
    Loaded RoutePredicateFactory [Cookie]
    Loaded RoutePredicateFactory [Header]
    Loaded RoutePredicateFactory [Host]
    Loaded RoutePredicateFactory [Method]
    Loaded RoutePredicateFactory [Path]
    Loaded RoutePredicateFactory [Query]
    Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
    Loaded RoutePredicateFactory [RemoteAddr]
    Loaded RoutePredicateFactory [Weight]
    Loaded RoutePredicateFactory [CloudFoundryRouteService]

springcloud学习系列目录

你可能感兴趣的:(springcloud学习笔记)