SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案
作者:zjh_746140129 发布时间:2021-08-26 12:54:32
标签: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刚出没多久,所以这块资料网上很少,查阅资料说是新版本中不包含此注解了,需要重新引入。
报错信息:
源码:
解决方案: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
来源:https://blog.csdn.net/zjh_746140129/article/details/80697921


猜你喜欢
- 开发环境:jdk版本:JDK8maven版本:maven-3.5.2开发工具:Itellij IDEA 2017.1前提条件:已安装以上软件
- 关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现。接
- 原理和listview一样 ,都是重写Android原生控件Activitypackage com.example.refreshgridv
- 一.Android项目中引入aar包的方法我在使用高德地图的sdk的时候,选择用引入aar包的方式,然后按照网上的教程引入。但是一直报错,我
- 一、只读自动属性(Read-only auto-properties) C# 6之前我们构建只读自动属性: public stri
- 本文实例讲述了C# winform实现右下角弹出窗口结果的方法。分享给大家供大家参考,具体如下:using System.Runtime.I
- Android OnCreate()中获取控件高度与宽度试过在OnCreate()中获取控件高度与宽度的童鞋都知道,getWidth()与g
- 本文实例讲述了Android编程设计模式之状态模式。分享给大家供大家参考,具体如下:一、介绍状态模式中的行为是由状态来决定的,不同的状态下有
- 1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空。例如:
- 目录目标功能点准备工作引入 Maven 依赖配置 DAO 数据层创建 JWT 工具类登录LoginFilterLoginSuccessHan
- 场景点击拨打电话按钮,跳转到拨打电话页面点击发送短信按钮,跳转到发送短信页面注:实现将布局改为LinearLayout,并通过android
- 本文实例讲述了Java常用HASH算法。分享给大家供大家参考,具体如下:/*** Hash算法大全<br>* 推荐使用FNV1算
- Spring Boot 异常处理异常处理是一种识别并响应错误的一致性机制,异常机制可以把程序中的异常处理代码和正常的业务逻辑代码分离,包装程
- Android中处理图像是一件很常见的事情,这里记录备忘一些亲身使用过的处理图片数据的方法。转为BitmapRGB值转Bitmappriva
- CSV(Comma Separated Values)文件是一种纯文本文件,包含用逗号分隔的数据,常用于将数据从一个应用程序导入或导出到另一
- 前言应用系统需要通过Cache来缓存不经常改变得数据来提高系统性能和增加系统吞吐量,避免直接访问数据库等低速存储系统。缓存的数据通常存放在访
- 在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将nu
- 今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都是可以提取出来的,就自己把那些公共部分提出出来,以后如果要获取某部分的硬件信息就
- MapperScan添加动态配置(占位符)在对Mybatis自动扫描配置中,使用注解配置时,@MapperScan中的配置,通常配置如下:@
- 在 C# 9 中,foreach 循环可以使用扩展方法。在本文中,我们将通过例子回顾 C# 9 中如何扩展 foreach 循环。代码演示下