【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。

相关推荐
摇滚侠12 分钟前
Spring Boot 3零基础教程,新特性 ProblemDetails,笔记50
spring boot·笔记
朝新_2 小时前
【SpringBoot】详解Maven的操作与配置
java·spring boot·笔记·后端·spring·maven·javaee
洛克大航海4 小时前
7-SpringCloud-服务网关 Gateway-高级特性 Route
java·spring cloud·gateway·route
程序定小飞5 小时前
基于springboot的电影评论网站系统设计与实现
java·spring boot·后端
苹果醋35 小时前
JAVA面试汇总(二)多线程(五)
运维·vue.js·spring boot·nginx·课程设计
兜兜风d'6 小时前
RabbitMQ 持久性详解
spring boot·分布式·rabbitmq·1024程序员节
问道飞鱼7 小时前
【微服务组件】Springboot结合Dubbo实现RPC调用
spring boot·微服务·rpc·dubbo
I'm Jie8 小时前
(二)Gradle 依赖仓库及安全凭证配置
java·spring boot·spring·gradle·maven
李少兄9 小时前
记一次 Spring Boot 项目中 Redis 工具类的重构实践
spring boot·redis·重构
摇滚侠10 小时前
Spring Boot3零基础教程,生命周期启动加载机制,笔记64
spring boot·笔记