Spring Cloud Gateway快速入门Demo

1.什么是Spring Cloud Gateway?

Spring Cloud Gateway 是一个基于 Spring Framework 和 Spring Boot 构建的 API 网关服务。它提供了一种简单而有效的方式来路由请求、提供跨领域的关注点(如安全、监控/指标和弹性)以及其他功能。Spring Cloud Gateway 旨在提供一种简单而有效的方式来路由 API 请求,并提供一些常见的网关功能,如路径重写、负载均衡、限流、熔断等。

应用场景

  1. 请求路由:将客户端请求路由到不同的微服务。
  2. 安全性:在网关层实现身份验证和授权。
  3. 负载均衡:在多个服务实例之间分配请求。
  4. 限流:限制请求的速率以保护后端服务。
  5. 熔断和降级:在后端服务不可用时提供默认响应。
  6. 监控和日志:收集和分析请求数据。

2.环境准备

配置hosts

复制代码
127.0.0.1 node1
127.0.0.1 node2

进去目录

bash 复制代码
cd D:\IdeaProjects\springcloud-demo\eureka\eureka-server

执行命令

ini 复制代码
C:\Users\Dell\.jdks\jbr-17.0.9\bin\java.exe -jar target/eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=node1
C:\Users\Dell\.jdks\jbr-17.0.9\bin\java.exe -jar target/eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=node2

访问[http://127.0.0.1:8761/\[!\[euraka](https://link.juejin.cn?target=http%3A%2F%2F127.0.0.1%3A8761%2F%255B!%255Beuraka "http://127.0.0.1:8761/[![euraka")](p9-xtjj-sign.byteimg.com/tos-cn-i-73...)](www.liuhaihua.cn/wp-content/...)

3.代码工程

实验目标

  • 请求路由 :当用户访问 /api/users 时,Spring Cloud Gateway 会将请求路由到用户服务。当访问 /api/orders 时,路由到订单服务。

  • 负载均衡:如果订单服务有多个实例,Spring Cloud Gateway 可以根据负载均衡策略将请求分发到不同的实例上。

Gateway

pom.xml

xml 复制代码
<?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>spring-cloud-gateway</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

SpringCloudGatewayApplication.java

typescript 复制代码
package com.et;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudGatewayApplication.class, args);
    }
}

application.yml

yaml 复制代码
server:
  port: 8080
spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: user_service
          uri: http://localhost:8081
          predicates:
            - Path=/api/users/**
        - id: order_service
          uri: lb://order-service
          predicates:
            - Path=/api/orders/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

User

pom.xml

xml 复制代码
<?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>spring-cloud-gateway</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>users</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

controller

kotlin 复制代码
package com.et;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/api/users")
    public String getUsers() {
        return "User list";
    }
}

UserServiceApplication.java

typescript 复制代码
package com.et;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}

application.yml

yaml 复制代码
server:
  port: 8081

Order

pom.xml

xml 复制代码
<?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>spring-cloud-gateway</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

controller

kotlin 复制代码
package com.et;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @GetMapping("/api/orders")
    public String getOrders() {
        return "Order list";
    }
}

OrderServiceApplication

typescript 复制代码
package com.et;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
}

application.yml

yaml 复制代码
server:
  port: 0 #random port

spring:
  application:
    name: order-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

4.测试

  1. 启动用户服务,提供 /api/users 端点。
  2. 启动多个订单服务实例,提供 /api/orders 端点。
  3. 启动 Spring Cloud Gateway。

启动这两个服务后,通过 Spring Cloud Gateway 访问 /api/users/api/orders,请求将被路由到相应的服务。

5.引用

相关推荐
aramae29 分钟前
模拟实现strlen()函数 (C语言)
c语言·开发语言·后端
SL_staff1 小时前
别急着买商业规则引擎:90%风控场景根本不需要全量编码能力
java·程序员·groovy
ManageEngineITSM1 小时前
DevOps和ITIL是什么关系?是替代还是互补一文讲清
java·服务器·资产管理·变更管理
行百里er2 小时前
Spring Insight 里如何把 Span 收成一条链路
spring boot·spring cloud·监控
IT_陈寒2 小时前
Python的GIL把我坑惨了,多线程跑得比单线程还慢
前端·人工智能·后端
分支预测失败2 小时前
RISC-V 时间子系统深度专题:mtime 访问路径、SBI 定时器与 Linux tickless 协同
后端
65岁退休Coder2 小时前
PI Agent 开发一个生产级 Harness
后端·node.js·agent
SL_staff2 小时前
ERP排程总在纸上谈兵?JVS-APS如何用真实产能约束打通计划与执行闭环
java·人工智能·开源
xcl09252 小时前
24小时自助健身房系统软件开发实战指南:从架构到部署
java·spring boot
用户8356290780512 小时前
如何使用 Python 给 Word 文档添加水印
后端·python