【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

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

application.yml

server:
  port: 9501
  servlet:
    context-path: /account

访问 API - HelloController.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

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

            <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

server:
  port: 9500

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

启动类 - KoalaGatewayApplication.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。

  • 28
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值