spring cloud微服务分布式云架构--hystrix的使用
hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足)
接下来的通过一个案例对hystrix的使用进行说明,案例完成的功能:
服务消费者根据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/eureka instance: prefer-ip-address: true instance-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 { @Autowired private RestTemplate restTemplate; @GetMapping(value="/simple/{id}",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8") @HystrixCommand(fallbackMethod = "findByIdFallback") public User findUserById(@PathVariable Long id){ //服务提供者地址 PROVIDER-USER return 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 @LoadBalanced public 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/eureka instance: prefer-ip-address: true turbine: aggregator: clusterConfig: default #默认defualt,只有一个服务时候可以写服务名称的大写CUSTOMER-USER appConfig: customer-user clusterNameExpression: "'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填入,便可进行数据的图像化。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
专访熊节:编程其实是个社会活动
重构20年,有人说Martin Fowler改变了人类开发软件的模式,这一点也不过分,从《分析模式》《UML精粹》《领域特定语言》,到这本《重构》新版可以看得出来,他的每一本书都是软件开发人员必备的案头读物。此前他参与的“敏捷宣言”,更是引领了整个行业对敏捷开发的认识,一直到现在。 2009年,熊节在为《重构》第1版的中译本再版整理译稿时,他已经隐约察觉行业中对“重构”这个概念的矛盾张力。 如今又是10年过去,只从国内的情况而论,“重构”概念的表里分离,大有愈演愈烈之势。随着当年的一线技术人员纷纷走上领导岗位,他们乐于将“重构”这块漂亮招牌用在更宽泛的环境下,例如系统架构乃至组织结构,都可以“重构”一下。然而基本功的欠缺,却也一路如影随形。当年在对象中的刀劈斧砍,如今被照搬到了架构、组织的调整。于是“重构”的痛苦回忆又一遍遍重演,甚而程度更深、影响更广、为害更烈。 此时转头看Martin Fowler时隔将近廿载后终于付梓的《重构》第2版,熊节说:“我不禁感叹于Fowler先生对‘微末功夫’ 的执着。在此书尚未成型之前,我和当时ThoughtWorks的同事曾有很多猜测,猜Fowler...
- 下一篇
spring cloud微服务分布式云架构-Gateway入门
Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。 入门案例 接下来,介绍一下简单使用SpringCloud Gateway路由功能 创建项目 新建一个项目,项目种加入SpringCloud Gateway依赖,完整pom如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schem...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS8编译安装MySQL8.0.19