SpringCloud 熔断器Hystrix 的使⽤及原理解析
什么是
Hystrix是Spring Cloud提供的⼀种带有熔断机制的框架,由于在系统中同⼀个操作会由多个不同的微服务来共同完成,所以微服务与微服务之间会由很多相互的调⽤,由于在分布式环境中经常会出现某个微服务节点故障的情况,所以会由调⽤失败发⽣,⽽熔断器的作⽤就是当出现远程调⽤失败的时候提供⼀种机制来保证程序的正常运⾏⽽不会卡死在某⼀次调⽤,类似Java程序中的try-catch结构,⽽只有当异常发⽣的时候才会进⼊catch的代码块。
使⽤Hystrix
建⽴提供服务的服务器
⾸先构建⼀个提供服务的服务器项⽬spring-cloud-server,在其l⽂件中加⼊如下依赖:<?xml version="1.0" encoding="UTF-8"?><project xmlns ="/POM/4.0.0" xsi ="/2001/XMLSchema-instance " schemaLocation ="/POM/4.0.0 /xsd/maven-4.0.0.xsd "> <modelVersion >4.0.0</modelVersion > <groupId &demine </groupId > <artifactId >spring-cl
oud-server </artifactId > <version >0.0.1-SNAPSHOT </version > <packaging >jar </packaging > <parent > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-starter-parent </artifactId > <version >2.0.5.RELEASE </version > <relativePath /> <!-- lookup parent from repository --> </parent > <properties > <project.build.sourceEncoding >UTF-8</project.build.sourceEncoding > <java.version >1.8</java.version > </properties > <dependencies > <dependency > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-starter-web </artifactId > </dependency > <dependency > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-starter-test </artifactId > <scope >test </scope > </dependency > </dependencies > <build > <plugins > <plugin > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-maven-plugin </artifactId > </plugin > </plugins > </build ></project >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
接着在项⽬中新建⼀个名为hello的package,然后在其中建⽴⼀个名为ServerApplication.java的⽂件,代码如下:
然后在resources⽬录下建⽴l⽂件,⾥⾯设置服务的端⼝号,如下:
整个项⽬的结构如下:
然后启动项⽬
建⽴客户端
接着建⽴连接服务器的客户端,⾸先建⽴⼀个名为hystrix-in-action的项⽬,项⽬的l的依赖如下:package hello ;import org .springframework .boot .SpringApplication ;import org .springframework .boot .autoconfigure .SpringBootApplication ;import org .springframework .web .bind .annotation .RequestMapping ;import org .springframework .web .bind .annotation .RestController ;@RestController @SpringBootApplication public class ServerApplication { public static void main (String [] args ) { SpringApplication .run (ServerApplication .class ); } @RequestMapping("/hello") public String hello () { return "hello from server"; }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21server : port : 8081
1
2<?xml version="1.0" encoding="UTF-8"?><project xmlns ="/POM/4.0.0" xsi ="/2001/XMLSchema-instance " schemaLocation ="/POM/4.0.0 /xsd/maven-4.0.0.xsd ">
1
2
3
schemaLocation ="/POM/4.0.0 /xsd/maven-4.0.0.xsd "> <modelVersion >4.0.0</modelVersion > <groupId &demine </groupId > <artifactId >hystrix-in-action </artifactId > <version >0.0.1-SNAPSHOT </version > <packaging >jar </packaging > <parent > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-starter-parent </artifactId > <version >2.0.5.RELEASE </version > <relativePath /> <!-- lookup parent from repository --> </parent > <properties > <project.build.sourceEncoding >UTF-8</project.build.sourceEncoding > <java.version >1.8</java.version > </properties > <dependencies > <dependency > <groupId >org.springframework.cloud </groupId > <artifactId >spring-cloud-starter-netflix-hystrix </artifactId > </dependency > <dependency > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-starter-web </artifactId > </dependency > <dependency > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-starter-test </artifactId > <scope >test </scope > </dependency > </dependencies > <dependencyManagement > <dependencies > <dependency > <groupId >org.springframework.cloud </groupId > <artifactId >spring-cloud-dependencies </artifactId > <version >Finchley.SR1</version > <type >pom </type > <scope >import </scope > </dependency > </dependencies > </dependencyManagement > <
build > <plugins > <plugin > <groupId >org.springframework.boot </groupId > <artifactId >spring-boot-maven-plugin </artifactId > </plugin > </plugins > </build > <repositories > <repository > <id >spring-milestones </id > <name >Spring Milestones </name > <url >repo.spring.io/libs-milestone </url > <snapshots > <enabled >false </enabled > </snapshots >
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
接着同样建⽴⼀个名为hello的package,然后在其中建⽴⼀个名为HystrixApplication.java的⽂件,代码如下:
然后建⽴⼀个名为HystrixService.java的⽂件,代码如下:
整个⼯程的⽬录如下: </snapshots > </repository > </repositories ></project >
68
69
70
71
72package hello ;import org .springframework .beans .factory .annotation .Autowired ;import org .springframework .boot .SpringApplication ;import org .springframework .boot .autoconfigure .SpringBootApplication ;import org .springframework .cloud flix .hystrix .EnableHystrix ;import org .springframework .web .bind .annotation .RequestMapping ;import org .springframework .web .bind .annotation .RestController ;@RestController @EnableHystrix @SpringBootApplication public class HystrixApplication { @Autowired HystrixService hystrixService ; public static void main (String [] args ) { SpringApplication .run (HystrixApplication .class ); } @RequestMapping("/hi") public String hi () { return hystrixService .hi (); }}
springboot结构1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package hello ;import com flix .hystrix .contrib .javanica .annotation .HystrixCommand ;import
org .springframework .stereotype .Service ;import org .springframework .web .client .RestTemplate ;@Service public class HystrixService { @HystrixCommand(fallbackMethod = "fallback") public String hi () { return new RestTemplate ().getForObject ("localhost:8081/hello", String .class ); } public String fallback () { return "fallback"; }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
然后启动项⽬
测试
我们可以通过IDEA⾃带的REST Client⼯具发起HTTP请求进⾏测试,当然也可以直接在浏览器中输⼊地址进⾏测试。
⾸先输⼊localhost:8081/hi,可以看到得到了“hello from server”的结果,接着关闭spring-cloud-server⼯程,再次发起请求,可以发现这次得到的结果是“fallback”。
结果解释
在HystrixService类中,我们使⽤了⽂章⼀开始所描述的熔断器机制,在hi()⽅法中加⼊了HystrixCommand注解,在注解中我们提供了⼀个名为“fallback”的字段,这个⽅法指向的是同⼀个类中的fallback()⽅法,当hi()⽅法调⽤失败的时候就会⾃动转⽽执⾏fallback()⽅法。
Hystrix源码解析
Hystrix在底层使⽤了Spring提供的切⾯技术。
在HystrixCommandAspect.java⽂件中可以看到定义的切⾯:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论