【Spring实战】31 Spring Boot3 集成 Gateway 微服务网关

文章目录

1. 定义

Spring Cloud Gateway 是一个基于 Spring Framework 的开源网关服务,用于构建微服务架构中的 API 网关。它提供了一种灵活的方式来路由请求、过滤请求以及对请求进行各种操作,从而实现对微服务的集中控制、安全性、监控等功能。

2. 功能

Spring Cloud Gateway 提供了丰富的功能,包括但不限于:

  • 动态路由: 根据配置动态地将请求路由到不同的微服务实例
  • 过滤器: 实现对请求和响应的各种操作,例如认证、授权、请求转发、限流等
  • 集成负载均衡: 通过集成负载均衡器,将请求分发到多个服务实例,提高系统的性能和可用性
  • 断路器支持: 处理微服务中的故障和延迟,防止故障扩散
  • 统一认证和授权: 通过集成 Spring Security 等机制,实现对微服务的统一认证和授权管理
  • 监控和日志: 提供监控和日志功能,帮助理解网关的运行状况,分析请求流量

3. 示例代码

下面是一个简单的 Spring Boot 项目,演示如何集成 Spring Cloud Gateway。

1) 创建一个业务服务

首先,我们需要提前使用 Spring boot 创建一个普通的业务服务,并且创建一个 REST 接口,调用 /hello 返回一个 Hello world

pom.xml

xml 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

application.yml

yml 复制代码
server:
  port: 9501
  servlet:
    context-path: /account

访问 API - HelloController.java

java 复制代码
package com.cheney.koala.account.controller;

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

@RestController
public class HelloController {

    @GetMapping("hello")
    public String hello() {
        return "Hello world";
    }
}

启动类 - KoalaAccountApplication.java

java 复制代码
package com.cheney.koala.account;

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

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

2)创建一个网关服务

然后,我们需要再使用 Spring boot 创建一个网关服务,并且配置一下路由转发

pom.xml

xml 复制代码
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2022.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>

application.yml

yml 复制代码
server:
  port: 9500

spring:
  cloud:
    gateway:
      routes:
        - id: account
          uri: http://127.0.0.1:9501
          predicates:
            - Path=/account/**

启动类 - KoalaGatewayApplication.java

java 复制代码
package com.cheney.koala.gateway;

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

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

这个简单的示例配置了一个路由,将以 "/account/**" 开头的请求转发到 "http://127.0.0.1:9501"。

3)启动服务

分别启动两个服务(业务服务和网关服务)

启动业务服务

启动网关服务

4)验证

首先,我们先直接访问业务服务,看一下效果

http://localhost:9501/account/hello

然后,我们再通过网关服务访问,看一下效果

http://localhost:9500/account/hello

通过这个简单的示例,你可以快速了解 Spring Cloud Gateway 的基本用法,以及如何配置和运行一个最最基本的网关服务。

4. 代码参考

https://gitee.com/cheney09/koala-system

结语

Spring Cloud Gateway 提供了一个强大而灵活的工具,用于构建微服务架构中的 API 网关。通过合理配置,你可以实现路由、过滤、负载均衡等功能,为微服务架构提供了更好的可维护性和可扩展性。在实际项目中,可以根据具体需求进一步定制和优化配置,以满足项目的特定要求。希望这篇博客能够帮助你入门 Spring Cloud Gateway。

相关推荐
考虑考虑13 小时前
Jpa使用union all
java·spring boot·后端
咖啡Beans21 小时前
SpringCloud网关Gateway功能实现
java·spring cloud
阿杆1 天前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
昵称为空C2 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu2 天前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
计算机毕业设计木哥2 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
青衫客362 天前
Spring异步编程- 浅谈 Reactor 核心操作符
java·spring·响应式编程
熙客2 天前
SpringCloud概述
java·spring cloud·微服务