快人一步
VMware 提供培训和认证,助力您快速进步。
了解更多如今,应用程序被构建为小型独立上游服务的集合。这加快了开发速度,并允许模块专注于特定职责,从而提高其质量。这是使用微服务方法的主要优势之一。然而,从一个服务跳转到另一个服务可能会增加额外的延迟,而当服务没有响应时,这种延迟可能会急剧增加。
如果您运行微服务,您会希望防止您的上游服务在无法正常工作时被调用。即使使用断路器模式也可能在响应时间上产生额外的开销。因此,有时最好主动检查您的上游服务,以验证它们在需要之前是否已准备就绪。
健康检查是一种根据服务状态确定其是否能正确响应的方法,可以防止超时和错误。
被动健康检查是在请求处理过程中进行的。如果服务最终不健康,应用程序将返回失败,并将该端点标记为不健康。这会增加额外的延迟。
主动健康检查会在收到请求之前在后台检查并移除不健康的服务。它不会增加额外的延迟。
最后但同样重要的是,这些特性可以与断路器库结合使用,以便在不遭受第一次失败惩罚的情况下立即回退到备用端点。
目标是通过负载均衡策略将请求转发到健康的的上游服务。
本文分为两部分
Spring 中有一些特性可以帮助您实现主动健康检查。
Spring Cloud Gateway Spring Cloud Gateway 提供了一个基于 Spring 和 Java 构建 API 网关的库。它通过 LoadBalancerClientFilter/ReactiveLoadBalancerClientFilter 全局过滤器支持上述特性。在本文中,您可以看到使用这些全局过滤器的不同方法。
首先,让我们探讨其中的一些特性。
Spring Cloud 中包含一个用于负载均衡的全局过滤器,可以通过使用特殊的 URI 符号激活:lb://your-service-name
。
spring:
cloud:
gateway:
routes:
- id: myRoute
uri: lb://your-service-name
predicates:
- Path=/service/**
负载均衡器过滤器 ReactiveLoadBalancerClientFilter(用于响应式应用程序)将检测 URI 并将其替换为与 "your-service-name" 关联的可用端点。
请注意,您需要在服务发现注册中心注册 "your-service-name"。我们将在后面的章节中看到不同的实现方法。
默认情况下,即使上游服务不健康,流量也会被路由到它们。为了避免选择不健康的服务,您可以启用 Spring Cloud 负载均衡器客户端提供的 health-check
配置。
spring:
cloud:
loadbalancer:
configurations: health-check
所有端点将通过自动使用 Spring Boot Actuator 健康端点进行定期检查。您还可以自定义一些选项,例如 spring.cloud.loadbalancer.health-check.<your-service-name>.path
和 spring.cloud.loadbalancer.health-check.interval
。
默认的健康检查配置使用
/actuator/health
端点检查上游服务端点,这需要在您的上游服务中激活 Spring Actuator。
有关更多选项,请查阅 LoadBalancerClientsProperties 和 LoadBalancerProperties 类。
Spring Cloud Gateway 中有一个内置功能,它将把所有可用服务部署为路由。本文描述的是相反的情况,我们声明的是包含主动健康检查的负载均衡路由。
在上一节中,您指定了一个负载均衡的 URI (lb://your-service-name
),但现在您需要注册与该 URI 的服务名称关联的端点。我们将在以下章节中探讨一些方法。
您可以通过配置 spring.cloud.discovery.client.simple.instances
属性来静态激活客户端负载均衡。它是一个 map,其中键是服务名称(由 lb:// URI 使用),值是指向上游服务的 org.springframework.cloud.client.ServiceInstance
对象数组。
静态负载均衡的一些好处包括
问题在于您在配置中静态设置了上游服务。如果您需要更改列表,则需要重新启动应用程序。
示例
spring:
cloud:
gateway:
routes:
- uri: lb://hello-service # Load Balancer URI handled by ReactiveLoadBalancerClientFilter
predicates:
- Path=/hello
loadbalancer:
configurations: health-check # Required for enabling SDC with health checks
discovery:
client:
simple: # SimpleDiscoveryClient to configure statically services
instances:
hello-service:
- secure: false
port: 8090
host: localhost
serviceId: hello-service
instanceId: hello-service-1
- secure: false
port: 8091
host: localhost
serviceId: hello-service
instanceId: hello-service-2
# Run server 1
SERVER_PORT=8090 ./gradlew :service:bootRun
# Run server 2
SERVER_PORT=8091 ./gradlew :service:bootRun
curl http://localhost:8090/actuator/health
{"status":"UP"}
curl localhost:8090/hello
{ "message": "hello world!"}%
./gradlew :1-service-disc-by-properties:bootRun
curl localhost:8881/hello
{ "message": "hello world from port 8090!"}%
curl localhost:8881/hello
{ "message": "hello world from port 8091!"}%
您可能需要多次运行前面的命令才能从不同的服务器获得响应。
curl localhost:8090/status/false -X PUT
curl http://localhost:8090/actuator/health
{"status":"DOWN"}
您可能会收到来自端口 8090 的响应,这是因为在您发送请求时健康检查尚未检查该端点。间隔可以在 spring.cloud.loadbalancer.health-check.interval
属性中修改。
此外,您可能会看到一些消息描述其中一个上游端点不健康,因此不可用。
2023-05-08 14:59:53.151 DEBUG 9906 --- [ctor-http-nio-3] r.n.http.client.HttpClientOperations : [12d42e83-77, L:/127.0.0.1:57439 - R:localhost/127.0.0.1:8090] Received response (auto-read:false) : RESPONSE(decodeResult: success, version: HTTP/1.1)
HTTP/1.1 503 Service Unavailable
curl localhost:8881/hello
{ "message": "hello world from port 8091!"}%
curl localhost:8091/status/false -X PUT
curl localhost:8881/hello
{"timestamp":"2023-05-08T13:07:48.704+00:00","path":"/hello","status":503,"error":"Service Unavailable","requestId":"6b5d6010-199"}%
静态配置不够灵活,但使用 Eureka 作为服务发现可以弥补这一缺点。
代价是您的架构中需要一个新组件,这可能会增加您的维护负担。对于某些客户端来说,这可能不是一个可行的选择。
以下示例配置了 Eureka 集成
spring:
application:
name: scg-client-with-eureka
cloud:
loadbalancer:
configurations: health-check # Note: required for enabling SDC with health checks - remove this line if you want to reproduce issues because not using health checks in LB
# Note: LoadBalancerCacheProperties.ttl (or spring.cloud.loadbalancer.cache.ttl) is 35 by default - You will need to wait 35secs after an instance turns healthy
gateway:
httpclient:
wiretap: true
routes:
- uri: lb://hello-service
predicates:
- Path=/headers
filters:
- StripPrefix=0
eureka:
client:
webclient:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka
fetchRegistry: true
registerWithEureka: false
instance:
preferIpAddress: true
./gradlew :eureka-server:bootRun
等待直到您看到 Eureka Server 已启动
2023-06-26 12:51:46.901 INFO 88601 --- [ Thread-9] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
# Run server 1
SPRING_PROFILES_ACTIVE=eureka SERVER_PORT=8090 ./gradlew :service:bootRun
# Run server 2
SPRING_PROFILES_ACTIVE=eureka SERVER_PORT=8091 ./gradlew :service:bootRun
您应该在步骤 1 的服务器日志中看到服务器实例已添加到 Eureka。
2023-06-26 12:52:50.805 INFO 88601 --- [nio-8761-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-SERVICE/192.168.0.14:hello-service:8090 with status UP (replication=true)
2023-06-26 12:53:29.127 INFO 88601 --- [nio-8761-exec-9] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-SERVICE/192.168.0.14:hello-service:8091 with status UP (replication=true)
访问 http://localhost:8761/ 并检查服务器是否已作为 hello-service
应用程序的实例包含在内
运行 Spring Cloud Gateway
SERVER_PORT=8883 ./gradlew :3-eureka-service-disc:bootRun
5. 测试 Spring Cloud Gateway 均衡器
curl localhost:8883/hello
{ "message": "hello world from port 8090!"}%
curl localhost:8883/hello
{ "message": "hello world from port 8091!"}%
curl localhost:8090/status/false -X PUT
您应该在 Eureka Dashboard 中看到只有一个可用实例,并且会看到一些日志消息抱怨端口 8090 上的服务不可用。健康检查不是立即的,因此您可能需要等待几秒钟才能看到实例被标记为 DOWN。
如您所见,Spring Cloud Gateway 提供了创建自定义过滤器的选项。它还允许您在不重新启动网关的情况下应用过滤器和更改路由。
在本节中,您将看到一个自定义过滤器实现,该实现使用 Spring Cloud Gateway 路由配置来设置服务的负载均衡和健康检查。
如果您的项目中已经有服务发现服务器,这可能不是您的最佳选择。如果还没有,这是一个简单且廉价的方式将这两个重要特性集成到您的项目中。
spring:
application:
name: custom-service-disc
cloud:
loadbalancer:
configurations: health-check # Note: required for enabling SDC with health checks - remove this line if you want to reproduce issues because not using health checks in LB
# Note: LoadBalancerCacheProperties.ttl (or spring.cloud.loadbalancer.cache.ttl) is 35 by default - You will need to wait 35secs after an instance turns healthy
gateway:
routes:
- uri: lb://hello-service
id: load-balanced
predicates:
- Path=/load-balanced/**
filters:
- StripPrefix=1
- LoadBalancer=localhost:8090;localhost:8091;localhost:8092
新的 LoadBalancer
路由过滤器允许您配置与 lb://hello-service
负载均衡 URI 相关的上游服务端点。
@Component
public class LoadBalancerGatewayFilterFactory extends AbstractGatewayFilterFactory<LoadBalancerGatewayFilterFactory.MyConfiguration> {
// ...
@Override
public GatewayFilter apply(MyConfiguration config) {
return (exchange, chain) -> {
final Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
if (StringUtils.hasText(config.getInstances()) && route.getUri().getScheme().equals("lb")) {
config.getServiceInstances(route.getUri().getHost()).forEach(discoveryClient::addInstance);
}
return chain.filter(exchange);
};
}
如果路由匹配 lb://<service-host>
模式,LoadBalancerGatewayFilterFactory
会将来自过滤器配置的所有上游服务端点与 service-host
关联。
在内部,我们包含了一个新的 ReactiveCustomDiscoveryClient
服务发现客户端实现,用于在代码中管理上游服务端点。Spring 会检测到这个 bean 并在用于确定可用端点的 DiscoveryClient 列表中优先使用它。
# Run server 1
SERVER_PORT=8090 ./gradlew :service:bootRun
# Run server 2
SERVER_PORT=8091 ./gradlew :service:bootRun
curl http://localhost:8090/actuator/health
{"status":"UP"}
curl localhost:8090/hello
{ "message": "hello world!"}%
SERVER_PORT=8882 ./gradlew :2-custom-service-disc:bootRun
curl localhost:8882/hello
{ "message": "hello world from port 8090!"}%
curl localhost:8882/hello
{ "message": "hello world from port 8091!"}%
您可能需要多次运行前面的命令才能从不同的服务器获得响应。
curl localhost:8090/status/false -X PUT
curl http://localhost:8090/actuator/health
{"status":"DOWN"}
您可能会收到来自端口 8090
的响应,这是因为在您发送请求时健康检查尚未检查该端点。间隔可以在 spring.cloud.loadbalancer.health-check.interval
属性中修改。
此外,您可能会看到一些消息描述其中一个上游端点不健康,因此不可用。
2023-05-08 15:59:53.151 DEBUG 9906 --- [ctor-http-nio-2] r.n.http.client.HttpClientOperations : [12d42e83-77, L:/127.0.0.1:57439 - R:localhost/127.0.0.1:8090] Received response (auto-read:false) : RESPONSE(decodeResult: success, version: HTTP/1.1)
HTTP/1.1 503 Service Unavailable
curl localhost:8882/hello
{ "message": "hello world from port 8091!"}%
curl localhost:8091/status/false -X PUT
curl localhost:8882/hello
{"timestamp":"2023-05-08T14:07:48.704+00:00","path":"/hello","status":503,"error":"Service Unavailable","requestId":"6b5d6010-199"}%
在本文中,您看到了在项目中实现负载均衡和主动健康检查的多种方法。
总而言之,您还看到如果您的架构不需要额外添加组件,那么 Spring Cloud Gateway 方法是一个不错的选择。
想了解更多关于 Spring Cloud 的信息吗?欢迎加入我们的 Spring Academy 虚拟课程!
只需在路由中添加一个属性即可实现主动健康检查,而无需亲自动手?请查看我们支持 Kubernetes 的商业平台。