SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
hystrix參數(shù)使用方法
通過注解@HystrixCommand的commandProperties去配置,
如下就是hystrix命令超時(shí)時(shí)間命令執(zhí)行超時(shí)時(shí)間,為1000ms和執(zhí)行是不啟用超時(shí)
@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版本時(shí),使用斷路器 Hystrix時(shí)候發(fā)現(xiàn)@hystrixcommand注解找不到,由于Spring Boot 2.0剛出沒多久,所以這塊資料網(wǎng)上很少,查閱資料說是新版本中不包含此注解了,需要重新引入。
報(bào)錯(cuò)信息:
源碼:
解決方案: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>
在程序的啟動(dòng)類ServiceRibbonApplication 加@EnableHystrix注解開啟Hystrix
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java字符編碼原理(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
Java開發(fā)中,常常會遇到亂碼的問題,一旦遇到這種問題,常常比較煩惱,大家都不想承認(rèn)是自己的代碼問題,其實(shí)搞明白編碼的本質(zhì)過程就簡單多了,接下來小編給大家?guī)韏ava字符編碼原理,要求看看吧2017-04-04Filter中獲取傳遞參數(shù)方式(解決post請求參數(shù)問題)
這篇文章主要介紹了Filter中獲取傳遞參數(shù)方式(解決post請求參數(shù)問題),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08MyBatis映射文件中的動(dòng)態(tài)SQL實(shí)例詳解
在本文中,我們深入探討了動(dòng)態(tài)SQL的各種標(biāo)簽,包括<if>、<choose>、<trim>、<foreach>等,通過實(shí)際的例子演示了它們的用法,感興趣的朋友一起揭開動(dòng)態(tài)SQL的神秘面紗,帶你領(lǐng)略它的魅力2024-01-01Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(56)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08一篇文章帶了解如何用SpringBoot在RequestBody中優(yōu)雅的使用枚舉參數(shù)
這篇文章主要介紹了SpringBoot中RequestBodyAdvice使用枚舉參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08基于UDP協(xié)議實(shí)現(xiàn)聊天系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于UDP協(xié)議實(shí)現(xiàn)聊天系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04