API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · 课时

搭建基础网关项目

使用 Spring Boot 和 Spring Cloud Gateway 初始化项目,并配置一个可正常运行的简单网关。

第 2 / 4 课11 个步骤

搭建基础网关项目 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Gateway Project Setup

Welcome! In this lesson, you'll learn how to initialize a basic Spring Cloud Gateway project. This is your first step to building powerful API gateways.

We'll cover setting up the necessary dependencies and creating a minimal working gateway configuration.

What You'll Need

Before we dive in, ensure you have the following tools installed:

  • Java Development Kit (JDK) 17+: For running Spring Boot applications.
  • Maven or Gradle: Build automation tools for managing project dependencies.
  • An IDE (e.g., IntelliJ IDEA, VS Code): For writing and managing your code.

These are standard for Spring Boot development.

Start with Spring Initializr

The easiest way to start any Spring Boot project is with Spring Initializr. It helps you generate a project with all the necessary setup.

For our gateway, you'll select specific dependencies to get started quickly.

Key Gateway Dependencies

When using Spring Initializr, search for and add these key dependencies:

  • Spring Cloud Gateway: This is the core library that provides gateway functionality.
  • Spring WebFlux: Gateway is built on reactive programming, so WebFlux is a transitive dependency and crucial for its operation.

Choose your preferred build tool (Maven or Gradle) and Java version (e.g., 17 or 21).

The Main Application Class

After generating and importing your project, you'll find a main application class. This class uses @SpringBootApplication to bootstrap your application.

Run this simple Spring Boot application to see it start up. It won't do much yet, but it's alive!

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

@SpringBootApplication
public class GatewayProjectApplication {

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

Configuring Gateway Routes

Now that our application can run, we need to tell the gateway what to do! This is done by defining routes in your application.yml (or application.properties) file.

A route maps incoming requests to a specific backend service based on certain conditions.

Basic Route Example

Let's add a simple route to src/main/resources/application.yml. This route will forward requests from /get to http://httpbin.org:80, a public testing service.

This is your first step to proxying requests!

spring:
  cloud:
    gateway:
      routes:
        - id: httpbin_route
          uri: http://httpbin.org:80
          predicates:
            - Path=/get

Understanding the Route

Let's break down that route configuration:

  • id: httpbin_route: A unique identifier for this route.
  • uri: http://httpbin.org:80: The target backend service URL where requests will be forwarded.
  • predicates: - Path=/get: A condition that must be met for this route to activate. Here, it means any request with a path starting with /get.

Predicates are like 'if' statements for routing.

Running & Testing Your Gateway

With the application.yml updated, restart your Spring Boot application. The gateway will now pick up the new route.

You can test it using curl or your web browser:

  • Open your terminal.
  • Run: curl http://localhost:8080/get

You should see a JSON response from httpbin.org, proving your gateway is working!

Gateway Setup Check

You've just set up your first Spring Cloud Gateway project!

Which of the following is the primary dependency required to enable Spring Cloud Gateway functionality in a Spring Boot project?

Recap: Basic Gateway Setup

Great job! You've learned how to:

  • Initialize a Spring Boot project using Spring Initializr.
  • Add the essential spring-cloud-starter-gateway dependency.
  • Create a basic Spring Cloud Gateway application class.
  • Configure your first gateway route in application.yml.
  • Test your gateway to ensure it's forwarding requests correctly.

This foundational knowledge will help you build more complex gateway configurations in future lessons!

免费开始

用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「搭建基础网关项目」课时是免费的吗?

是的 — 「搭建基础网关项目」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

「搭建基础网关项目」这节课中我会学到什么?

使用 Spring Boot 和 Spring Cloud Gateway 初始化项目,并配置一个可正常运行的简单网关。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「搭建基础网关项目」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?

能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 网关与传统微服务
  2. 搭建基础网关项目
  3. 定义路由与谓词
  4. 理解响应式基础
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)