博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud 2.x学习笔记:3、Hystrix(Greenwich版本)
阅读量:2390 次
发布时间:2019-05-10

本文共 3507 字,大约阅读时间需要 11 分钟。

1、Hystrix基本思想

在分布式系统当中,服务之间调用关系会随着业务的发展而变的复杂,一个服务可能依赖多个服务,服务之间层层依赖;一个服务的瘫痪可能导致整个系统的崩溃。

与电闸思想类似:每栋房子,每户都安装了电闸,电闸的作用是保证有一家电路出现短路时,电闸进行断电跳闸的操作,这样不至于导致整栋楼用电瘫痪。

2、新建hystrix模块

2.1 pom文件

4.0.0
com.cntaiping.tpa
hystrix
0.0.1-SNAPSHOT
jar
hystrix
Demo project for Spring Boot
com.cntaiping.tpa
cloud
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.cloud
spring-cloud-starter-netflix-hystrix

2.2 配置文件application.properties

#服务名称spring.application.name=consumer-hystrix#端口号server.port=8505#在注册中心中进行注册eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/

2.3 Application入口

package com.cntaiping.tpa.hystrix;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableHystrixpublic class HystrixApplication {    public static void main(String[] args) {        SpringApplication.run(HystrixApplication.class, args);    }    @Bean    @LoadBalanced    RestTemplate restTemplate() {        return new RestTemplate();    }}

2.4 服务消费类

在hello方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串

package com.cntaiping.tpa.hystrix.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class HelloService {    @Autowired    RestTemplate restTemplate;    @HystrixCommand(fallbackMethod = "error")    public String hello(String name) {        return restTemplate.getForObject("http://producer/get?name="+name, String.class);    }    public String error(String name) {        return "hi,"+name+",sorry,error!";    }}
package com.cntaiping.tpa.hystrix.controller;import com.cntaiping.tpa.hystrix.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @Autowired    HelloService helloService;    @GetMapping(value = "/hello")    public String hello(@RequestParam String name) {        return helloService.hello( name );    }}

3、运行效果

(1)注册中心

在这里插入图片描述
(2)consumer-hystrix模块

在这里插入图片描述

(3)测试熔断

将服务提供方producer的两个实例进程(8700和8701)关闭,再次运行http://localhost:8505/hello?name=chengyq

hi,chengyq,sorry,error!

在这里插入图片描述

这就说明当producer模块不可用的时候,consumer-hystrix调用producer的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

转载地址:http://ggvab.baihongyu.com/

你可能感兴趣的文章
Insecure default in Elasticsearch enables remote code execution
查看>>
how to use this bugs unserialize()
查看>>
PHP5 Globals Vulnerability
查看>>
关于php包含Apache日志的随想
查看>>
Grep与web漏洞挖掘
查看>>
正则表达式使用详解
查看>>
引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法(新手推荐)
查看>>
编写不受魔术引号影响的php应用
查看>>
PHP开发安全设置
查看>>
Php Endangers - Remote Code Execution
查看>>
变量的变量,PHP和你
查看>>
PROC系列之四---/proc/loadavg
查看>>
某大型网站的内核TCP/ip优化脚本
查看>>
Defeating SSL using SSLStrip (Marlinspike Blackhat)
查看>>
大型网站数据库架构
查看>>
rdp 安全策略
查看>>
Threat Intelligence Quotient Test
查看>>
Cisco路由器上防止DDOS的一些建议
查看>>
系统安全防护之UNIX下入侵检测方法
查看>>
域控渗透技巧
查看>>