服务网关Gateway_入门案例

本文介绍了如何在SpringCloud中创建一个名为cloud-gateway-gateway9527的工程,包括添加依赖、配置application.yml、定义路由规则以及主启动类的实现。通过EurekaClient实现服务发现,展示了一个简单的HTTP路径匹配路由示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建cloud-gateway-gateway9527工程

image-20220215101632986

pom文件引入依赖

<dependencies>
    <!--  引入网关Gateway依赖   -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
    </dependency>
  </dependencies>

新增application.yml


server:
  port: 9527

spring:
  cloud:
    gateway:
      routes:
        # 路由ID,没有固定规则但要求唯一,建议配合服务名
        - id: cloud-order-openfeign-consumer
        # 匹配后提供服务的路由地址
          uri: http://localhost:80
        # 断言3
        # http://localhost:9527/order/index
          predicates:
            # 路径相匹配的进行路由
            - Path=/order/*

主启动类

@Slf4j
@EnableEurekaClient
@SpringBootApplication
public class GatewayMain {
  public static void main(String[] args) {
    SpringApplication.run(GatewayMain.class,args);
    log.info("********** GatewayMain 服务启动成功 *********");
   }
}

测试

  • 启动注册中心 7001,7002
  • 启动服务提供者80
  • 启动网关服务9527

请求localhost:9527/order/index

### Spring Cloud Gateway 入门教程与示例代码 Spring Cloud Gateway 是一个基于 Spring 5.0、Spring Boot 2.0 和 Project Reactor 技术栈开发的 API 网关工具,主要用于微服务架构中的路由管理和请求过滤等功能[^2]。 #### 创建 Spring Cloud Gateway 应用程序 以下是创建并运行一个简单的 Spring Cloud Gateway 示例的具体方法: --- #### 1. 添加 Maven 依赖项 在项目的 `pom.xml` 文件中引入 `spring-cloud-starter-gateway` 依赖项: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ``` 此依赖会自动导入所需的组件来支持 Spring Cloud Gateway 功能[^5]。 --- #### 2. 配置 application.yml 或 application.properties 通过 YAML 文件配置网关的基本参数以及路由规则。以下是一个基本的 YML 配置示例: ```yaml server: port: 8080 spring: cloud: gateway: routes: - id: example_route uri: http://localhost:8701/example-service predicates: - Path=/example/** filters: - AddRequestHeader=MyCustomHeader, MyValue ``` 上述配置说明如下: - **id**: 定义该路由的唯一标识符。 - **uri**: 指定目标服务的实际地址。 - **predicates**: 设置匹配条件,此处表示当路径以 `/example/` 开头时触发该路由[^3]。 - **filters**: 可选字段,用于自定义请求或响应的行为,比如添加 HTTP 请求头部信息。 --- #### 3. 编写启动类 编写主应用程序入口类,并启用服务发现功能(如果需要)。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient // 如果要集成 Eureka 注册中心,则需开启此项 public class SpringCloudGatewayExampleApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudGatewayExampleApplication.class, args); } } ``` 这里使用了 `@EnableDiscoveryClient` 注解以便让网关能够自动注册到服务中心(如 Eureka),从而实现动态路由的功能[^4]。 --- #### 运行测试 完成以上步骤后,启动应用并通过浏览器访问指定 URL 来验证是否成功转发至对应的服务端点。假设本地存在名为 `example-service` 的服务监听于端口 `8701` 上,则可以通过访问 `http://localhost:8080/example/somePath` 测试其效果。 --- ### 总结 本实例展示了如何利用 Spring Cloud Gateway 构建基础版 API 网关的过程,涵盖了必要的 POM 配置、YAML 参数设定及 Java 启动脚本等内容。此外还提及了一些高级特性,像 Predicate 表达式和 Filter 使用场景等知识点也有所涉及。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CuteTTU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值