13.4Gateway项目搭建

分类: Gateway API网关

Gateway 项目搭建

本节将学习如何搭建 Gateway 项目。本节将学习 Gateway 项目搭建。

本节将学习:项目创建、依赖添加、配置文件,以及启动类编写。

在商城项目中创建 Gateway 服务

步骤1:创建 Gateway 服务模块

文件路径: mall-microservices/gateway-service/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>com.mall</groupId> <artifactId>mall-microservices</artifactId> <version>1.0.0</version> </parent> <artifactId>gateway-service</artifactId> <name>Gateway Service</name> <description>API网关服务</description> <dependencies> <!-- Spring Cloud Gateway --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Nacos 服务发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- LoadBalancer(Gateway 需要) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <!-- Spring Boot Actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

注意:Gateway 不能使用 spring-boot-starter-web,因为 Gateway 基于 WebFlux(响应式),而不是 Spring MVC。

依赖添加

Maven 依赖

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>

步骤2:配置 Gateway

Gateway 配置文件

文件路径: mall-microservices/gateway-service/src/main/resources/application.yml

server: port: 8080 spring: application: name: gateway-service cloud: nacos: discovery: server-addr: localhost:8848 namespace: public group: DEFAULT_GROUP gateway: discovery: locator: enabled: true # 启用服务发现路由 lower-case-service-id: true # 服务名转小写 routes: # 用户服务路由 - id: user-service uri: lb://user-service predicates: - Path=/api/users/** filters: - StripPrefix=1 # 去掉 /api 前缀 # 商品服务路由 - id: product-service uri: lb://product-service predicates: - Path=/api/products/** filters: - StripPrefix=1 # 订单服务路由 - id: order-service uri: lb://order-service predicates: - Path=/api/orders/** filters: - StripPrefix=1 # 支付服务路由 - id: payment-service uri: lb://payment-service predicates: - Path=/api/payments/** filters: - StripPrefix=1 # 库存服务路由 - id: inventory-service uri: lb://inventory-service predicates: - Path=/api/inventory/** filters: - StripPrefix=1 # Actuator 配置 management: endpoints: web: exposure: include: health,info,metrics,gateway endpoint: health: show-details: always

配置说明

Gateway 配置项说明:

  • discovery.locator.enabled:启用服务发现路由,可以自动发现服务
  • lower-case-service-id:服务名转小写,避免大小写问题
  • uri: lb://service-name:使用 LoadBalancer 进行负载均衡
  • predicates:路由断言,匹配请求路径
  • filters:路由过滤器,可以修改请求和响应

步骤3:编写启动类

Gateway 启动类

文件路径: mall-microservices/gateway-service/src/main/java/com/mall/gateway/GatewayServiceApplication.java

package com.mall.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class GatewayServiceApplication { public static void main(String[] args) { SpringApplication.run(GatewayServiceApplication.class, args); } }

注意:Gateway 使用 WebFlux,不需要 @EnableWebMvc 注解。

步骤4:验证 Gateway 启动

启动 Gateway

cd mall-microservices/gateway-service mvn spring-boot:run

验证路由

测试通过 Gateway 访问服务:

# 通过 Gateway 访问用户服务 curl http://localhost:8080/api/users/1 # 通过 Gateway 访问商品服务 curl http://localhost:8080/api/products/1 # 通过 Gateway 访问订单服务 curl http://localhost:8080/api/orders/1

验证服务发现路由:

Gateway 启用了服务发现路由后,可以通过服务名直接访问:

# 访问格式:http://gateway:port/service-name/path curl http://localhost:8080/user-service/api/users/1

官方资源

本节小结

在本节中,我们学习了:

第一个是项目创建。 如何创建 Gateway 项目。

第二个是依赖添加。 添加 Gateway 相关依赖。

第三个是配置文件。 配置 Gateway 路由。

第四个是启动类编写。 编写启动类。

这就是 Gateway 项目搭建。完成搭建后,我们就可以使用 Gateway 了。

在下一节,我们将学习路由配置。