SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案

作者:zjh_746140129 时间:2021-08-26 12:54:32 

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

标签:SpringCloud,Finchley,@hystrixcommand
0
投稿

猜你喜欢

  • c#使用xamarin编写拨打电话程序

    2023-09-04 18:09:20
  • Java 多线程使用要点分析

    2023-12-18 22:41:36
  • 详细解读JAVA多线程实现的三种方式

    2022-01-14 04:35:31
  • Flutter加载图片流程之ImageProvider源码示例解析

    2021-07-19 05:05:19
  • JDK源码之PriorityQueue解析

    2022-05-15 17:17:15
  • Java实现平滑加权轮询算法之降权和提权详解

    2022-10-02 20:51:22
  • macOS上使用gperftools定位Java内存泄漏问题及解决方案

    2023-03-02 11:42:38
  • 浅谈Java中replace与replaceAll区别

    2021-07-05 12:56:56
  • C#编程获取客户端计算机硬件及系统信息功能示例

    2023-08-09 01:57:15
  • java 中的instanceof用法详解及instanceof是什么意思(推荐)

    2023-06-07 13:52:27
  • Mybatis Plus 中的LambdaQueryWrapper示例详解

    2023-03-26 04:52:06
  • SpringMVC整合mybatis实例代码

    2022-09-24 08:22:03
  • NancyFx框架检测任务管理器详解

    2023-02-18 13:10:53
  • Java实现简单登陆界面

    2022-11-25 05:47:47
  • 使用@Order控制配置类/AOP/方法/字段的加载顺序详解

    2023-09-05 17:34:35
  • Java实现企业员工管理系统

    2023-08-22 16:44:50
  • Android利用RecyclerView实现全选、置顶和拖拽功能示例

    2023-06-05 18:21:22
  • C#入门教程之集合ArrayList用法详解

    2022-04-30 06:32:13
  • Java的Struts框架简介与环境配置教程

    2023-10-29 05:23:03
  • Java实现简易学生管理系统

    2022-10-16 19:26:49
  • asp之家 软件编程 m.aspxhome.com