当前位置:网站首页>Fault tolerant processing with hystrix
Fault tolerant processing with hystrix
2022-07-23 07:59:00 【Female programmer ape who can't write code】
Create a new Maven project hystrix-feign-demo, increase Hystrix Dependence , The code is as follows .
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
Add on the startup class @EnableHystrix perhaps @EnableCircuitBreaker. Be careful ,@EnableHystrix It contains @EnableCircuitBreaker.
Then write a method to call the interface , Add one to it @HystrixCommand annotation , Used to specify the method to be called when a dependent service call is delayed or fails , The code is as follows .
@GetMapping("/callHello")
@HystrixCommand(fallbackMethod = "defaultCallHello")
public String callHello() {
String result = restTemplate.getForObject("http://localhost:8088/house/hello", String.class);
return result;
}
It will be used when the call fails to trigger the fusing defaultCallHello Method to go back to the specific content , Definition default-CallHello The code for the method is as follows .
public String defaultCallHello() {
return "fail";
}
As long as it doesn't start 8088 The service where the port is located , call /callHello Interface , You can see that the returned content is “fail”, Pictured 1 Shown .
Will start... On the class @EnableHystrix Get rid of , Restart the service , Call again /callHello The interface can see that it returns 500 error message , At this time, the fallback function is not used .
{
code: 500,
message: "I/O error on GET request for
"http://localhost:8088/house/hello": Connection refused; nested
exception is java.net.ConnectException: Connection refused
", data:
null
}
Configuration details
HystrixCommand In addition to fallbackMethod There are many more configurations , Now let's take a look at these configurations , As shown in the following table :
HystrixCommand Configuration details 
Please refer to the official configuration information document :https://github.com/Netflix/Hystrix/wiki/Configuration.
It's all listed above Hystrix Configuration information , So in Spring Cloud How to use it in English ? Just use it on the interface method HystrixCommand annotation ( As shown in the following code ), Just specify the corresponding attribute .
@HystrixCommand(fallbackMethod = "defaultCallHello",commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value = "THREAD")
}
)
@GetMapping("/callHello")
public String callHello() {
String result = restTemplate.getForObject("http://localhost:8088/house/hello", String.class);
return result;
}
边栏推荐
猜你喜欢
随机推荐
Ftxui basic notes (Hello World)
亚马逊旗下Zoox通过安全测试 并在加州申请试驾
實驗二 YUV
Bottom compulsory of large factory: "communication realization between application program and AMS"
ROS2常用命令行工具整理ROS2CLI
记一次线上SQL死锁事故:如何避免死锁?
Fastapi learning (II) -- fastapi+jinjia2 template rendering web page (jump back to the rendering page)
LAN SDN hard core technology insider 17 from one to 100
不会吧?钉钉都下载了,你还不知道可以这样玩?
@Transactional transaction methods contain multiple transaction methods of the same kind. These transaction methods themselves are set to fail. There are two solutions
实验五 JPEG
1.11 ArrayList&学生管理系统
The new idea 2022.2 was officially released, and the new features are really fragrant
I use the factory mode in jd.com and explain the factory mode clearly
实验二 YUV
真人踩过的坑,告诉你避免自动化测试常犯的10个错误
QT document reading notes - qaudioinput & qaudioformat parsing and examples
Worthington哺乳动物乳酸脱氢酶研究——特点及测定方案
6-14漏洞利用-rpcbind漏洞利用
pycharm中使用私钥远程连接服务器









