簡介
spring cloud提供了hystrix容錯庫用以在服務(wù)不可用時,對配置了斷路器的方法實行降級策略,臨時調(diào)用備用方法。這篇文章將創(chuàng)建一個產(chǎn)品微服務(wù),注冊到eureka服務(wù)注冊中心,然后我們使用web客戶端訪問/products api來獲取產(chǎn)品列表,當(dāng)產(chǎn)品服務(wù)故障時,則調(diào)用本地備用方法,以降級但正常提供服務(wù)。
基礎(chǔ)環(huán)境
- jdk 1.8
- maven 3.3.9
- intellij 2018.1
git:項目源碼
添加產(chǎn)品服務(wù)
在intellij中創(chuàng)建一個新的maven項目,使用如下配置
- groupid: cn.zxuqian
- artifactid: productservice
然后在pom.xml中添加如下代碼:
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<?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:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>cn.zxuqian</groupid> <artifactid>productservice</artifactid> <version> 1.0 -snapshot</version> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.0 . 1 .release</version> <relativepath/> </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-eureka-client</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-config</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.m9</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>https: //repo.spring.io/libs-milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> </project> |
我們繼續(xù)使用了spring-cloud-starter-netflix-eureka-client以使產(chǎn)品服務(wù)自動注冊到eureka服務(wù)中。然后還使用了spring-cloud-starter-config讀取配置服務(wù)中心的配置文件。這個項目只是一個簡單的spring web項目。
在src/main/resources下創(chuàng)建bootstrap.yml文件,添加如下內(nèi)容:
1
2
3
4
5
6
|
spring: application: name: product-service cloud: config: uri: http: //localhost:8888 |
在配置中心的git倉庫中創(chuàng)建product-service.yml文件 添加如下配置并提交:
1
2
|
server: port: 8081 |
此配置指定了產(chǎn)品服務(wù)的端口為8081。接著創(chuàng)建application類,添加如下代碼:
1
2
3
4
5
6
7
8
9
10
11
|
package cn.zxuqian; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; @enablediscoveryclient @springbootapplication public class application { public static void main(string[] args) { springapplication.run(application. class , args); } } |
@enablediscoveryclient注解將指示spring cloud自動把本服務(wù)注冊到eureka。最后創(chuàng)建cn.zxuqian.controllers.productcontroller控制器,提供/products api,返回示例數(shù)據(jù):
1
2
3
4
5
6
7
8
9
10
11
12
|
package cn.zxuqian.controllers; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class productcontroller { @requestmapping ( "/products" ) public string productlist() { return "外套,夾克,毛衣,t恤" ; } } |
配置web客戶端
打開我們之前創(chuàng)建的web項目,在pom.xml中新添hystrix依賴:
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-hystrix</artifactid> </dependency> |
然后更新application類的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package cn.zxuqian; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.web.client.resttemplatebuilder; import org.springframework.cloud.client.circuitbreaker.enablecircuitbreaker; import org.springframework.cloud.client.discovery.enablediscoveryclient; import org.springframework.context.annotation.bean; import org.springframework.web.client.resttemplate; @enablecircuitbreaker @enablediscoveryclient @springbootapplication public class application { public static void main(string[] args) { springapplication.run(application. class , args); } @bean public resttemplate rest(resttemplatebuilder builder) { return builder.build(); } } |
這里使用@enablecircuitbreaker來開啟斷路器功能,然后還添加了一個rest方法并使用@bean注解。這部分屬于spring依賴注入功能,使用@bean標(biāo)記的方法將告訴如何初始化此類對象,比如本例中就是使用resttemplatebuilder來創(chuàng)建一個resttemplate的對象,這個稍后在使用斷路器的service中用到。
創(chuàng)建cn.zxuqian.service.productservice類,并添加如下代碼:
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
|
package cn.zxuqian.services; import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand; import org.springframework.beans.factory.annotation.autowired; import org.springframework.cloud.client.serviceinstance; import org.springframework.cloud.client.discovery.discoveryclient; import org.springframework.stereotype.service; import org.springframework.web.client.resttemplate; import java.util.list; @service public class productservice { private final resttemplate resttemplate; @autowired private discoveryclient discoveryclient; public productservice(resttemplate resttemplate) { this .resttemplate = resttemplate; } @hystrixcommand (fallbackmethod = "backupproductlist" ) public string productlist() { list<serviceinstance> instances = this .discoveryclient.getinstances( "product-service" ); if (instances != null && instances.size() > 0 ) { return this .resttemplate.getforobject(instances.get( 0 ).geturi() + "/products" , string. class ); } return "" ; } public string backupproductlist() { return "夾克,毛衣" ; } } |
之所以要創(chuàng)建一個service類,是因為hystrix只能在標(biāo)記為@service或@component的類中使用,這樣才能夠正常使用spring context所提供的api。這個以后深入spring時再作說明。
使用@hystrixcommand注解后,hystrix將監(jiān)控被注解的方法即productlist(底層使用proxy包裝此方法以此實現(xiàn)監(jiān)控),一旦此方法的錯誤累積到一定門檻的時候,就會啟動斷路器,后續(xù)所有調(diào)用productlist方法的請求都會失敗,而會臨時調(diào)用fallbackmethod指定的方法backupproductlist(),然后當(dāng)服務(wù)恢復(fù)正常時,斷路器就會關(guān)閉。
我們還在此類中用了discoveryclient用以尋找產(chǎn)品服務(wù)的uri地址,使用產(chǎn)品服務(wù)的spring.application.name配置項的值,即product-service作為serviceid傳給discoveryclient.getinstances()方法,然后會返回一個list,因為目前我們只有一個產(chǎn)品服務(wù)啟動著,所以只需要取第一個實例的uri地址即可。
然后我們使用resttemplate來訪問產(chǎn)品服務(wù)的api,注意這里使用了spring的構(gòu)造方法注入,即之前我們用@bean注解的方法會被用來初始化resttemplate變量,不需我們手動初始化。resttemplate類提供了getforobject()方法來訪問其它rest api并把結(jié)果包裝成對象的形式,第一個參數(shù)是要訪問的api的uri地址,第二參數(shù)為獲取的結(jié)果的類型,這里我們返回的是string,所以傳給他string.class。
backupproductlist()方法返回了降級后的產(chǎn)品列表信息。
最后創(chuàng)建一個控制器cn.zxuqian.controllers.productcontroller并添加如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package cn.zxuqian.controllers; import cn.zxuqian.services.productservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class productcontroller { @autowired private productservice productservice; @requestmapping ( "/products" ) public string productlist() { return productservice.productlist(); } } |
這里使用productservice為/products路徑提供數(shù)據(jù)。
測試
首先,我們使用spring-boot:run插件啟動配置中心服務(wù),config-server,然后啟動eureka-server,再啟動product-service,最后啟動web客戶端,稍等片刻待eureka服務(wù)注冊成功之后訪問http://localhost:8080/products,正常的情況下會得到外套,夾克,毛衣,t恤結(jié)果,然后我們關(guān)閉product-service,之后再訪問同樣的路徑,會得到降級后的結(jié)果:夾克,毛衣
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/fengqiuzhihua/article/details/80199056