欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

搭建SpringCloudAlibaba开发环境之网关Gateway

程序员文章站 2022-05-31 18:37:40
...

搭建SpringCloudAlibaba开发环境之网关Gateway

搭建程序的统一入口(网关)

特别注意
spring cloud gateway 使用WebFlux作为服务器,不使用web作为
服务器,默认加入的WebFlux的依赖,所以在pom中切记不要添加
web依赖,也不要添加WebFlux相关依赖

在pom文件加入gateway依赖

<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">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>bygones-gateway</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.bygones</groupId>
        <artifactId>bygones-dependencies</artifactId>
        <version>1.0.0-SNAPSHOST</version>
        <relativePath>../bygones-dependencies/pom.xml</relativePath>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- nacos discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--open feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>
</project>

在application.yml中配置路由的规则和策略
策略:
设置gateway组件与服务注册发现组件结合,采用服务名作为路由
策略
spring.cloud.gateway.discovery.locator.enabled=true
规则:
spring.cloud.gateway.routes

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery: # 设置gateway组件与服务注册发现组件结合,采用服务名作为路由策略
        locator:
          enabled: true
      routes: # 为消费者配置路由规则,并支持负载均衡(id,uri,predicates 可以配置多组指向不同的消费者)
        - id: CONSUMER
          uri: lb://consumer # lb(load balance负载均衡)
          predicates:
            - Method=GET,POST
    sentinel:
      transport:
        dashboard: localhost:8090 # 将服务注册到sentinel dashboard中

server:
  port: 8080

gateway全局过滤器

实现GlobalFilter与Ordered接口即可

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.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthFilter implements GlobalFilter,Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("执行过滤器");
        return chain.filter(exchange);
    }

    /** 设置过滤器的执行顺序,数字越小,优先级越高 */
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE; // 最低优先级
    }
}