当前位置:网站首页>Hystrix使用
Hystrix使用
2022-07-25 11:22:00 【dengjili】
Hystrix项目使用
配置pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
配置RestTemplate对象
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate create() {
return new RestTemplate();
}
}
添加测试类
@RestController
public class HystrixController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "failCallHello")
@GetMapping("/hello")
public String callHello() {
String result = restTemplate.getForObject("http://192.168.1.55:8081/hello", String.class);
return result;
}
public String failCallHello() {
return "Fail";
}
@HystrixCommand(fallbackMethod = "failCallHello", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") })
@GetMapping("/hello2")
public String callHello2() {
String result = "Ok";
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
- /hello测试程序异常
- /hello2测试超时异常
激活Hystrix,启动类添加注解@EnableHystrix
@EnableHystrix
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
准备
启动Eureka服务,并注册两个应用作为测试集群机器,如图所示;测试接口/hello
Feign项目使用
再添加配置pom.xml依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
封装Feign接口,eureka-provider-app为注册到Eureka中的服务名称
@FeignClient(value = "eureka-provider-app", fallback = TestClientFailBack.class)
public interface TestClient {
@GetMapping("/hello")
String hello();
}
TestClientFailBack失败处理策略
@Component
public class TestClientFailBack implements TestClient{
@Override
public String hello() {
return "FAIL_fail";
}
}
添加测试类
@RestController
public class TestController {
@Autowired
private TestClient client;
@GetMapping("/test")
public String test() {
String result = client.hello();
return result;
}
}
激活Feign配置
@EnableFeignClients(basePackageClasses = {
TestClient.class})
@EnableHystrix
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件application.properties添加Eureka注册配置,启动feign与hystrix整合
eureka.client.serviceUrl.defaultZone=http://192.168.1.55:8761/eureka/
feign.hystrix.enabled=true```
添加测试类
```java
@RestController
public class TestController {
@Autowired
private TestClient client;
@GetMapping("/test")
public String test() {
String result = client.hello();
return result;
}
}
测试,成功测试

停掉应用,测试
边栏推荐
- What is the difference between session and cookie?? Xiaobai came to tell you
- 【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
- 已解决 Files‘ name is invalid or does not exist (1205)
- 异构图神经网络用于推荐系统问题(ACKRec,HFGN)
- Application and innovation of low code technology in logistics management
- 给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
- Introduction to pl/sql, very detailed notes
- R语言ggpubr包ggarrange函数将多幅图像组合起来、annotate_figure函数为组合图像添加注释、注解、标注信息、fig.lab参数添加图像标签、fig.lab.face参数指定样式
- aaaaaaaaaaA heH heH nuN
- Eureka使用记录
猜你喜欢

投屏收费背后:爱奇艺季度盈利,优酷急了?

Brpc source code analysis (V) -- detailed explanation of basic resource pool

return 和 finally的执行顺序 ?各位大佬请看过来,

30套中国风PPT/创意PPT模板

Application and innovation of low code technology in logistics management

Return and finally? Everyone, please look over here,

Solutions to the failure of winddowns planning task execution bat to execute PHP files
![[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel](/img/0f/a0139be99c61fde08e73a5be6d6b4c.png)
[imx6ull notes] - a preliminary exploration of the underlying driver of the kernel

【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)

Brpc source code analysis (VII) -- worker bthread scheduling based on parkinglot
随机推荐
LeetCode 50. Pow(x,n)
微信公众号开发 入手
Power Bi -- these skills make the report more "compelling"“
Intelligent information retrieval (overview of intelligent information retrieval)
【高并发】高并发场景下一种比读写锁更快的锁,看完我彻底折服了!!(建议收藏)
brpc源码解析(七)—— worker基于ParkingLot的bthread调度
【6篇文章串讲ScalableGNN】围绕WWW 2022 best paper《PaSca》
图神经网络用于推荐系统问题(IMP-GCN,LR-GCN)
RestTemplate与Ribbon简单使用
R语言ggpubr包ggarrange函数将多幅图像组合起来、annotate_figure函数为组合图像添加注释、注解、标注信息、fig.lab参数添加图像标签、fig.lab.face参数指定样式
【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
Learning to pre train graph neural networks
Power BI----这几个技能让报表更具“逼格“
【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020
【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
PHP 上传ftp路径文件到外网服务器上 curl base64图片
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
flink sql client 连接mysql报错异常,如何解决?
R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为jpeg格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)