织梦网站百度推送加哪,网站对不同分辨率,百度的网址,迷失传奇网站naocq文章目录 Resilience4j概述Resilience4j官方地址Resilience4j-RateLimiter微服务演示Payment processorPOM配置文件ServiceController Payment servicePOMModelServiceRestConfigController配置验证 探究 Rate Limiting请求三次 #xff0c;观察等待15秒连续访问6次 Resilienc… 文章目录 Resilience4j概述Resilience4j官方地址Resilience4j-RateLimiter微服务演示Payment processorPOM配置文件ServiceController Payment servicePOMModelServiceRestConfigController配置验证 探究 Rate Limiting请求三次 观察等待15秒连续访问6次 Resilience4j概述
Resilience4J 是一个针对 Java 8 应用程序的轻量级容错和弹性库。它设计用于在分布式系统中的服务之间提供弹性和容错性。Resilience4J 的名字来源于它提供的核心功能即让系统服务能够“弹性”resilient地应对各种失败情况包括网络问题、第三方服务故障等。
Resilience4J 提供了以下功能
断路器Circuit Breaker当检测到服务异常或超时断路器会打开阻止进一步的请求发送到该服务。一段时间后通常是秒级断路器会进入半开状态允许一个测试请求通过以检查服务是否恢复。如果请求成功断路器关闭如果失败断路器会再次打开。限流Rate Limiter限制进入系统的请求速率防止系统过载。这可以通过令牌桶算法或滑动窗口算法实现。隔离Isolation通过信号量或线程池隔离不同的服务调用防止一个服务的失败影响到其他服务。超时Timeouts为服务调用设置超时时间超过时间后会触发超时异常。重试Retry在遇到特定异常时自动重试服务调用可以配置重试次数和间隔。缓存Caching提供缓存机制以避免重复执行计算密集型或远程调用。
Resilience4J 的一大特点是它的轻量级特性它只使用了 Vavr 库一个函数式编程库没有其他外部库依赖。这使得它在集成到现有系统时非常方便且性能开销小。
Resilience4J 设计上易于配置支持通过代码、配置文件或运行时参数进行配置。它也支持通过 actuator 模块与 Spring Boot 的监控和管理特性集成。
由于 Resilience4J 的这些特性和优势它在现代分布式系统和微服务架构中得到了广泛应用尤其是在需要高可用性和弹性的环境中。 Resilience4j官方地址
https://resilience4j.readme.io/ https://github.com/resilience4j/resilience4j Resilience4j-RateLimiter
https://resilience4j.readme.io/docs/ratelimiter RateLimiter 的默认实现是 AtomicRateLimiter 它通过 AtomicReference 管理其状态。 AtomicRateLimiter.State 是完全不可变的。
功能点 Warm-Up Period: 当启动应用程序或重置后可能会有一个预热期在此期间速率限制器逐渐增加允许的请求速率。这是为了防止启动后流量突然激增从而可能导致系统过载。 Steady State: 预热期结束后速率限制器进入稳定状态。在此阶段速率限制器根据配置的速率限制允许请求通过。例如如果将限制设置为每分钟 100 个请求则速率限制器将允许大约每 0.6 秒一个请求。 Limit Exceeded: 如果传入请求速率超过配置的限制速率限制器立即开始拒绝超出的请求。 Replenishing Tokens: 速率限制器以与配置的限制相对应的速率持续补充“Token”。每个允许的请求消耗一个令牌。如果系统未充分利用允许的速率则未使用的令牌会累积从而允许偶尔爆发请求。 Cooldown Period: 如果速率限制器因超出速率限制而拒绝请求则可能存在一个冷却期在此期间速率限制器会再次逐渐增加允许的请求速率。这是为了防止限制放宽后流量突然激增。 微服务演示 我们的演示有 2 个服务名为支付服务和支付处理器。 付款服务处理来自购物者的传入付款请求并将其转发到付款处理器进行处理。支付处理器处理并发送结果。
我们将对支付服务实施速率限制以控制传入付款请求的速率。
Payment processor
首先构建支付处理器因为它是一个依赖服务. 为了演示的目的将其简化为显示成功消息 POM
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.1.2/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.artisan/groupIdartifactIdpayment-processor/artifactIdversion0.0.1-SNAPSHOT/versionnamepayment-processor/namepropertiesjava.version17/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project 配置文件
server:port: 1010
spring:application:name: payment-processorService
package com.artisan.paymentprocessor.service;
/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
public interface PaymentProcessorService {String processPayment(String paymentInfo);
}package com.artisan.paymentprocessor.service.impl;import org.springframework.stereotype.Service;import com.artisan.paymentprocessor.service.PaymentProcessorService;
/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
Service
public class PaymentProcessorServiceImpl implements PaymentProcessorService {Overridepublic String processPayment(String paymentInfo) {// Simulated logic to process paymentreturn Payment processed: paymentInfo;}
} Controller
package com.artisan.paymentprocessor.controller;import com.artisan.paymentprocessor.service.PaymentProcessorService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import lombok.RequiredArgsConstructor;/*** author artisan*/
RestController
RequestMapping(/api/v1/processor-payment)
RequiredArgsConstructor
public class PaymentProcessorController {private final PaymentProcessorService paymentProcessorService;PostMappingpublic String processPayment(RequestBody String paymentInfo) {return paymentProcessorService.processPayment(paymentInfo);}
}测试一下 Payment service
我们将配置 Rate Limiter并通过 Actuator 监控其状态 。
POM
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.1.2/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.artisan/groupIdartifactIdpayment-service/artifactIdversion0.0.1-SNAPSHOT/versionnamepayment-service/namepropertiesjava.version17/java.versionspring-cloud.version2022.0.4/spring-cloud.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-circuitbreaker-resilience4j/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project Model
public interface Type {
}Data
public class Success implements Type {private final String msg;
} Data
public class Failure implements Type {private final String msg;
}Service
如何调用外部API --------------我们这里使用 Spring的 RestTemplate 。
package com.artisan.paymentservice.service;import com.artisan.paymentservice.model.Type;/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
public interface PaymentService {Type submitPayment(String paymentInfo);
}
package com.artisan.paymentservice.service.impl;import com.artisan.paymentservice.model.Failure;
import com.artisan.paymentservice.model.Success;
import com.artisan.paymentservice.model.Type;
import com.artisan.paymentservice.service.PaymentService;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import lombok.RequiredArgsConstructor;/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
Service
RequiredArgsConstructor
public class PaymentServiceImpl implements PaymentService {private final RestTemplate restTemplate;private static final String SERVICE_NAME payment-service;private static final String PAYMENT_PROCESSOR_URL http://localhost:1010/api/v1/processor-payment;OverrideRateLimiter(name SERVICE_NAME, fallbackMethod fallbackMethod)public Type submitPayment(String paymentInfo) {HttpHeaders headers new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);HttpEntityString entity new HttpEntity(paymentInfo, headers);ResponseEntityString response restTemplate.exchange(PAYMENT_PROCESSOR_URL,HttpMethod.POST, entity, String.class);Success success new Success(response.getBody());return success;}private Type fallbackMethod(RequestNotPermitted requestNotPermitted) {return new Failure(服务降级 Payment service does not permit further calls);}
}
重点关注: RateLimiter(name SERVICE_NAME, fallbackMethod fallbackMethod)
需要注意这两种方法应该返回相同的数据类型 所以对两个模型类都使用“Type”来实现。 RestConfig
package com.artisan.paymentservice.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
Configuration
public class RestConfig {Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}Controller
package com.artisan.paymentservice.controller;import com.artisan.paymentservice.model.Type;
import com.artisan.paymentservice.service.PaymentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import lombok.RequiredArgsConstructor;
/*** author 小工匠* version 1.0* mark: show me the code , change the world*/
RestController
RequestMapping(/api/v1/payment-service)
RequiredArgsConstructor
public class PaymentController {private final PaymentService paymentService;PostMappingpublic Type submitPayment(RequestBody String paymentInfo) {return paymentService.submitPayment(paymentInfo);}
} 配置
server:port: 9090
spring:application:name: payment-service
management:endpoint:health:show-details: alwaysendpoints:web:exposure:include: healthhealth:ratelimiters:enabled: true
resilience4j:ratelimiter:instances:payment-service:limit-for-period: 5limit-refresh-period: 15stimeout-duration: 5sregister-health-indicator: truelimit-for-period一个“limit-refresh-period”期间允许的请求数limit-refresh-period指定“limit-for-period”将被重置的持续时间timeout-duration设置速率限制器允许后续请求的最大等待时间。
这段配置确保了payment-service服务的请求速率不会超过每15秒5次同时如果请求超过10秒没有响应则认为请求超时。此外通过注册健康指标可以对速率限制器的状态进行监控和管理。 验证 探究 Rate Limiting
确保两个服务启动成功 访问 http://localhost:9090/actuator/health 查看速率限制器详细信息。 请求三次 观察 http://localhost:9090/api/v1/ payment-service 请求3次 然后刷新执行器链接 http://localhost:9090/actuator/health 等待15秒
等待 15 秒如果在 API 访问之前开始时间可能会更短然后刷新执行器链接 http://localhost:9090/actuator/health我们将观察到允许的请求重置为 5。 连续访问6次
API 访问 6 次 http://localhost:9090/api/v1/ payment-service。第 6 个请求将因超出限制而延迟 5 秒。等待期间刷新 http://localhost:9090/actuator/health 以获取以下详细信息