Spring Hystrix是否仅与@Service和@Component一起使用?
我有一个定义为@RestController的类,并且我的HystrixCommand无法启动,该方法可以执行,但不能充当HystrixCommand。当我创建一个@Service类并放入HystrixCommand方法并回退到其中时,HystrixCommand将正常运行。
可与@EnableHystrix一起使用的适当的Spring注释是什么?
我一直在将现有应用程序迁移到Spring Cloud的服务发现,Ribbon负载平衡和断路器。该应用程序已经广泛使用RestTemplate,并且我已经能够成功使用模板的负载平衡版本。但是,我一直在测试服务有两个实例的情况,并且将其中一个实例停运。我希望RestTemplate故障转移到下一个服务器。根据我所做的研究,似乎故障切换逻辑存在于Feign客户端以及使用Zuul时。看来LoadBalancedRest模板没有用于故障转移的逻辑。在深入研究代码时,RibbonClientHttpRequestFactory似乎正在使用netflix RestClient(似乎具有进行重试的逻辑)。
那么,我从哪里去工作呢?
我宁愿不使用Feign客户端,因为我必须清除很多代码。我发现此链接建议将@Retryable注释与@HystrixCommand一起使用,但这似乎应该成为负载平衡的其余模板的一部分。
我做了一些深入研究RibbonClientHttpRequestFactory.RibbonHttpRequest的代码:
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
try {
addHeaders(headers);
if (outputStream != null) {
outputStream.close();
builder.entity(outputStream.toByteArray());
}
HttpRequest request = builder.build();
HttpResponse response = client.execute(request, config);
return new RibbonHttpResponse(response);
}
catch (Exception e) {
throw new IOException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
看来,如果我重写此方法并将其更改为使用“ client.executeWithLoadBalancer()”,那么我也许能够利用RestClient内置的重试逻辑?我想我可以创建自己的RibbonClientHttpRequestFactory版本来执行此操作?
只是寻找最佳方法的指导。
谢谢
我正在使用spring cloud consul 1.0.0.M5和spring boot 1.3.2.spring cloud consul用于连接到consul代理的默认主机和端口是localhost:8500.我想改变这一点,但到目前为止我尝试的每种方法都不尊重这些设置.
我查看了spring cloud consul的源代码,并使用spring-boot中的@ConfigurationProperties在属性前缀spring.cloud.consul中读取了ConsulProperties.
以下是ConsulProperties的Spring云源中的声明:
/**
* @author Spencer Gibb
*/
@ConfigurationProperties("spring.cloud.consul")
@Data
public class ConsulProperties {
@NotNull
private String host = "localhost";
@NotNull
private int port = 8500;
private boolean enabled = true;
private String prefix = "config";
}
Run Code Online (Sandbox Code Playgroud)
以及如何在spring cloud consul源中注入它以定义向咨询代理发出http请求的ConsulClient:
/**
* @author Spencer Gibb
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnConsulEnabled
public class ConsulAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ConsulProperties consulProperties() {
return new ConsulProperties();
}
@Bean
@ConditionalOnMissingBean
public ConsulClient consulClient() {
return …Run Code Online (Sandbox Code Playgroud) 我正在使用带有RabbitMQ活页夹的Spring Cloud Stream.它适用于byte[]有效负载和Java本机序列化,但我需要使用JSON有效负载.
这是我的处理器类.
@EnableBinding(Processor.class)
public class MessageProcessor {
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public OutputDto handleIncomingMessage(InputDto inputDto) {
// Run some job.
return new OutputDto();
}
}
Run Code Online (Sandbox Code Playgroud)
InputDto并且OutputDto是杰克逊注释的POJO.
我正在尝试使用具有基于文件的存储库的Spring Cloud配置服务器在Spring Cloud客户端之间共享配置:
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
// application.yml
server:
port: 8888
spring:
profiles:
active: native
test:
foo: world
Run Code Online (Sandbox Code Playgroud)
我的一个Spring Cloud客户端使用test.foo配置服务器中定义的配置,其配置如下:
@SpringBootApplication
@RestController
public class HelloWorldServiceApplication {
@Value("${test.foo}")
private String foo;
@RequestMapping(path = "/", method = RequestMethod.GET)
@ResponseBody
public String helloWorld() {
return "Hello " + this.foo;
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldServiceApplication.class, args);
}
}
// …Run Code Online (Sandbox Code Playgroud) 我们有一个Spring-Boot应用程序,我们使用Eureka来发现Spring Cloud Config并检索配置.我们正在整合Vault以注入安全/敏感信息,并且在加载和解析参数方面遇到了问题.我们作为Vault客户端使用的项目是vault-spring-boot-starter,只要我们不通过eureka与config-server一起使用它就可以很好地工作.
具体而言,Eureka访问URL包含从Vault检索的参数/凭据.启用所有组件后,当DiscoveryClient尝试访问尚未填充/替换参数的URL时,Eureka请求将失败.
( Example: http://${user}:${pass}..... )
试图指定@Order和
@AutoConfigureBefore({EurekaClientAutoConfiguration.class, DiscoveryClientConfigServiceAutoConfiguration.class})
在Vault-spring-boot-starter的VaultBootstrapConfiguration中似乎没有任何影响.我认为这个问题与处理PropertySources的顺序有关,但是我无法在Eureka之前成功注入Vault的PropertySource.在DiscoveryClient和配置服务器访问之前,我们如何指示自定义/ Vault PropertySourceLocator逻辑执行?
更新
我们使用的是spring-cloud版本Angel.SR6.
我按照建议将@Order(Ordered.HIGHEST_PRECEDENCE)注释添加到VaultPropertySourceLocator,但参数解析仍然不起作用.启用Spring调试日志记录后,我认为Vault PropertySource实际上存在,但由于某种原因未被使用.我修改了代码,以便VaultConfiguration实现SmartLifecycle和Ordered(order = 0和phase = Integer.MIN_VALUE),这可能会影响事情.我将不得不做更多的调试,试图找出正在发生的事情.
我正在尝试实施Turbine AMQP来整合从多个服务到Hystrix Dashboard的所有流.
所以我在gradle文件中添加了几个依赖项,之后由于某种原因我无法启动我的应用程序.
来自启动的LOGS,我看到异常.
[LogMessage=Application startup failed]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bindingService' defined in class path resource [org/springframework/cloud/stream/config/ChannelBindingServiceConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [org.springframework.cloud.stream.binder.BinderFactory]: Error creating bean with name 'binderFactory' defined in class path resource [org/springframework/cloud/stream/config/BinderFactoryConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.cloud.stream.binder.BinderTypeRegistry]: Error creating bean with name 'binderTypeRegistry' defined in class path resource [org/springframework/cloud/stream/config/BinderFactoryConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed …Run Code Online (Sandbox Code Playgroud) turbine spring-boot hystrix spring-cloud spring-cloud-stream
在阅读了关于Spring cloud Zuul的文档后,我已经了解到,使用SEMAPHORE隔离和大约2的最大并发请求,Zuul服务器可以轻松处理大约5000 rps.当我试图从Jmeter调用服务时,它的值大约为2000,抛出以下异常并显示错误率100%.
com.netflix.hystrix.exception.HystrixRuntimeException:
Service1 could not acquire a semaphore for execution and no fallback available.
Run Code Online (Sandbox Code Playgroud)
然后我将最大并发请求的数量增加到200000但仍然抛出异常,但错误率已下降到10%.能告诉我原因吗?这是因为Spring云Zuul中的微服务缓慢或任何配置问题?以下是配置:
ribbon:
ConnectTimeout: 20000000
ReadTimeout: 20000000
MaxTotalHttpConnections: 5000
MaxHttpConnectionsPerHost: 5000
ActiveConnectionsLimit: 4000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 20000000
zuul.hystrix.command.default.execution.isolation.strategy: SEMAPHORE
zuul.hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests: 2000
zuul.hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests: 2000
zuul.eureka.default.semaphore.maxSemaphores: 30000
Run Code Online (Sandbox Code Playgroud) 我有一个问题,也许很简单,但我找不到解决方案.
我正在使用spring boot并在代码中添加了一些注释,如下所示:
@EnableEurekaClient
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在其他一些环境中,例如,在生产环境中,我们想要删除EurekaClient,但我不想手动为每个环境手动删除它,相反,我想使用环境变量或命令行参数来控制行为.我想这样做:
@EnableEurekaClient(Enabled = {EnableEureka})
@SpringBootApplication
@EnableCaching
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我可以轻松启动此应用程序而无需触及代码.
任何人都可以告诉我这是否可能?如果是这样,我该怎么办?
谢谢
@FeignClient(name ="test",url ="http:// xxxx")
如何在运行时更改feign url(url ="http:// xxxx")?因为网址只能在运行时确定.
spring-cloud ×10
spring-boot ×5
spring ×4
java ×3
hystrix ×2
consul ×1
json ×1
netflix ×1
netflix-zuul ×1
turbine ×1