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的下载,限流和熔断
相关推荐
壹佰大多12 小时前
【spring-cloud-gateway总结】
java·spring·gateway
龙哥·三年风水14 小时前
workman服务端开发模式-应用开发-后端api推送修改二
分布式·gateway·php
龙哥·三年风水18 小时前
workman服务端开发模式-应用开发-后端api推送修改一
分布式·gateway·php
Hello Dam18 小时前
面向微服务的Spring Cloud Gateway的集成解决方案:用户登录认证与访问控制
spring cloud·微服务·云原生·架构·gateway·登录验证·单点登录
小笨猪-1 天前
统⼀服务⼊⼝-Gateway
java·spring cloud·微服务·gateway
bohu831 天前
通过gateway实现服务的平滑迁移
gateway·平滑·weight
岁月变迁呀1 天前
Spring Cloud Gateway 源码
java·spring·spring cloud·gateway
龙哥·三年风水2 天前
workman服务端开发模式-应用开发-后端api推送工具开发
分布式·gateway·php
龙哥·三年风水2 天前
workman服务端开发模式-应用开发-后端api登录之浏览器版本工具开发
分布式·gateway·php
龙哥·三年风水2 天前
workman服务端开发模式-GatewayWorker的使用
分布式·gateway·php