wordpress google 360,信阳seo优化顾问,国际机票网站建设,大连百度推广Spring Cloud项目为Netflix OSS Hystrix库提供了全面的支持。 之前我已经写过有关如何使用原始Hystrix库包装远程调用的文章。 在这里#xff0c;我将探讨如何将Hystrix与Spring Cloud结合使用 基本 实际上并没有什么大不了的#xff0c;这些概念仅在特定于Spring引导的增强… Spring Cloud项目为Netflix OSS Hystrix库提供了全面的支持。 之前我已经写过有关如何使用原始Hystrix库包装远程调用的文章。 在这里我将探讨如何将Hystrix与Spring Cloud结合使用 基本 实际上并没有什么大不了的这些概念仅在特定于Spring引导的增强中保留下来。 考虑一个简单的Hystrix命令该命令包含对Remote服务的调用 import agg.samples.domain.Message;
import agg.samples.domain.MessageAcknowledgement;
import agg.samples.feign.RemoteServiceClient;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class RemoteMessageClientCommand extends HystrixCommandMessageAcknowledgement {private static final String COMMAND_GROUP demo;private static final Logger logger LoggerFactory.getLogger(RemoteMessageClientCommand.class);private final RemoteServiceClient remoteServiceClient;private final Message message;public RemoteMessageClientCommand(RemoteServiceClient remoteServiceClient, Message message) {super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP));this.remoteServiceClient remoteServiceClient;this.message message;}Overrideprotected MessageAcknowledgement run() throws Exception {logger.info(About to make Remote Call);return this.remoteServiceClient.sendMessage(this.message);}Overrideprotected MessageAcknowledgement getFallback() {return new MessageAcknowledgement(message.getId(), message.getPayload(), Fallback message);}
} 这里没有与Spring相关的类此命令可以直接在基于Spring的项目中使用例如在控制器中以下列方式使用 RestController
public class RemoteCallDirectCommandController {Autowiredprivate RemoteServiceClient remoteServiceClient;RequestMapping(/messageDirectCommand)public MessageAcknowledgement sendMessage(Message message) {RemoteMessageClientCommand remoteCallCommand new RemoteMessageClientCommand(remoteServiceClient, message);return remoteCallCommand.execute();}
} Hystrix命令的行为自定义通常是通过NetflixOSS Archaius属性执行的但是Spring Cloud提供了一个桥梁使Spring定义的属性显示为Archaius属性这简而言之意味着我可以使用Spring特定的配置文件来定义我的属性自定义命令行为时将可见。 因此如果较早进行自定义则可以使用Archaius属性来表示HelloWorldCommand的行为如下所示 hystrix.command.HelloWorldCommand.metrics.rollingStats.timeInMilliseconds10000
hystrix.command.HelloWorldCommand.execution.isolation.strategyTHREAD
hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds1000
hystrix.command.HelloWorldCommand.circuitBreaker.errorThresholdPercentage50
hystrix.command.HelloWorldCommand.circuitBreaker.requestVolumeThreshold20
hystrix.command.HelloWorldCommand.circuitBreaker.sleepWindowInMilliseconds5000 这可以在Spring Cloud世界中以完全相同的方式在application.properties文件或application.yml文件中通过以下方式完成 hystrix:command:HelloWorldCommand:metrics:rollingStats:timeInMilliseconds: 10000execution:isolation:strategy: THREADthread:timeoutInMilliseconds: 5000circuitBreaker:errorThresholdPercentage: 50requestVolumeThreshold: 20sleepWindowInMilliseconds: 5000基于注释的方法 我个人更喜欢基于直接命令的方法但是在Spring世界中使用Hystrix的更好方法可能是使用基于hystrix-javanica的注释。 最好通过一个示例说明如何使用此注释。 这是包装在Hystrix命令中并带有注释的远程调用 import agg.samples.domain.Message;
import agg.samples.domain.MessageAcknowledgement;
import agg.samples.feign.RemoteServiceClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class RemoteMessageAnnotationClient {private final RemoteServiceClient remoteServiceClient;Autowiredpublic RemoteMessageAnnotationClient(RemoteServiceClient remoteServiceClient) {this.remoteServiceClient remoteServiceClient;}HystrixCommand(fallbackMethod defaultMessage, commandKey RemoteMessageAnnotationClient )public MessageAcknowledgement sendMessage(Message message) {return this.remoteServiceClient.sendMessage(message);}public MessageAcknowledgement defaultMessage(Message message) {return new MessageAcknowledgement(-1, message.getPayload(), Fallback Payload);}} 这些注解使用方面转换为幕后的常规Hystrix命令但很妙的是在Spring Cloud项目中没有使用该注解的仪式它只是工作而已。 与以前一样如果需要自定义行为则可以使用命令特定的属性来完成。 一个小问题是默认情况下命令名称是方法名称因此在我的示例中命令名称将是“ sendMessage”我已使用注释将其自定义为其他名称。 如果您有兴趣进一步探索该示例请参阅我的github项目 。 翻译自: https://www.javacodegeeks.com/2015/11/spring-cloud-support-hystrix.html