当前位置:网站首页>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;
}
}
测试,成功测试

停掉应用,测试
边栏推荐
- 浅谈低代码技术在物流管理中的应用与创新
- Go 垃圾回收器指南
- 【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021
- 'C:\xampp\php\ext\php_ zip. Dll'-%1 is not a valid Win32 Application Solution
- [RS sampling] a gain tuning dynamic negative sampler for recommendation (WWW 2022)
- Transformer变体(Sparse Transformer,Longformer,Switch Transformer)
- 给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
- Video Caption(跨模态视频摘要/字幕生成)
- Intelligent information retrieval(智能信息检索综述)
- Brpc source code analysis (VII) -- worker bthread scheduling based on parkinglot
猜你喜欢

氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心

Differences in usage between tostring() and new string()

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

brpc源码解析(五)—— 基础类resource pool详解

Start with the development of wechat official account

【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)

Word中的空白页,怎么也删不掉?如何操作?

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

知识图谱用于推荐系统问题(MVIN,KERL,CKAN,KRED,GAEAT)

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
随机推荐
[high concurrency] Why is the simpledateformat class thread safe? (six solutions are attached, which are recommended for collection)
异构图神经网络用于推荐系统问题(ACKRec,HFGN)
Transformer变体(Sparse Transformer,Longformer,Switch Transformer)
浅谈低代码技术在物流管理中的应用与创新
Brpc source code analysis (VII) -- worker bthread scheduling based on parkinglot
Brpc source code analysis (VIII) -- detailed explanation of the basic class eventdispatcher
Go 垃圾回收器指南
R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
Sword finger offer 22. the penultimate node in the linked list
程序员送给女孩子的精美礼物,H5立方体,唯美,精致,高清
创新突破!亚信科技助力中国移动某省完成核心账务数据库自主可控改造
php curl post Length Required 错误设置header头
给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
How to solve the problem of the error reported by the Flink SQL client when connecting to MySQL?
马斯克的“灵魂永生”:一半炒作,一半忽悠
Qin long, a technical expert of Alibaba cloud: a prerequisite for reliability assurance - how to carry out chaos engineering on the cloud
'C:\xampp\php\ext\php_zip.dll' - %1 不是有效的 Win32 应用程序 解决
【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
PHP 上传ftp路径文件到外网服务器上 curl base64图片