hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足)

接下来的通过一个案例对hystrix的使用进行说明,案例完成的功能:Spring Cloud大型企业分布式微服务云架构源码请加一七九一七四三三八零

服务消费者根据Id调用服务提供者的接口,获取User表单的对应的记录,若请求超时则返回id为-1的User记录

一、基于Ribbon

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version><mysql-connector.version>5.1.39</mysql-connector.version>
</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.3.5.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId><version>1.3.5.RELEASE</version></dependency></dependencies>复制代码

2、application.yml

server:port: 8089
spring:application:name: customer-user
eureka:client: serviceUrl: defaultZone: http://user:zj123@localhost:8761/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
provider-user: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule复制代码

3、Controller调用类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhuojing.bean.User;@RestController
public class UserController {@Autowiredprivate RestTemplate restTemplate;@GetMapping(value="/simple/{id}",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")@HystrixCommand(fallbackMethod = "findByIdFallback")public User findUserById(@PathVariable Long id){//服务提供者地址 PROVIDER-USERreturn this.restTemplate.getForObject("http://PROVIDER-USER/simple/"+id, User.class);}/*** 断路器模式,当请求的消费者provider-user超时的情况下,就会直接调用此方法,此方法的参数和返回类型必须和findUserById方法一致,请求超时时间为1秒* @param id* @return*/public User findByIdFallback(Long id){User user = new User();user.setId(0L);return user;}}复制代码

hystrix的默认的超时时间为1秒,若自定hystrix超时时间有一下两种方式

a、@HystrixCommand注解配置

@HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})复制代码

b、在application.yml配置

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

4、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class AppCusRibbonPropertiesTest {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(AppCusRibbonPropertiesTest.class, args);}
}复制代码

二、dashboard的使用

1、pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId><version>1.3.5.RELEASE</version></dependency></dependencies>复制代码

2、application.yml

server: port: 8030

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@EnableHystrixDashboard
@SpringBootApplication
public class DashboardApplication {public static void main(String[] args) {SpringApplication.run(DashboardApplication.class, args);}
}复制代码

启动后访问:http:/ /localhost:8030/hystrix 进入首页,在地址栏上输入http:/ /localhost:8089/hystrix.stream(hystrix.stream)进行监控,在使用了hystrix的服务调用后才有数据。

三、Turbine的使用,Turbine是对微服务集群的监听

1、pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-turbine</artifactId><version>1.3.5.RELEASE</version></dependency></dependencies>复制代码

2、application.yml

server:port: 8031
spring:application:name: microservice-hystrix-turbine
eureka:client:serviceUrl:defaultZone: http://user:zj123@localhost:8761/eurekainstance:prefer-ip-address: true
turbine:aggregator:clusterConfig: default  #默认defualt,只有一个服务时候可以写服务名称的大写CUSTOMER-USERappConfig: customer-userclusterNameExpression: "'default'"复制代码

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;@EnableTurbine
@SpringBootApplication
public class TurbineApplication {public static void main(String[] args) {SpringApplication.run(TurbineApplication.class, args);}
}复制代码

hystrix 主页面中将localhost:8031/turbine.stream?cluster=CUSTOMER-USER填入,便可进行数据的图像化。

spring cloud微服务分布式云架构--hystrix的使用相关推荐

  1. (二)spring cloud微服务分布式云架构 - 整合企业架构的技术点

    spring cloud本身提供的组件就很多,但我们需要按照企业的业务模式来定制企业所需要的通用架构,那我们现在需要考虑使用哪些技术呢? 下面我针对于spring cloud微服务分布式云架构做了以下 ...

  2. spring cloud微服务分布式云架构 - 整合企业架构的技术点

    spring cloud本身提供的组件就很多,但我们需要按照企业的业务模式来定制企业所需要的通用架构,那我们现在需要考虑使用哪些技术呢? 下面我针对于spring cloud微服务分布式云架构做了以下 ...

  3. (二)spring cloud微服务分布式云架构-整合企业架构的技术点

    spring cloud本身提供的组件就很多,但我们需要按照企业的业务模式来定制企业所需要的通用架构,那我们现在需要考虑使用哪些技术呢?Spring Cloud大型企业分布式微服务云架构源码请加一七九 ...

  4. spring cloud微服务分布式云架构-整合企业架构的技术点

    spring cloud本身提供的组件就很多,但我们需要按照企业的业务模式来定制企业所需要的通用架构,那我们现在需要考虑使用哪些技术呢?完整项目的源码来源 技术支持一七九一七四三三八零 下面我针对于s ...

  5. spring cloud微服务分布式云架构 - 整合企业架构的技术点(二)

    点击上面 免费订阅本账号! 本文作者:it菲菲 原文:https://yq.aliyun.com/articles/672231 点击阅读全文前往 spring cloud本身提供的组件就很多,但我们 ...

  6. spring cloud微服务分布式云架构 - Spring Cloud集成项目简介

    Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...

  7. Spring Cloud微服务分布式云架构—集成项目简介

    Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Clo ...

  8. spring cloud微服务分布式云架构 - Spring Cloud集成项目简介(三)

    点击上面 免费订阅本账号! 本文作者:it菲菲 原文:https://yq.aliyun.com/articles/672242 点击阅读全文前往 Spring Cloud集成项目有很多,下面我们列举 ...

  9. spring cloud微服务分布式云架构 - Spring Cloud简介

    Spring Cloud是一系列框架的有序集合.利用Spring Boot的开发模式简化了分布式系统基础设施的开发,如服务发现.注册.配置中心.消息总线.负载均衡.断路器.数据监控等(这里只简单的列了 ...

最新文章

  1. Hinton新论文:如何在神经网络中表示“部分-整体层次结构”?
  2. 从短句到长文,计算机如何学习阅读理解
  3. mysql释放练级_面试官:谈谈Mysql事务隔离级别?
  4. 计算机网络(谢希仁第八版)第四章:网络层
  5. OnPaint()函数的作用原理
  6. MyBatis 实际使用案例-typeAliases
  7. CreatePipe匿名管道通信
  8. 入门人工智能,我究竟该学些什么?
  9. 编程常用英语词汇 | GitHub
  10. 83998 连接服务器出错_Linux高性能服务器设计
  11. mysql添加mcafee 审计插件
  12. crmeb重新安装_CRMEB
  13. 华为防火墙-USG6000系列-补丁的安装和删除
  14. Java绩效评语_导师评语(转) - Java, Only Java! - BlogJava
  15. 教你如何使用SwipeRefreshLayout来构建一个上拉加载下拉刷新框架
  16. 什么是非同质化代币(NFT)\ NFT有哪些应用?
  17. box-shadow 93种经典效果
  18. 【91xcz】教你win7电脑如何升级win8系统
  19. 荣耀智慧屏 X1 55 长期评测 — 智慧的基础是什么?
  20. 使用numpy计算相关系数矩阵:np.corrcoef()

热门文章

  1. apue第四章习题的一些拙见(不定时更新)
  2. git - 简易指南
  3. silverlight数据库应用程序开发
  4. 如何成为Android开发高手
  5. ssh远程执行多个命令
  6. C#中抽象类和接口的区别
  7. C#精髓【月儿原创】第一讲 使用垃圾回收器
  8. PHP内核中的哈希表结构
  9. OpenFace库(Tadas Baltrusaitis)中基于HOG进行正脸人脸检测的测试代码
  10. NEON在Android中的使用举例