• SpringCloud-Gateway


    一、介绍

    (1)网关服务
    (2)功能:断言、路由、过滤
    (3)能避免用户直接访问到业务主机
    在这里插入图片描述

    二、项目搭建

    a、编写pom.xml(注意移除web框架,gateway中自带有)

    <?xml version="1.0" encoding="UTF-8"?>
    <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>demo20220821</artifactId>
            <groupId>com.wsh.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-gateway-gateway9527</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>com.wsh.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    b、编写application.yml

    server:
      port: 9527
    
    spring:
      application:
        name: cloud-gateway-service
    
    eureka:
      client:
        #    客户端设置为true
        register-with-eureka: true
        #    客户端设置为true
        fetch-registry: true
        service-url:
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
      instance:
        instance-id: gateway9527
        prefer-ip-address: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    c、编写启动类

    package com.wsh.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    /**
     * @ClassName PaymentMain8002
     * @Description: TODO
     * @Author wshaha
     * @Date 2022/8/23
     * @Version V1.0
     **/
    @EnableEurekaClient
    @SpringBootApplication
    public class GatewayMain8002 {
        public static void main(String[] args) {
            SpringApplication.run(GatewayMain8002.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    d、application.yml增加路由配置

    server:
      port: 9527
    
    spring:
      application:
        name: cloud-gateway-service
      cloud:
        gateway:
          routes:
            - id: r1
              uri: http://localhost:8001
              predicates:
                - Path=/payment/test/**
    
    eureka:
      client:
        #    客户端设置为true
        register-with-eureka: true
        #    客户端设置为true
        fetch-registry: true
        service-url:
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
      instance:
        instance-id: gateway9527
        prefer-ip-address: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    e、运行
    在这里插入图片描述
    (4)路由开启支持负载均衡

    server:
      port: 9527
    
    spring:
      application:
        name: cloud-gateway-service
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true #开启使用微服务名路由
          routes:
            - id: r1
              uri: lb://CLOUD-PAYMENT-SERVICE
              predicates:
                - Path=/payment/test/**
    
    eureka:
      client:
        #    客户端设置为true
        register-with-eureka: true
        #    客户端设置为true
        fetch-registry: true
        service-url:
          #      defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka
      instance:
        instance-id: gateway9527
        prefer-ip-address: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述
    在这里插入图片描述

    三、断言配置

    在这里插入图片描述
    在这里插入图片描述

    四、过滤

    (1)
    在这里插入图片描述
    (2)自定义

    package com.wsh.springcloud.config;
    
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.core.Ordered;
    import org.springframework.stereotype.Component;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
    
    /**
     * @ClassName FiltersConfig
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/14
     * @Version V1.0
     **/
    @Component
    public class FiltersConfig implements GlobalFilter, Ordered {
    
    
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
            queryParams.entrySet().stream()
                    .forEach(item -> {
                        System.out.println(item.getKey() + "-" + item.getValue());
                    });
            return exchange.getResponse().setComplete();//结束
    //        return chain.filter(exchange); //进入下一个过滤器
        }
    
        @Override
        public int getOrder() {
            return 0;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    北京互联网营销服务商浩希数字科技申请1350万美元纳斯达克IPO上市
    数据可视化之:没有西瓜的夏天不叫夏天
    DJYGUI系列文章五:GK显示器接口
    Jmeter组件作用域及执行顺序
    Kubernetes常用命令
    CSDN云IDE初体验 - 有些惊艳
    36.6K star!Immich - 一款开源高性能的自托管照片和视频备份方案
    一文带你享受数学之优美
    狂奔的低代码,画风各异的阿里云、腾讯云
    Windows Nginx 服务器部署(保姆级)
  • 原文地址:https://blog.csdn.net/qq_25243147/article/details/133833150