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的下载,限流和熔断
相关推荐
柳叶寒1 天前
医院信息化与智能化系统(17)
java·nacos·gateway·全栈·项目
cyt涛2 天前
SpringCloudGateway — 网关路由
java·开发语言·网关·gateway·路由·断言·转发
代码魔法师Sunny4 天前
502 Bad Gateway 错误详解:从表现推测原因,逐步排查直至解决
gateway
RedCong5 天前
通过route访问Openshift上的HTTP request报错504 Gateway Time-out【已解决】
http·gateway·openshift
飞升不如收破烂~5 天前
包括 Nginx、Gateway、Nacos、Dubbo、Sentinel、RocketMQ 和 Seata 的调用链路描述:
nginx·gateway·dubbo
还是转转5 天前
Kong Gateway 指南
gateway·kong
攒了一袋星辰11 天前
微服务网关gateway过滤器工厂与自定义过滤器
微服务·架构·gateway
猫猫不是喵喵.11 天前
Gateway 统一网关
java·网络·微服务·gateway
12 天前
504 Gateway Time-outopenresty
gateway
普通人zzz~12 天前
SpringCloud Gateway路由核心原理解析
java·spring cloud·gateway