SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案
程序员文章站
2024-02-26 11:44:34
hystrix参数使用方法
通过注解@hystrixcommand的commandproperties去配置,
如下就是hystrix命令超时时间命令执行超时时...
hystrix参数使用方法
通过注解@hystrixcommand的commandproperties去配置,
如下就是hystrix命令超时时间命令执行超时时间,为1000ms和执行是不启用超时
@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添加依赖
<dependency> <groupid>com.netflix.hystrix</groupid> <artifactid>hystrix-javanica</artifactid> <version>release</version> </dependency>
完整pom.xml
<?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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。