hystrix参数使用方法
通过注解@hystrixcommand的commandproperties去配置,
如下就是hystrix命令超时时间命令执行超时时间,为1000ms和执行是不启用超时
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
@restcontroller public class moviecontroller { @autowired private resttemplate resttemplate;
@getmapping ( "/movie/{id}" ) @hystrixcommand (commandproperties = { @hystrixproperty (name = "execution.isolation.thread.timeoutinmilliseconds" , value = "1000" ), @hystrixproperty (name = "execution.timeout.enabled" , value = "false" )},fallbackmethod = "findbyidfallback" ) public user findbyid( @pathvariable long id) { return this .resttemplate.getforobject( "http://microservice-provider-user/simple/" + id, user. class ); }
/** * fallback方法 * @param id * @return */ public user findbyidfallback( long id) { user user = new user(); user.setid(5l); return user; } } |
问题描述:
笔者在使用spring boot 2.0整合spring cloud finchley.rc2版本时,使用断路器 hystrix时候发现@hystrixcommand注解找不到,由于spring boot 2.0刚出没多久,所以这块资料网上很少,查阅资料说是新版本中不包含此注解了,需要重新引入。
报错信息:
源码:
解决方案:pom.xml添加依赖
1 2 3 4 5 |
<dependency> <groupid>com.netflix.hystrix</groupid> <artifactid>hystrix-javanica</artifactid> <version>release</version> </dependency> |
完整pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.serverribbon</groupid> <artifactid>serverribbon</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging>
<name>serverribbon</name> <description>demo project for spring boot</description>
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.0 . 2 .release</version> <relativepath/> <!-- lookup parent from repository --> </parent>
<properties> <project.build.sourceencoding>utf-</project.build.sourceencoding> <project.reporting.outputencoding>utf-</project.reporting.outputencoding> <java.version> 1.8 </java.version> <spring-cloud.version>finchley.rc2</spring-cloud.version> </properties>
<dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> </dependency>
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> </dependency>
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-hystrix</artifactid> </dependency>
<dependency> <groupid>com.netflix.hystrix</groupid> <artifactid>hystrix-javanica</artifactid> <version>release</version> </dependency>
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>com.netflix.hystrix</groupid> <artifactid>hystrix-core</artifactid> <version>release</version> </dependency> <dependency> <groupid>com.netflix.hystrix</groupid> <artifactid>hystrix-core</artifactid> <version>release</version> </dependency> </dependencies>
<dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement>
<build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>
<repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories>
</project> |
在程序的启动类serviceribbonapplication 加@enablehystrix注解开启hystrix
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/zjh_746140129/article/details/80697921
查看更多关于SpringBoot2.0整合SpringCloud Finchley @hystrixcomman的详细内容...