SpringBoot2.7 + Nacos + GateWay

  1. pom包,主要是记录版本

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.17</version>
    <relativePath/>
    </parent>
    <groupId>com.student.gateway</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <url/>
    <licenses>
    <license/>
    </licenses>
    <developers>
    <developer/>
    </developers>
    <scm>
    <connection/>
    <developerConnection/>
    <tag/>
    <url/>
    </scm>
    <properties>
    <java.version>8</java.version>
    </properties>
    <dependencies>

         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
             <version>2021.0.5.0</version>
         </dependency>
    
         <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-gateway</artifactId>
             <version>3.1.9</version>
         </dependency>
    
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
             <version>2021.0.5.0</version>
         </dependency>
    
         <!--sentinel gateway-->
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
             <version>2021.0.5.0</version>
         </dependency>
         
         <dependency>
             <groupId>com.alibaba.cloud</groupId>
             <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
             <version>2021.0.5.0</version>
         </dependency>
    
         <!-- 202.0.5版本的cloud,需要这个注解才能负载均衡也就是 lb:student才生效 -->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-loadbalancer</artifactId>
             <version>3.1.4</version>
         </dependency>
    
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
    
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
    
    </project>
  2. Nacos搭建,我这边是2.x版本的

  3. yaml配置

    server:
    port: 8004

    spring:
    application:
    name: gateway
    config:
    import: nacos:gateway.yaml // config的全名
    cloud:
    nacos:
    discovery:
    server-addr: 127.0.0.1:8848
    config:
    server-addr: 127.0.0.1:8848
    file-extension: yaml
    sentinel:
    transport:
    dashboard: 127.0.0.1:8005
    eager: true
    gateway:
    routes:
    - id: student
    uri: lb://student
    predicates:
    - Path=/api/redis/**
    filters:
    - StripPrefix=1 // 过滤前缀
    - Log=23 // 自定义过滤器

  4. 自定义过滤器写法

java 复制代码
package com.student.gateway.demo.filters.gateway;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;

@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogModal> {

    public LogGatewayFilterFactory() {
        super(LogModal.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name1");
    }

    @Override
    public GatewayFilter apply(LogModal config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println(config.getName1());
                Mono<Void> mono = chain.filter(exchange);
                return mono;
            }
        };
    }
}
java 复制代码
package com.student.gateway.demo.filters.gateway;

public class LogModal {

    private String name1;

    public String getName1() {
        return name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }

}
  1. 全局过滤器
java 复制代码
package com.student.gateway.demo.filters.gateway;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;

@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogModal> {

    public LogGatewayFilterFactory() {
        super(LogModal.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("name1");
    }

    @Override
    public GatewayFilter apply(LogModal config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println(config.getName1());
                Mono<Void> mono = chain.filter(exchange);
                return mono;
            }
        };
    }
}
  1. sentinel的下载,限流和熔断
相关推荐
wclass-zhengge2 天前
SpringCloud篇(服务网关 - GateWay)
spring boot·spring cloud·gateway
H愚公移山H2 天前
Spring Cloud Alibaba [Gateway]网关。
java·gateway·springcloud
醇氧2 天前
【spring 】Spring Cloud Gateway 的Filter学习
学习·spring·gateway
蚰蜒螟2 天前
Spring gateway 路由 配置在数据库
数据库·spring·gateway
因我你好久不见3 天前
解决绿盟漏洞扫描 gateway、nacos、springboot tomcat检测到目标主机可能存在缓慢的HTTP拒绝服务攻击问题
spring boot·http·gateway
moxiaoran57535 天前
搭建Spring gateway网关微服务
spring·微服务·gateway
飞天大拖把6 天前
Zuul和GateWay
gateway
.生产的驴8 天前
SpringCloud Gateway网关路由配置 接口统一 登录验证 权限校验 路由属性
java·spring boot·后端·spring·spring cloud·gateway·rabbitmq
菜菜-plus9 天前
分布式,微服务,SpringCloudAlibaba,nacos,gateway,openFeign
java·分布式·微服务·nacos·gateway·springcloud·openfeign
七月在野,八月在宇,九月在户9 天前
前端--> nginx-->gateway产生的跨域问题分析
前端·nginx·gateway