Netflix之后,如何用Spring Cloud 新组件构建微服务架构?( 三 )



Netflix之后,如何用Spring Cloud 新组件构建微服务架构?
文章图片
下一步 , 我们需要使用Resilience4JCircuitBreakerFactory创建一个断路器实例并为HTTP客户端启用它 , 如下所示 。 @RestController@RequestMapping("/caller")classCallerController(privatevaltemplate:RestTemplate,privatevalfactory:Resilience4JCircuitBreakerFactory){privatevarid:Int=0@PostMapping("/random-send/{message}")funrandomSend(@PathVariablemessage:String):CallmeResponse?{valrequest=CallmeRequest(++id,message)valcircuit=factory.create("random-circuit")returncircuit.run{template.postForObject("http://inter-callme-service/callme/random-call",request,CallmeResponse::class.java)}}}6SpringCloudAPI网关在我们微服务架构中 , 缺失的最后一个元素就是API网关 。 SpringCloudGateway能帮助我们实现这一组件 。 目前 , 它是SpringCloud中仅次于SpringCloudNetflix , 欢迎程度排在第二名的项目 。 在GitHub上它有2800+star 。 它构建在SpringWebFlux和Reactor项目之上 , 它以反应式的方式运行 , 需要Netty作为运行时框架 。
API网关的主要目标是提供一个有效的方式路由至API , 从而为外部客户端隐藏微服务系统的复杂性 , 但是它也能解决一些安全性和可靠性相关的问题 。 用来配置SpringCloudGateway的主要组件是路由 。
它由一个ID、一个目标URI、一个断言的集合和一个过滤器的集合组成 。 如果断言聚合为true的话 , 则会匹配路由 。 通过过滤器 , 我们则可以在发送下游请求之前或之后修改请求和响应 。
通过预定义的网关过滤器集合 , 我们可以实现路径重写、速率限制、发现客户端、断路器、fallback或路由指标等机制 。 为了在网关上启用所有这些功能 , 我们首先需要包含以下依赖关系 。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-reflect</artifactId><groupId>org.jetbrains.kotlin</groupId><artifactId>kotlin-stdlib</artifactId><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId></dependency>为实现上述所列的所有特性 , 我们并不需要编写太多的代码 。 几乎所有的内容都是通过应用属性配置的 。
predicates:-Path=/api/callme/**filters:-RewritePath=/api(?/?.*),${path}-name:RequestRateLimiterargs:redis-rate-limiter.replenishRate:20redis-rate-limiter.burstCapacity:40name:sampleSlowCircuitBreakerfallbackUri:forward:/fallback/test-id:inter-caller-serviceuri:lb://inter-caller-servicepredicates:-Path=/api/caller/**-StripPrefix=1-name:RequestRateLimiterhost:192.168.99.100有些设置依然需要在代码中进行配置 , 也就是断路器的配置 , 它基于Resilience4J项目 , 我们需要注册Customizer<reactiveresilience4jcircuitbreakerfactory></reactiveresilience4jcircuitbreakerfactory>bean 。 此外 , 我们还需要定义一个速率限制的key , 它用来设置为限制计数选择请求的策略 。 @SpringBootApplicationclassApiGatewayApplication{@BeanfunkeyResolver:KeyResolver=KeyResolver{_->Mono.just("1")}@BeanfundefaultCustomizer:Customizer<ReactiveResilience4JCircuitBreakerFactory>{returnCustomizer{factory:ReactiveResilience4JCircuitBreakerFactory->factory.configureDefault{id:String?->Resilience4JConfigBuilder(id).timeLimiterConfig(TimeLimiterConfig.custom.timeoutDuration(Duration.ofMillis(500)).build).circuitBreakerConfig(CircuitBreakerConfig.custom.slidingWindowSize(10).failureRateThreshold(33.3F).slowCallRateThreshold(33.3F).build).build}}}}runApplication<ApiGatewayApplication>(*args)}7小结在本文中 , 我们快速了解了如何使用最新的SpringCloud组件构建微服务架构 。 关于这些组件的更多详情 , 读者可以阅读SpringCloud的最新文档 。
原文链接:https://piotrminkowski.com/2020/05/01/a-new-era-of-spring-cloud/