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

停掉应用,测试
边栏推荐
- Introduction to redis
- Power Bi -- these skills make the report more "compelling"“
- Transformer variants (routing transformer, linformer, big bird)
- Go 垃圾回收器指南
- What is the global event bus?
- Transformer variants (spark transformer, longformer, switch transformer)
- 【多模态】《TransRec: Learning Transferable Recommendation from Mixture-of-Modality Feedback》 Arxiv‘22
- Word中的空白页,怎么也删不掉?如何操作?
- Data transmission under the same LAN based on tcp/ip
- Eureka使用记录
猜你喜欢

30 sets of Chinese style ppt/ creative ppt templates

Multi-Label Image Classification(多标签图像分类)

PHP curl post x-www-form-urlencoded

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)

【无标题】

Transformer variants (spark transformer, longformer, switch transformer)

Brpc source code analysis (I) -- the main process of RPC service addition and server startup

NLP的基本概念1

【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021

Transformer变体(Routing Transformer,Linformer,Big Bird)
随机推荐
【多模态】《HiT: Hierarchical Transformer with Momentum Contrast for Video-Text Retrieval》ICCV 2021
Meta learning (meta learning and small sample learning)
Solved files' name is invalid or doors not exist (1205)
【微服务~Sentinel】Sentinel降级、限流、熔断
30套中国风PPT/创意PPT模板
Go 垃圾回收器指南
【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
【无标题】
[high concurrency] Why is the simpledateformat class thread safe? (six solutions are attached, which are recommended for collection)
brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制
【AI4Code最终章】AlphaCode:《Competition-Level Code Generation with AlphaCode》(DeepMind)
给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
Eureka使用记录
What is the difference between session and cookie?? Xiaobai came to tell you
【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
Video caption (cross modal video summary / subtitle generation)
Brpc source code analysis (V) -- detailed explanation of basic resource pool
'C:\xampp\php\ext\php_ zip. Dll'-%1 is not a valid Win32 Application Solution
Power BI----这几个技能让报表更具“逼格“
dirReader.readEntries 兼容性问题 。异常错误DOMException