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 "用户信息查询出现异常!";
}
}
 

Hystix熔断解决雪崩问题的更多相关文章

  1. 2019.12.4 Hystix熔断&Feign进行远程调用&Zuul

    0.学习目标 会配置Hystix熔断 会使用Feign进行远程调用 能独立搭建Zuul网关 能编写Zuul的过滤器 1.Hystrix 1.1.简介 Hystrix,英文意思是豪猪,全身是刺,看起来就 ...

  2. spring cloud Hystix熔断机制--基本熔断配置和Fegin client熔断配置

    所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢? 当现在服务的提供方出现了问题之后整个的程序将出现错误的信息显示 ...

  3. Redis击穿、穿透、雪崩产生原因以及解决思路

    击穿 大家都知道,计算机的瓶颈之一就是IO,为了解决内存与磁盘速度不匹配的问题,产生了缓存,将一些热点数据放在内存中,随用随取,降低连接到数据库的请求链接,避免数据库挂掉.需要注意的是,无论是击穿还是 ...

  4. Spring Cloud之Hystrix雪崩效应解决方案

    基于Hystris解决雪崩效应: 1.服务降级:    防止用户一直等待,使用降级方式,调用FallBack(返回友好提示,不会去处理请求) 案例: 当前请求人数过多,请稍后重试 2.服务熔断:(和服 ...

  5. Spring Cloud第五篇 | 服务熔断Hystrix

    ​ 本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  6. 雪崩利器 hystrix-go 源码分析

    阅读源码的过程,就像是在像武侠小说里阅读武功秘籍一样,分析高手的一招一式,提炼出精髓,来增强自己的内力. 之前的帖子说了一下微服务的雪崩效应和常见的解决方案,太水,没有上代码怎么叫解决方案.githu ...

  7. 初识Sentinel--雪崩问题及其解决方法

    什么是雪崩问题? 雪崩问题:微服务调用链中的某个服务故障,引起整个链路中的所有微服务不可用. 解决雪崩问题的常见四种方式: ①超时处理:设定超时时长,请求超过一定时间没有响应就返回错误信息,不会无休止 ...

  8. 14 微服务电商【黑马乐优商城】:day03-springcloud(Hystix,Feign)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一) ...

  9. 009 SpringCloud 学习笔记5-----Hystrix保护机制

    1.概述 Hystrix,英文意思是豪猪,全身是刺,看起来就不好惹,是一种保护机制.Hystrix也是Netflix公司的一款组件.主页:https://github.com/Netflix/Hyst ...

随机推荐

  1. Mysql服务启动与关闭

    启动: 在cmd中输入 net start mysql 关闭: 在cmd中输入  net stop mysql

  2. TensorFlow 常用函数汇总

    本文介绍了tensorflow的常用函数,源自网上整理. TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU ...

  3. 如何离线安装python的whl库

    对于使用公司内网环境办公的人来说,可能无法使用pip install 命令安装python的whl库.对于这种情况,我们可以用以下的方法安装一个whl库. 1 下载whl文件,下载时注意,whl文件的 ...

  4. 《Spring Cloud与Docker微服务架构实战》配套代码

    不才写了本使用Spring Cloud玩转微服务架构的书,书名是<Spring Cloud与Docker微服务架构实战> - 周立,已于2017-01-12交稿.不少朋友想先看看源码,现将 ...

  5. MySQL基本命令1

    在ubuntu系统中操作命令:登录:mysql -uroot -p启动:service mysql start停止:service mysql stop重启:service mysql restart ...

  6. MAC下Intellij IDEA常用快捷键

    alt+f7 : 查找在哪里使用 command+alt+f7 : 这个是查找选中的字符在工程中出现的地方,可以不是方法变量类等,这个和上面的有区别的 command+F7 : 可以查询当前元素在当前 ...

  7. delete.go

    package api import (     "net/http"     "fmt"     "io/ioutil"     &quo ...

  8. 打包前端WebSite到Go程序

    打包前端WebSite到Go程序 Coolpy5发布在即,新版本要求服务端程序只是一个运行文件,经历了go的template无数坑后,最后还是放弃了,所以还是要把前端独立开发一个纯前端程序,但是go程 ...

  9. 约会 倍增lca

    题意:一棵树,给两个点,求树上有多少点到他俩距离相等 倍增lca,分好多情况讨论.. #include<cstdio> #include<cstring> #include&l ...

  10. 【状压dp】Bzoj2064 分裂

    Description 背景: 和久必分,分久必和... 题目描述: 中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力. 同时经常搞OI的他把这个变成了一个数学模型. 假设中 ...