SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办

发布时间:2021-07-13 11:21:09 作者:小新
来源:亿速云 阅读:360

这篇文章给大家分享的是有关SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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刚出没多久,所以这块资料网上很少,查阅资料说是新版本中不包含此注解了,需要重新引入。

报错信息:

SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办

源码:

SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办

SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办

SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办

解决方案: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

感谢各位的阅读!关于“SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到怎么办”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 11、RestTemplate+Ribbon整合断路器Hys
  2. SpringCloud之熔断器Hystrix(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springcloud finchley

上一篇:ajax中delete、put方法接收不到参数怎么办

下一篇:windows中怎么配置rocketmq开发环境

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》