GateWay网关学习

1、引包

        <!--引入了依赖默认即开启gateway了,如果暂时不想使用这个功能,这可以配置spring.cloud.gateway.enabled=false即可。-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- 服务的注册与发现 eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        

2、route 路由流程

包含:路由规则,路由校验,过滤处理,容错处理
在这里插入图片描述

3、路由配置

  • Route

    一个gateway里包含多个路由Route,每个路由Route都有自己的id,url,predicate集合(谓语/断定式子)以及Filters集合。

    Filters:所有生效的filter都说GateWayFilter的实例,在GateWay运行过程中负责代理服务“之前”或“之后”做一些事情。

    predicate: 谓词(规则),断言 匹配条件,校验

  • RouteDefinition

    具体查看RouteDefinition类

在这里插入图片描述

  • ​ **predicates **谓词(规则),断言

    具体看GatewayPredicate接口的实现类 命名规则就是XXXGatewayPredicate 使用工厂+内部类来定义的接口实现
    在这里插入图片描述

    注意:

    ​ # 定义一个谓词。格式: 谓词名字=参数 或者 name:名字 args:参数

    spring:
    	cloud: # 配置Spring Cloud相关属性
            gateway: # 配置Spring Cloud Gateway相关属性
                # 连接超时控制
          		httpclient:
           			connectTimeout: 2000
           			responseTimeout: 10s
                routes: # 配置网关中的一个完整路由,包括命名,地址,谓词集合(规则),过滤器集
                	# 每一个 - 代表一个对象
                    - id: first # 路由定义的命名,唯一即可。命名规则符合Java中的变量符命名规则
                      predicates: # 配置胃词集合
                      	# 定义一个谓词。格式: 谓词名字=参数     或者  name:名字 args:参数
                    	- Path=/api/** 
                    	#- name: xxx
                    	#  args: xxx
    
  • Filters 过滤

    具体看 GatewayFilter接口的实现类 命名规则就是XXXGatewayFilterFactory 使用工厂+内部类来定义的接口实现

    注意:

    ​ - xx=xx # 定义一个过滤器 格式:过滤器名字=参数 或者 name: 名字 args: 参数

    spring:
    	cloud: # 配置Spring Cloud相关属性
            gateway: # 配置Spring Cloud Gateway相关属性
                # 连接超时控制
          		httpclient:
           			connectTimeout: 2000
           			responseTimeout: 10s      
    routes: # 配置网关中的一个完整路由,包括命名,地址,谓词集合(规则),过滤器集
    	- id: first # 路由的ID,自动义的命名,没有固定规则但要求唯一,简易配合服务名。命名规则符合Java中的变量符命名规则
          # lb -代表loadbalance
          # 当前路由定义对应的微服务转发地址
          uri: lb://basecmm-custom
    	  # 过滤器
    	  filters: # 配置过滤器集合
             # 过滤转发地址前缀,过滤1节
             # 如:请求地址 - http://Localhost:8060/api/getArgs?name=admin&age=20
             # 对应的谓词,规则是 /basecmm-custom,符合
             # 对应的uri是Lb://basecmm-custom 转换成 http://basecmm-custom且包含负载均衡
             # 转发地址是: http://basecmm-custom/api/getArgs?name=admin&age=20
             # 过滤器是  过滤转发地址前缀,过滤1节,即删除/basecmm-custom -> http://basecmm-custom/getArgs?name=admin&age=20
    		 # - xx=xx # 定义一个过滤器   格式:过滤器名字=参数  或者 name: 名字  args: 参数
             - StripPrefix=1
             - PrefixPath=/basecmm-custom
           # 保留原始请求的host头信息,并原封不动的转发出去,而不是被gateway的http客户端重置。
           # - PreserveHostHeader
    
  • Uri配置

    在gateway中配置uri配置有三种方式,包括
    第一种:ws(websocket)方式: uri: ws://localhost:9000
    第二种:http方式: uri: http://localhost:8130/
    第三种:lb(注册中心中服务名字)方式: uri: lb://basecmm-custom

第一种配置文件yml

#################################### common config : ####################################
server:
  port: 8060
logging:
  config: classpath:logback-spring.xml
  file:
    path: ./logs/
spring:
  application:
    name: gaea-gateway
  cloud: # 配置Spring Cloud相关属性
    gateway: # 配置Spring Cloud Gateway相关属性
      # 连接超时控制
      httpclient:
        connectTimeout: 2000
        responseTimeout: 10s
      discovery: # 配置网关发现机制
        locator: # 配置处理机制
          # 只要请求地址符合规则:http://gatewayIp:gateayPort/微服务名称/微服务请求地址
          # 网关自动映射。把请求转发到http://微服务名称/微服务请求地址
          # ps:有微服务叫:basecmm-custom
          # 请求地址:http://localhost:8060/basecmm-custom/getArgs?name=abc111&age=21
          # 自动转发到: http://basecmm-custom/getArgs?name=abc111&age=21
          # 表明Gateway开启服务注册和发现的功能,并且Spring Cloud Gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
          # ps:如果为true,配合定义的route  http://192.168.0.182:8060/api/getArgs?name=abc111&age=21     http://192.168.0.182:8060/basecmm-custom/getArgs?name=abc111&age=21 都可以访问  如果enabled为false并且不配做route 则请求不通
          enabled: false # 开启网关自动映射处理逻辑  生产关闭 防止拿到转发地址
          lower-case-service-id: true # 开启服务名称小写转换 eureka对服务命名管理默认全大写,需转换一下
          filters:
            # 全局路由保留原始请求的host头信息,并原封不动的转发出去,而不是被gateway的http客户端重置。
            - PreserveHostHeader
			
      routes: # 配置网关中的一个完整路由,包括命名,地址,谓词集合(规则),过滤器集
        # 每一个- 代表一个对象
        - id: first # 路由定义的命名,唯一即可。命名规则符合Java中的变量符命名规则
          # lb -代表loadbalance
          # 当前路由定义对应的微服务转发地址
          uri: lb://basecmm-custom
          # 谓词,命名是有套路的。是GatewayPredicate 接口实现的命名前缀,XxxRoutePredicateFactory
          predicates: # 配置胃词集合
            - Path=/api/** # 定义一个谓词。格式: 谓词名字=参数或者name:名字 args:参数
#            - name: xxx
#              args: xxx
          # 过滤器
          filters: # 配置过滤器集合
            # 过滤转发地址前缀,过滤1节
            # 如:请求地址 - http://Localhost:8060/api/getArgs?name=admin&age=20
            # 对应的谓词,规则是 /basecmm-custom,符合
            # 对应的uri是Lb://basecmm-custom 转换成 http://basecmm-custom且包含负载均衡
            # 转发地址是: http://basecmm-custom/api/getArgs?name=admin&age=20
            # 过滤器是  过滤转发地址前缀,过滤1节,即删除/basecmm-custom -> http://basecmm-custom/getArgs?name=admin&age=20
            - StripPrefix=1
            - PrefixPath=/basecmm-custom
            # - xx=xx # 定义一个过滤器   格式:过滤器名字=参数  或者 name: 名字  args: 参数

            # 保留原始请求的host头信息,并原封不动的转发出去,而不是被gateway的http客户端重置。
            - PreserveHostHeader



#################################### eureka config : ####################################
eureka:
  client:
    service-url:
      defaultZone: http://192.168.1.202:8761/eureka/,http://192.168.1.202:8763/eureka/,http://192.168.1.202:8765/eureka/
  instance:
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
    prefer-ip-address: true

第二中配置

@Configuration
public class RouteConfig {
    @Bean("baseCustomRouteLocator")
    public RouteLocator baseCustomRouteLocator(RouteLocatorBuilder builder) {
        // lb 请求默认会将服务名称去掉 如果需要加上服务名 需要用到prefixPath  或者后端不加prefixPath   前端地址需要写上服务名
        // 例如:  http://192.168.0.182:8060/api/getArgs?name=abc111&age=21
        // 或者  http://192.168.0.182:8060/api/basecmm-custom/getArgs?name=abc111&age=21
        return builder.routes()
                .route("basecmm-custom",r -> r.path("/api/**").filters(f->f.stripPrefix(1).prefixPath("/basecmm-custom")).uri("lb://basecmm-custom")).build();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值