1. 概述

本文基于SpringBoot4.0.6、JDK21、SpringCloud 2025.1.1,实现Gateway网关整合Eureka注册中心,摒弃硬写IP端口方式,通过 服务名+lb负载均衡 完成动态路由转发,实现微服务统一入口管理。

2. 创建项目

3. 核心代码

  • SpringBoot:4.0.6
  • JDK:21

3.1. 依赖管理(pom.xml )

3.1.1. 网关依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-server-webflux</artifactId>
        </dependency>

3.1.2. Eureka客户端依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

3.1.3. 依赖版本

    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2025.1.1</spring-cloud.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.2. 路由配置和Eureka配置(application.yml)

配置说明:lb:// 为SpringCloud内置负载均衡协议,自动从Eureka拉取服务实例列表,实现轮询负载均衡,无需手动填写服务IP与端口。

spring:
  cloud:
    gateway:
      server:
        webflux:
          routes:
            - id: user-route  # 路由唯一标识
              uri: lb://hello-user-service # lb=负载均衡,后面跟Eureka注册的服务名
              predicates:
                - Path=/users  # 断言:精确匹配 /users 路径的请求

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # Eureka Server的地址

4. 启动网关服务


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v4.0.6)

2026-05-12T22:55:35.817+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] e.h.HelloGatewayWebfluxEurekaApplication : Starting HelloGatewayWebfluxEurekaApplication using Java 21.0.1 with PID 10340 (E:\hello-world\hello-gateway-webflux-eureka\target\classes started by SongGuanxun in E:\hello-world\hello-gateway-webflux-eureka)
2026-05-12T22:55:35.827+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] e.h.HelloGatewayWebfluxEurekaApplication : No active profile set, falling back to 1 default profile: "default"
2026-05-12T22:55:38.219+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d9e1a744-e11d-31b9-97f8-dc0d0ff040ca
2026-05-12T22:55:40.974+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [After]
2026-05-12T22:55:40.975+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Before]
2026-05-12T22:55:40.975+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Between]
2026-05-12T22:55:40.975+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Cookie]
2026-05-12T22:55:40.975+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Header]
2026-05-12T22:55:40.975+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Host]
2026-05-12T22:55:40.975+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Method]
2026-05-12T22:55:40.976+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Path]
2026-05-12T22:55:40.976+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Query]
2026-05-12T22:55:40.976+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [ReadBody]
2026-05-12T22:55:40.976+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [RemoteAddr]
2026-05-12T22:55:40.976+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Version]
2026-05-12T22:55:40.977+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [XForwardedRemoteAddr]
2026-05-12T22:55:40.977+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Weight]
2026-05-12T22:55:40.977+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2026-05-12T22:55:41.554+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestClient.
2026-05-12T22:55:41.643+08:00  WARN 10340 --- [hello-gateway-webflux-eureka] [           main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2026-05-12T22:55:41.726+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2026-05-12T22:55:41.786+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2026-05-12T22:55:41.795+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2026-05-12T22:55:41.818+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2026-05-12T22:55:41.819+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2026-05-12T22:55:41.819+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2026-05-12T22:55:41.819+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2026-05-12T22:55:41.820+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2026-05-12T22:55:41.820+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2026-05-12T22:55:41.820+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2026-05-12T22:55:42.740+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : The response status is 200
2026-05-12T22:55:42.744+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2026-05-12T22:55:42.749+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2026-05-12T22:55:42.753+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1778597742751 with initial instances count: 1
2026-05-12T22:55:42.760+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application HELLO-GATEWAY-WEBFLUX-EUREKA with eureka with status UP
2026-05-12T22:55:42.761+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1778597742761, current=UP, previous=STARTING]
2026-05-12T22:55:42.766+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [foReplicator-%d] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_HELLO-GATEWAY-WEBFLUX-EUREKA/songguanxun-HP:hello-gateway-webflux-eureka:9090: registering service...
2026-05-12T22:55:42.881+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [foReplicator-%d] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_HELLO-GATEWAY-WEBFLUX-EUREKA/songguanxun-HP:hello-gateway-webflux-eureka:9090 - registration status: 204
2026-05-12T22:55:43.433+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] o.s.boot.reactor.netty.NettyWebServer    : Netty started on port 9090 (http)
2026-05-12T22:55:43.435+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 9090
2026-05-12T22:55:43.489+08:00  INFO 10340 --- [hello-gateway-webflux-eureka] [           main] e.h.HelloGatewayWebfluxEurekaApplication : Started HelloGatewayWebfluxEurekaApplication in 9.171 seconds (process running for 11.183)

5. 功能验证:测试路由转发效果

测试说明:请求网关地址+断言路径,网关自动转发至注册中心内对应微服务,完成接口统一代理。

5.1. Eureka控制台

5.2. 通过网关访问后端服务

6. 完整项目源码

6.1. pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.0.6</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>hello-gateway-webflux-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2025.1.1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-server-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

6.2. application.yml

spring:
  application:
    name: hello-gateway-webflux-eureka
  cloud:
    gateway:
      server:
        webflux:
          routes:
            - id: user-route  # 路由唯一标识
              uri: lb://hello-user-service # lb=负载均衡,后面跟Eureka注册的服务名
              predicates:
                - Path=/users  # 断言:精确匹配 /users 路径的请求

server:
  port: 9090

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # Eureka Server的地址

6.3. HelloGatewayWebfluxEurekaApplication

package com.example.hello_gateway_webflux_eureka;

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

@SpringBootApplication
public class HelloGatewayWebfluxEurekaApplication {

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

}

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐