美文网首页
2023-12-20 网关getway

2023-12-20 网关getway

作者: 大也 | 来源:发表于2024-01-14 16:43 被阅读0次

    官方 文档
    https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/

    1,pom
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    2,配置文件

    server:
      port: 9527
    spring:
      application:
        name: cloud-gateway
    eureka:
      instance:
        hostname: cloud-gateway-service
      client: #服务提供者provider注册进eureka服务列表内
        service-url:
          register-with-eureka: true
          fetch-registry: true
          defaultZone: http://eureka7001.com:7001/eureka
    

    3,主启动类

    image.png
    4,针对pay模块,设置路由:
    - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
    uri: lb://cloud-payment-service #匹配后提供服务的路由地址
    predicates:
    - Path=/payment/get/** # 断言,路径相匹配的进行路由

    5,开始测试


    image.png

    6,GateWay的网关配置,

     gateway:
          discovery:
            locator:
              enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          routes:
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001          #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service #匹配后提供服务的路由地址 lb 负载均衡 CLOUD-PAYMENT-SERVICE
              predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
    

    7使用硬编码配置GateWay:

    @Configuration
    public class GetWayConfig {
    //    public Route.AsyncBuilder geturia(PredicateSpec r){
    //        return r.path("/guoji").uri("http://news.baidu.com/guonei");
    //    }
        @Bean
        public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
            RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes(); //https://news.baidu.com/guonei
            routes.route("path_route_atguigu", r -> r.path("/guoji").uri("https://news.baidu.com/guoji"))
                    .build();
            return routes.build();
        }
    }
    

    8,然后重启服务即可

    spring:
      application:
        name: cloud-gateway
      cloud:
        gateway:
          #      discovery:
          #        locator:
          #          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          routes:
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001          #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
    
            - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
              #uri: http://localhost:8001          #匹配后提供服务的路由地址
              uri: lb://cloud-payment-service #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/lb/**         # 断言,路径相匹配的进行路由
    #            - After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
                #            - Before=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
                #            - Between=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] ,  2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
                #            curl http://localhost:9527/payment/lb --cookie "username=zzyy"
                #            - Cookie=username,zzyy   #Cookie=cookieName,正则表达式
                # 请求头要有X-Request-Id属性并且值为整数的正则表达式 curl http://localhost:9527/payment/lb --cookie "username=zzyy" -H "X-Request-Id:11"
    #            - Header=X-Request-Id, \d+
    #            - Host=**.atguigu.com  # curl http://localhost:9527/payment/lb -H "Host:afae.atguigu.com"
    

    9.断言

    predicates:
                - Path=/payment/lb/**         # 断言,路径相匹配的进行路由
    #            - After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] #!!当前时间
    #            - Before=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
    #            - Between=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] ,  2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
    #                Cookie
    #                curl http://localhost:9527/payment/lb --cookie "username=zzyy"
    #            - Cookie=username,zzyy   #Cookie=cookieName,正则表达式
    #                请求头要有X-Request-Id属性并且值为整数的正则表达式
    #                curl http://localhost:9527/payment/lb --cookie "username=zzyy" -H "X-Request-Id:11"
    #            - Header=X-Request-Id, \d+ 请求头有X-Request-Id属性 且为正数
    #                curl http://localhost:9527/payment/lb -H "Host:afae.atguigu.com"
    #            - Host=**.atguigu.com
    #                请求方式
    #            - Method=GET,POST
    #                路径
    #            - Path=/red/{segment},/blue/{segment}
    #                要有 username 且要是正数
    #            - Query=username, \d+
    #                远端地址
    #            - RemoteAddr=192.168.1.1/24
    #10。1 /11/12
    

    10.过滤器

    @Component
    @Slf4j
    public class MyLogGateWayFilter implements GlobalFilter, Ordered {
    
        /**
         *  代理过滤器 方法 GatewayFilterChain chain方法链对象 需要返回
         *  exchange 方法信息  链内信息
         * */
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            log.info("***********come in MyLogGateWayFilter:  " + new Date());
    
            // 获取请求参数中的 uname
            String uname = exchange.getRequest().getQueryParams().getFirst("uname");
    
            if (uname == null) {
                log.info("*******用户名为null,非法用户,o(╥﹏╥)o");
                exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
                return exchange.getResponse().setComplete();
            }
    
            return chain.filter(exchange);
        }
    
        /**
         * 加载过滤器顺序,数字越小优先级越高 implements Ordered
         *
         * @return
         */
        @Override
        public int getOrder() {
            return 0;
        }
    }
    

    [ERROR] Failed to execute goal on project cloud-gateway-gateway9527: Could not resolve dependencies for project com.atguigu.springcloud:cloud-gateway-gateway9527:jar:1.0-SNAPSHOT: Failed to collect dependencies at com.atguigu.springcloud:cloud-api-commons:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for com.atguigu.springcloud:cloud-api-commons:jar:1.0-SNAPSHOT: Could not find artifact com.atguigu.springcloud:cloud2020:pom:1.0-SNAPSHOT -> [Help 1]

    https://blog.csdn.net/weixin_40731298/article/details/129644391
    概念

    image.png image.png
    image.png
    image.png image.png
    image.png
    image.png

    相关文章

      网友评论

          本文标题:2023-12-20 网关getway

          本文链接:https://www.haomeiwen.com/subject/guyzgdtx.html