当前位置:网站首页>Use of hystrix
Use of hystrix
2022-07-25 12:29:00 【dengjili】
Hystrix Project use
To configure pom.xml rely on
<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>
To configure RestTemplate object
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate create() {
return new RestTemplate();
}
}
Add test class
@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 The test program is abnormal
- /hello2 Test timeout exception
Activate Hystrix, Start the class to add comments @EnableHystrix
@EnableHystrix
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Get ready
start-up Eureka service , And register two applications as test cluster machines , As shown in the figure ; Test interface /hello
Feign Project use
Then add the configuration pom.xml rely on
<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>
encapsulation Feign Interface ,eureka-provider-app To register to Eureka Service name in
@FeignClient(value = "eureka-provider-app", fallback = TestClientFailBack.class)
public interface TestClient {
@GetMapping("/hello")
String hello();
}
TestClientFailBack Failure handling strategy
@Component
public class TestClientFailBack implements TestClient{
@Override
public String hello() {
return "FAIL_fail";
}
}
Add test class
@RestController
public class TestController {
@Autowired
private TestClient client;
@GetMapping("/test")
public String test() {
String result = client.hello();
return result;
}
}
Activate Feign To configure
@EnableFeignClients(basePackageClasses = {
TestClient.class})
@EnableHystrix
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The configuration file application.properties add to Eureka Registration configuration , start-up feign And hystrix Integrate
eureka.client.serviceUrl.defaultZone=http://192.168.1.55:8761/eureka/
feign.hystrix.enabled=true```
Add test class
```java
@RestController
public class TestController {
@Autowired
private TestClient client;
@GetMapping("/test")
public String test() {
String result = client.hello();
return result;
}
}
test , Successful test

Stop the app , test 
边栏推荐
- From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"
- 微软Azure和易观分析联合发布《企业级云原生平台驱动数字化转型》报告
- 通过Referer请求头实现防盗链
- Spirng @Conditional 条件注解的使用
- 【11】 Production and adjustment of vector and grid data Legends
- Feign使用
- 【五】页面和打印设置
- 客户端开放下载, 欢迎尝鲜
- Data transmission under the same LAN based on tcp/ip
- 【5】 Page and print settings
猜你喜欢

Build a series of vision transformer practices, and finally meet, Timm library!

第一个scrapy爬虫

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?

面试官:“同学,你做过真实落地项目吗?”

919. Complete binary tree inserter: simple BFS application problem

Those young people who left Netease

Crawler crawls dynamic website

微软Azure和易观分析联合发布《企业级云原生平台驱动数字化转型》报告

Brpc source code analysis (II) -- the processing process of brpc receiving requests

Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"
随机推荐
【4】 Layout view and layout toolbar usage
通过Referer请求头实现防盗链
Jenkins configuration pipeline
Unexpected rollback exception analysis and transaction propagation strategy for nested transactions
通信总线协议一 :UART
想要做好软件测试,可以先了解AST、SCA和渗透测试
搭建Vision Transformer系列实践,终于见面了,Timm库!
Eureka registration center opens password authentication - record
【高并发】通过源码深度分析线程池中Worker线程的执行流程
Analysis of TCP packet capturing using Wireshark
919. 完全二叉树插入器 : 简单 BFS 运用题
【三】DEM山体阴影效果
OSPF comprehensive experiment
协程
Client open download, welcome to try
R language ggplot2 visualization: visualize the scatter diagram, add text labels to some data points in the scatter diagram, and use geom of ggrep package_ text_ The repl function avoids overlapping l
Dr. water 2
3.2.1 什么是机器学习?
Plus版SBOM:流水线物料清单PBOM
Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"