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

停掉应用,测试
边栏推荐
- 浅谈低代码技术在物流管理中的应用与创新
- 【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
- Word中的空白页,怎么也删不掉?如何操作?
- [high concurrency] I summarized the best learning route of concurrent programming with 10 diagrams!! (recommended Collection)
- Sword finger offer 22. the penultimate node in the linked list
- 【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
- 已解决The JSP specification requires that an attribute name is preceded by whitespace
- 【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
- brpc源码解析(四)—— Bthread机制
- 【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)
猜你喜欢

Power BI----这几个技能让报表更具“逼格“

【6篇文章串讲ScalableGNN】围绕WWW 2022 best paper《PaSca》

剑指 Offer 22. 链表中倒数第k个节点

微信公众号开发 入手

浅谈低代码技术在物流管理中的应用与创新

brpc源码解析(四)—— Bthread机制

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

【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021

selenium使用———安装、测试

Return and finally? Everyone, please look over here,
随机推荐
Word中的空白页,怎么也删不掉?如何操作?
一文入门Redis
【黑马早报】运营23年,易趣网宣布关停;蔚来对大众CEO抛出橄榄枝;华为天才少年曾放弃360万年薪;尹烨回应饶毅炮轰其伪科学...
马斯克的“灵魂永生”:一半炒作,一半忽悠
那些离开网易的年轻人
[multimodal] transferrec: learning transferable recommendation from texture of modality feedback arXiv '22
Learning to Pre-train Graph Neural Networks(图预训练与微调差异)
【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
【高并发】高并发场景下一种比读写锁更快的锁,看完我彻底折服了!!(建议收藏)
pycharm连接远程服务器ssh -u 报错:No such file or directory
Application of comparative learning (lcgnn, videomoco, graphcl, XMC GaN)
[GCN multimodal RS] pre training representations of multi modal multi query e-commerce search KDD 2022
已解决 Files‘ name is invalid or does not exist (1205)
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
LeetCode 50. Pow(x,n)
GPT plus money (OpenAI CLIP,DALL-E)
Power BI----这几个技能让报表更具“逼格“
【多模态】《TransRec: Learning Transferable Recommendation from Mixture-of-Modality Feedback》 Arxiv‘22
Intelligent information retrieval (overview of intelligent information retrieval)
What is the global event bus?