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

停掉应用,测试
边栏推荐
- Power BI----这几个技能让报表更具“逼格“
- Learning to Pre-train Graph Neural Networks(图预训练与微调差异)
- 【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
- R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为jpeg格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)
- [USB device design] - composite device, dual hid high-speed (64BYTE and 1024byte)
- 已解决The JSP specification requires that an attribute name is preceded by whitespace
- 投屏收费背后:爱奇艺季度盈利,优酷急了?
- [cloud co creation] what is the role of AI in mathematics? What will be the disruptive impact on the mathematical world in the future?
- R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
- 30 sets of Chinese style ppt/ creative ppt templates
猜你喜欢

【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020

利用wireshark对TCP抓包分析

Sword finger offer 22. the penultimate node in the linked list

对比学习的应用(LCGNN,VideoMoCo,GraphCL,XMC-GAN)

web编程(二)CGI相关

马斯克的“灵魂永生”:一半炒作,一半忽悠
![[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

从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」

pycharm连接远程服务器ssh -u 报错:No such file or directory

brpc源码解析(六)—— 基础类socket详解
随机推荐
[USB device design] - composite device, dual hid high-speed (64BYTE and 1024byte)
【RS采样】A Gain-Tuning Dynamic Negative Sampler for Recommendation (WWW 2022)
Transformer变体(Routing Transformer,Linformer,Big Bird)
【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020
There is no sound output problem in the headphone jack on the front panel of MSI motherboard [solved]
Go garbage collector Guide
[comparative learning] understanding the behavior of contractual loss (CVPR '21)
Video Caption(跨模态视频摘要/字幕生成)
GPT plus money (OpenAI CLIP,DALL-E)
How to solve the problem of the error reported by the Flink SQL client when connecting to MySQL?
Brpc source code analysis (V) -- detailed explanation of basic resource pool
return 和 finally的执行顺序 ?各位大佬请看过来,
Feign使用
Meta-learning(元学习与少样本学习)
PHP uploads the FTP path file to the curl Base64 image on the Internet server
那些离开网易的年轻人
创新突破!亚信科技助力中国移动某省完成核心账务数据库自主可控改造
[high concurrency] I summarized the best learning route of concurrent programming with 10 diagrams!! (recommended Collection)
Qin long, a technical expert of Alibaba cloud: a prerequisite for reliability assurance - how to carry out chaos engineering on the cloud
[untitled]