Hystix熔断解决雪崩问题
2019-04-29 08:51:15来源:博客园 阅读 ()
1.线程隔离,服务降级(服务的消费方做降级处理)
当服务繁忙时,如果服务出现异常,不是粗暴的直接报错,而是返回一个友好的提示,虽然拒绝了用户的访问,但是会返回一个结果。
这就好比去买鱼,平常超市买鱼会额外赠送杀鱼的服务。等到逢年过节,超时繁忙时,可能就不提供杀鱼服务了,这就是服务的降级。
系统特别繁忙时,一些次要服务暂时中断,优先保证主要服务的畅通,一切资源优先让给主要服务来使用,在双十一、618时,京东天猫都会采用这样的策略。
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
UserConsumerApplication.java
package cn.itcast.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
//@EnableDiscoveryClient
//@SpringBootApplication
//@EnableCircuitBreaker //服务的熔断
@SpringCloudApplication //SpringCloudApplication可以替代上面三个
public class UserConsumerDemoApplication {
@Bean
@LoadBalanced //负载均衡
public RestTemplate restTemplate() {
// 这次我们使用了OkHttp客户端,只需要注入工厂即可
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
public static void main(String[] args) {
SpringApplication.run(UserConsumerDemoApplication.class, args);
}
}
3.UserHController.java
package cn.itcast.user.controller; import cn.itcast.user.pojo.User; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("consumerH") public class UserHController { @Autowired private RestTemplate restTemplate; @GetMapping("{id}") @HystrixCommand(fallbackMethod = "queryUserByIdFallback") //单个方法的超时返回 public String queryUserById(@PathVariable("id") Long id){ String url = "http://user-service/user/" + id; //User user = restTemplate.getForObject(url, User.class); //return user; String user = restTemplate.getForObject(url, String.class); return user; } public String queryUserByIdFallback(@PathVariable("id") Long id){ return "用户信息查询出现异常!"; // User user = new User(); // user.setId(id); // user.setNickName("用户信息查询出现异常!"); // return user; } }
==========================
配置统一超时信息
package cn.itcast.user.controller;
import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("consumerH")
@DefaultProperties(defaultFallback = "queryUserByIdFallback")
public class UserHController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("{id}")
//@HystrixCommand(fallbackMethod = "queryUserByIdFallback")
@HystrixCommand
public String queryUserById(@PathVariable("id") Long id){
String url = "http://user-service/user/" + id;
//User user = restTemplate.getForObject(url, User.class);
//return user;
String user = restTemplate.getForObject(url, String.class);
return user;
}
//public String queryUserByIdFallback(@PathVariable("id") Long id){
//return "用户信息查询出现异常!";
// User user = new User();
// user.setId(id);
// user.setNickName("用户信息查询出现异常!");
// return user;
//}
public String queryUserByIdFallback(){
return "用户信息查询出现异常!";
}
}
=============================================================
package cn.itcast.user.controller;
import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("consumerH")
@DefaultProperties(defaultFallback = "queryUserByIdFallback")
public class UserHController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("{id}")
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"), //响应超时时间
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
})
public String queryUserById(@PathVariable("id") Long id){
if(id % 2 ==0){
throw new RuntimeException("");
}
String url = "http://user-service/user/" + id;
String user = restTemplate.getForObject(url, String.class);
return user;
}
public String queryUserByIdFallback(){
return "用户信息查询出现异常!";
}
}
原文链接:https://www.cnblogs.com/wuxiang12580/p/10785643.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 聊聊微服务架构及分布式事务解决方案! 2020-06-10
- 解决IDEA Maven下载依赖包速度慢问题 2020-06-05
- Java生鲜电商平台-生鲜电商中配送订单解决方案(小程序/APP) 2020-05-31
- 一口气说出8种幂等性解决重复提交的方案,面试官懵了! 2020-05-30
- Java生鲜电商平台-生鲜电商中采购订单与库存解决方案(小程序 2020-05-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash