Spring Cloud-如何为分布式Spring应用程序获得重试,负载平衡和断路器的好处 [英] Spring cloud - how to get benefits of retry,load balancing and circuit breaker for distributed spring application

查看:134
本文介绍了Spring Cloud-如何为分布式Spring应用程序获得重试,负载平衡和断路器的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在spring-cloud-Eureka支持的微服务应用程序中具有以下功能.

I want the following features in spring-cloud-Eureka backed microservices application.

1)负载平衡-如果我为一个服务有3个节点,则应在它们之间进行负载平衡

1) Load balancing - if I have 3 nodes for one service, load balancing should happen between them

2)重试逻辑-如果其中一个节点没有响应,则应重试一定次数(例如3.应该是可配置的),然后再回到另一个节点.

2)Retry logic - if one of the nodes did not respond, retry should happen for certain number ( eg 3. should be configurable) before falling back to another node.

3)断路器-如果由于某些原因,服务的所有3个节点在访问db并抛出异常或没有响应时都出现问题,则电路应打开,调用后退方法,并且在服务恢复后电路自动关闭

3)circuit breaker - if for some reasons, all the 3 nodes of service is having some issue accessing db and throwing exceptions or not responding, the circuit should get open, fall back method called and circuit automatically closes after the services recovers.

通过查看Spring-cloud的许多示例,我发现

Looking at many examples of Spring-cloud, I figured out

1)RestTemplate将为选项1提供帮助.但是,当RestTemplate访问服务的一个实例时,如果该节点发生故障,它将尝试与其他两个节点一起使用吗?

1) RestTemplate will help with option 1. but when RestTemplate access one instance of service and if the node fails, will it try with other two nodes?

2)Hystix将为断路器选项提供帮助(上述3).但是,如果只有一个节点没有响应,它将在打开电路并调用回退方法之前尝试其他节点.服务恢复后会自动关闭电路吗?

2) Hystix will help with circuit breaker option (3 above). but if just one node is not responding, will it try other nodes, before opening up circuit and call fallback method. and will it automatically close circuit once the service recovers?

3)如何通过spring-cloud获取retryLogic?我确实了解@Retryable注释.但是在以下情况下有帮助吗? 对一个节点重试3次,然后在发生故障后,尝试3个下一个节点,对最后一个节点尝试3次,然后再打开断路器.

3) how to get retryLogic with spring-cloud? I do know about @Retryable annotation. But will it help in the following situation? Retry with one node for 3 times and after it fails, try the next node 3 times and the last node 3 times before circuit breaker kicks in.

我看到所有这些配置在Spring Cloud中都可用.但是很难理解如何为所有这些配置以实现有效的解决方案.

I see that all these configurations are available in spring cloud. but having a hard-time understanding how to configure for all these for efficient solution.

这是 建议:

@HystrixCommand
@Retryable
public Object doSomething() {
   // use your RestTemplate here
}

但是我不完全知道这是否会对我上面提到的所有微妙之处有所帮助.

But I don't totally know if it is going to help me with all the subtleties I mentioned above.

我确实看到有一个@FeignClient.但是从这个

I do see there is a @FeignClient. But from this blog, I understand that it provides a high level feature for HTTP client requests. Does it help with retry and circuit breaker and load balancing all-in-one?

谢谢

推荐答案

我确实看到有一个@FeignClient.它对重试和断路器以及负载平衡多合一有帮助吗?

I do see there is a @FeignClient. Does it help with retry and circuit breaker and load balancing all-in-one?

如果您使用完整的spring-cloud堆栈,它实际上可以解决您提到的所有问题.

If you are using the full spring-cloud stack, it actually solves everything you mentioned.

在这种情况下,netflix组件在spring-cloud中如下所示:

The netflix components in this scenario are the following in spring-cloud:

Eureka-服务注册表

让我们强烈地注册您的服务,以便只需要在应用程序(eureka)中修复一台主机.

Let's you dyanmically register your services so you only need to fix one host in your app (eureka).

功能区-负载均衡器

开箱即用的功能是为您提供循环负载均衡,但是您可以实现自己的@RibbonClient(即使是针对特定服务),也可以基于eureka元数据设计自定义负载均衡.负载平衡发生在客户端.

Out of the box it's providing you with round robin loadbalancing, but you can implement your own @RibbonClient (even for a specific service) and design your custom loadbalancing for example based on eureka metadata. The loadbalancing happens on the client side.

伪装-Http客户端

使用@FeignClient,您可以为其他服务(或基础架构之外的服务)快速开发客户端.它与功能区和尤里卡(eureka)集成在一起,因此您可以参考服务@FeignClient(yourServiceNameInEureka),最终得到的是一个客户端,该客户端使用您的首选逻辑在已注册实例之间进行负载平衡.如果您使用的是Spring,则可以使用熟悉的@RequestMapping批注来描述您正在使用的端点.

With @FeignClient you can rapidly develop clients for you other services (or services outside of your infrastructure). It is integrated with ribbon and eureka so you can refer to your services @FeignClient(yourServiceNameInEureka) and what you end up with is a client which loadbalances between the registered instances with your preferred logic. If you are using spring you can use the familiar @RequestMapping annotation to describe the endpoint you are using.

Hystrix-断路器

默认情况下,您的伪装客户端将使用hystrix,每个请求都将包装在hystrix命令中.当然,您可以手动创建hytrix命令,并根据需要对其进行配置.

By default your feign clients will use hystrix, every request will be wrapped in a hystrix command. You can of course create hytrix commands by hand and configure them for your needs.

您必须进行一些配置才能使这些功能正常工作(实际上,您的配置中只有几个@Enable注释).

You have to configure a little to get thees working (actually just a few @Enable annotation on your configuration).

我强烈建议您阅读提供的spring文档,因为它可以在相当快速的阅读中涵盖您几乎所有方面.

I highly recommend reading the provided spring documentation because it wraps up almost all of your aspects in a fairly quick read.

http://cloud.spring.io/spring- cloud-netflix/spring-cloud-netflix.html

这篇关于Spring Cloud-如何为分布式Spring应用程序获得重试,负载平衡和断路器的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆