软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案

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

报错信息:

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

来源:https://blog.csdn.net/zjh_746140129/article/details/80697921

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com