前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring Boot Hystrix

Spring Boot Hystrix

作者头像
黑洞代码
发布于 2021-10-26 07:06:24
发布于 2021-10-26 07:06:24
45900
代码可运行
举报
运行总次数:0
代码可运行

Hystrix是Netflix的一个库。Hystrix隔离了服务之间的访问点,阻止了它们之间的级联故障并提供了后备选项。

例如,当调用第三方应用程序时,发送响应需要更多时间。所以在那个时候,控件转到了回退方法并将自定义响应返回给你的应用程序。

在本章中,将看到如何在Spring Boot应用程序中实现Hystrix。

首先,需要在构建配置文件中添加Spring Cloud Starter Hystrix依赖项。

Maven用户可以在pom.xml 文件中添加以下依赖项 -

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

现在,将@EnableHystrix注释添加到主Spring Boot应用程序类文件中。@EnableHystrix注释用于将Hystrix功能启用到Spring Boot应用程序中。

主 Spring Boot 应用程序类文件代码如下 -

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableHystrix
public class HystrixappApplication {
   public static void main(String[] args) {
      SpringApplication.run(HystrixappApplication.class, args);
   }
}

现在编写一个简单的Rest Controller,使其在请求的时间后3秒后返回String。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RequestMapping(value = "/")
public String hello() throws InterruptedException {
   Thread.sleep(3000);
   return "Welcome Hystrix";
}

现在,为Rest API添加@Hystrix命令和@HystrixProperty,并以毫秒为单位定义超时值。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = {
   @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
})

接下来,如果请求需要很长时间来响应,请定义回退方法fallback_hello()。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
private String fallback_hello() {
   return "Request fails. It takes long time to response";
}

此处显示包含REST API和Hystrix属性的完整Rest Controller类文件 -

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RequestMapping(value = "/")
@HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = {
   @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
})
public String hello() throws InterruptedException {
   Thread.sleep(3000);
   return "Welcome Hystrix";
}
private String fallback_hello() {
   return "Request fails. It takes long time to response";
}

在此示例中,REST API编写在主Spring Boot应用程序类文件本身中。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import org.springframework.boot.SpringApplication;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@SpringBootApplication
@EnableHystrix
@RestController
public class HystrixappApplication {
   public static void main(String[] args) {
      SpringApplication.run(HystrixappApplication.class, args);
   }
   @RequestMapping(value = "/")
   @HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = {
      @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
   })
   public String hello() throws InterruptedException {
      Thread.sleep(3000);
      return "Welcome Hystrix";
   }
   private String fallback_hello() {
      return "Request fails. It takes long time to response";
   }
}

可以创建可执行的JAR文件,并使用以下Maven或Gradle命令运行Spring Boot应用程序 -

对于Maven,使用如下所示的命令 -

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
mvn clean install

在“BUILD SUCCESS”之后,可以在target目录下找到JAR文件。

现在,使用下面给出的命令运行JAR文件 -

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
java –jar <JARFILE>

这将在Tomcat端口8080上启动应用程序。

现在,从Web浏览器中点击URL => http://localhost:8080/,然后查看Hystrix响应。API需要3秒钟才能响应,但Hystrix超时为1秒。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-10-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 落叶飞翔的蜗牛 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档