1. 什么是网关服务?
网关服务(API Gateway)是微服务架构中的一个关键组件,它充当所有客户端请求的单一入口,负责请求的路由、聚合、协议转换、认证和授权等功能。通过网关服务,客户端可以简化与多个后端服务的交互,增强系统的可维护性和安全性。
2. 网关服务的主要功能
- 请求路由:将请求路由到适当的微服务。
- 负载均衡:分配请求到多个服务实例。
- 协议转换:在不同协议之间进行转换(如HTTP到HTTPS)。
- 安全管理:提供认证和授权功能。
- 请求聚合:将多个服务的响应聚合成一个响应。
- 限流和熔断:保护后端服务免受过载影响。
3. 配置解读
version: "3.7"
services:
gateway-service:
image: piomin/gateway-service:1.1-SNAPSHOT
ports:
- "8060:8060"
depends_on:
discovery-service:
condition: service_healthy
environment:
SPRING_PROFILES_ACTIVE: docker
links:
- config-service
- discovery-service
- employee-service
- department-service
- organization-service
- zipkin
4. 配置详解
image: piomin/gateway-service:1.1-SNAPSHOT:使用指定的镜像来运行网关服务。ports: ["8060:8060"]:将主机的8060端口映射到容器的8060端口。通过访问http://localhost:8060来与网关服务交互。depends_on:定义服务的依赖关系。discovery-service:网关服务依赖于服务发现组件,确保在启动网关服务之前,服务发现组件已经健康可用。
environment:配置环境变量。SPRING_PROFILES_ACTIVE: docker:设置Spring Boot的激活配置文件为docker。
links:使网关服务可以通过名称访问其他服务。- 列出所有需要访问的服务,包括config-service、discovery-service、employee-service、department-service、organization-service和zipkin。
5. 实践示例
Docker Compose 文件示例
version: "3.7"
services:
config-service:
image: piomin/config-service:1.1-SNAPSHOT
ports:
- "8088:8088"
healthcheck:
test: curl --fail http://localhost:8088/employee/docker || exit 1
interval: 5s
timeout: 2s
retries: 3
discovery-service:
image: piomin/discovery-service:1.1-SNAPSHOT
ports:
- "8061:8061"
depends_on:
config-service:
condition: service_healthy
links:
- config-service
healthcheck:
test: curl --fail http://localhost:8061/eureka/v2/apps || exit 1
interval: 4s
timeout: 2s
retries: 3
environment:
SPRING_PROFILES_ACTIVE: docker
employee-service:
image: piomin/employee-service:1.2-SNAPSHOT
ports:
- "8080"
depends_on:
discovery-service:
condition: service_healthy
links:
- config-service
- discovery-service
- zipkin
environment:
SPRING_PROFILES_ACTIVE: docker
department-service:
image: piomin/department-service:1.2-SNAPSHOT
ports:
- "8080"
depends_on:
discovery-service:
condition: service_healthy
links:
- config-service
- discovery-service
- employee-service
- zipkin
environment:
SPRING_PROFILES_ACTIVE: docker
organization-service:
image: piomin/organization-service:1.2-SNAPSHOT
ports:
- "8080"
depends_on:
discovery-service:
condition: service_healthy
links:
- config-service
- discovery-service
- employee-service
- department-service
- zipkin
environment:
SPRING_PROFILES_ACTIVE: docker
gateway-service:
image: piomin/gateway-service:1.1-SNAPSHOT
ports:
- "8060:8060"
depends_on:
discovery-service:
condition: service_healthy
environment:
SPRING_PROFILES_ACTIVE: docker
links:
- config-service
- discovery-service
- employee-service
- department-service
- organization-service
- zipkin
运行服务
- 启动所有服务:bash复制代码
docker-compose up -d - 验证服务健康状况:
- 检查各个服务是否健康运行,包括config-service、discovery-service、employee-service、department-service、organization-service和gateway-service。
- 使用浏览器或API客户端(如Postman)访问网关服务的端点,验证网关是否能正确路由请求到相应的服务。
- 访问网关服务:
- 打开浏览器,访问
http://localhost:8060,可以通过网关服务与后端微服务进行交互。
- 打开浏览器,访问
结论
通过理解网关服务的作用和配置,你可以更好地管理和路由请求,提升微服务架构的整体灵活性和安全性。网关服务作为请求的单一入口,提供了丰富的功能,如请求路由、负载均衡、安全管理和请求聚合,简化了客户端与后端服务的交互。配置正确的依赖关系和健康检查,可以确保各个服务的可靠性和可用性。
网关服务中的 Swagger
1. 什么是 Swagger?
Swagger 是一个用于生成、描述、调用和可视化 RESTful Web 服务的开源工具。它允许开发者用标准化的方式来编写 API 文档,并提供一个用户界面(Swagger UI)来交互和测试这些 API。
2. 为什么在网关服务中使用 Swagger?
在微服务架构中,各个服务可能都有自己的 Swagger 文档,但使用网关服务,可以集中管理和访问所有微服务的 API 文档。通过在网关服务中集成 Swagger,开发者可以从单一入口查看和测试所有微服务的 API。
3. 在网关服务中配置 Swagger
假设我们已经有一个 Spring Cloud Gateway 服务,并且希望在其中集成 Swagger。
3.1. 添加依赖
首先,在网关服务的 pom.xml 文件中添加 Swagger 相关的依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3.2. 配置 Swagger
在网关服务的配置类中(例如 SwaggerConfig.java),添加 Swagger 的配置:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.gateway"))
.paths(PathSelectors.any())
.build();
}
}
3.3. 在 application.yml 中配置网关路由
在 application.yml 文件中,为网关配置路由,使其可以访问各个微服务的 Swagger 文档:
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: employee-service
uri: http://employee-service:8080
predicates:
- Path=/employee/**, /v2/api-docs
- id: department-service
uri: http://department-service:8080
predicates:
- Path=/department/**, /v2/api-docs
- id: organization-service
uri: http://organization-service:8080
predicates:
- Path=/organization/**, /v2/api-docs
# 配置 Swagger Aggregation
swagger:
aggregation:
api-docs:
url-patterns:
- /employee/v2/api-docs
- /department/v2/api-docs
- /organization/v2/api-docs
routes:
employee-service:
path: /employee/**
url: http://employee-service:8080/v2/api-docs
department-service:
path: /department/**
url: http://department-service:8080/v2/api-docs
organization-service:
path: /organization/**
url: http://organization-service:8080/v2/api-docs
3.4. 聚合 Swagger 文档
在 SwaggerConfig 中,添加一个聚合的配置:
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@Configuration
public class SwaggerAggregationConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("employee-service", r -> r.path("/employee/**")
.uri("lb://employee-service"))
.route("department-service", r -> r.path("/department/**")
.uri("lb://department-service"))
.route("organization-service", r -> r.path("/organization/**")
.uri("lb://organization-service"))
.build();
}
}
3.5. 测试配置
启动网关服务后,打开浏览器并访问 http://localhost:8060/swagger-ui.html,你应该能够看到所有通过网关聚合的微服务的 API 文档。