SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
hystrix參數(shù)使用方法
通過注解@HystrixCommand的commandProperties去配置,
如下就是hystrix命令超時時間命令執(zhí)行超時時間,為1000ms和執(zhí)行是不啟用超時
@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時候發(fā)現(xiàn)@hystrixcommand注解找不到,由于Spring Boot 2.0剛出沒多久,所以這塊資料網(wǎng)上很少,查閱資料說是新版本中不包含此注解了,需要重新引入。
報錯信息:

源碼:



解決方案: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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot @ConditionalOnMissingBean注解的作用詳解
- SpringBoot 注解事務(wù)聲明式事務(wù)的方式
- springboot2中使用@JsonFormat注解不生效的解決
- SpringBoot中@Pattern注解對時間格式校驗方式
- springboot FeignClient注解及參數(shù)
- SpringBoot中@ConfigurationProperties注解的使用與源碼詳解
- 親測SpringBoot參數(shù)傳遞及@RequestBody注解---踩過的坑及解決
- 詳解SpringBoot 快速整合Mybatis(去XML化+注解進階)
- SpringBoot單元測試中@SpyBean使用小結(jié)
相關(guān)文章
Spring Security實現(xiàn)驗證碼登錄功能
這篇文章主要介紹了Spring Security實現(xiàn)驗證碼登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
java實現(xiàn)簡單的學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細介紹了java實現(xiàn)簡單的學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
詳解mysql插入數(shù)據(jù)后返回自增ID的七種方法
這篇文章主要介紹了詳解mysql插入數(shù)據(jù)后返回自增ID的七種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringMVC 使用JSR-303進行校驗 @Valid示例
本篇文章主要介紹了SpringMVC 使用JSR-303進行校驗 @Valid示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
使用Jackson來實現(xiàn)Java對象與JSON的相互轉(zhuǎn)換的教程
這篇文章主要介紹了使用Jackson來實現(xiàn)Java對象與JSON的互相轉(zhuǎn)換的教程,文中羅列了3中Jackson的使用方式,需要的朋友可以參考下2016-01-01
Java中的CyclicBarrier循環(huán)柵欄深入解析
這篇文章主要介紹了Java中的CyclicBarrier循環(huán)柵欄深入解析,CycleBarrier 它就相當(dāng)于是一個柵欄,所有線程在到達柵欄后都需要等待其他線程,等所有線程都到達后,再一起通過,需要的朋友可以參考下2023-12-12
Java.toCharArray()和charAt()的效率對比分析
這篇文章主要介紹了Java.toCharArray()和charAt()的效率對比分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

