摘要:為了保證服務(wù)高可用性,單個服務(wù)通常會進行集群部署。這時斷路器就派上用場了。當(dāng)對某個服務(wù)的調(diào)用的不可用達到一個閥值默認是秒次斷路器將會被自動被打開。斷路打開后,方法可以直接返回一個預(yù)先設(shè)置的固定值。
公眾號: java樂園
在微服務(wù)架構(gòu)中,根據(jù)業(yè)務(wù)需求拆分成一個個的微小服務(wù),然后服務(wù)與服務(wù)之間可以相互RPC遠程調(diào)用。在Spring Cloud可以使用RestTemplate+Ribbon或者Feign來進行RPC遠程調(diào)用。為了保證服務(wù)高可用性,單個服務(wù)通常會進行集群部署。由于網(wǎng)絡(luò)原因或者自身的原因,服務(wù)并不能保證百分之一百可用,如果服務(wù)方出現(xiàn)問題,調(diào)用這個服務(wù)就會出現(xiàn)線程阻塞,此時若有出現(xiàn)大量請求,導(dǎo)致服務(wù)方癱瘓。這時斷路器就派上用場了。
當(dāng)對某個服務(wù)的調(diào)用的不可用達到一個閥值(Hystric 默認是5秒20次) 斷路器將會被自動被打開。斷路打開后, fallback方法可以直接返回一個預(yù)先設(shè)置的固定值。
1、 新建項目sc-eureka-client-consumer-ribbon-hystrix,對應(yīng)的pom.xml文件
4.0.0 spring-cloud sc-eureka-client-consumer-ribbon 0.0.1-SNAPSHOT jar sc-eureka-client-consumer-ribbon http://maven.apache.org org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom import UTF-8 1.8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-ribbon org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-hystrix 2.0.1.RELEASE
備注:spring-cloud-starter-hystrix已經(jīng)在spring cloud 2.x標(biāo)注成過期。推薦使用spring-cloud-starter-netflix-hystrix
2、 新建spring boot 啟動類ConsumerApplication.java
package sc.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication @EnableEurekaClient @EnableHystrix public class ConsumerHystrixApplication { public static void main(String[] args) { SpringApplication.run(ConsumerHystrixApplication.class, args); } }
可以看出這個啟動類只是《服務(wù)發(fā)現(xiàn)&服務(wù)消費者Ribbon》的啟動類添加多了一個注解EnableHystrix(啟動斷路器)
3、 編寫服務(wù)類,并添加斷路器注解
package sc.consumer.service.impl; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.github.andrewoma.dexx.collection.ArrayList; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import sc.consumer.model.User; import sc.consumer.service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "getUserError") @Override public MapgetUser(Long id) { return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/getUser/{1}", Map.class, id); } public Map getUserError(Long id){ Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); User u = new User(); u.setId(-1L); u.setUserName("failName"); map.put("body", u); return map; } @HystrixCommand(fallbackMethod = "listUserError") @Override public Map listUser() { return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/listUser", Map.class); } public Map listUserError(){ Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", new ArrayList ()); return map; } @HystrixCommand(fallbackMethod = "addUserError") @Override public Map addUser(User user) { return restTemplate.postForObject("http://sc-eureka-client-provider:8200/user/addUser", user, Map.class); } public Map addUserError(User user){ Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", 0); return map; } @HystrixCommand(fallbackMethod = "updateUserError") @Override public Map updateUser(User user) { restTemplate.put("http://sc-eureka-client-provider:8200/user/updateUser",user); Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); return map; } public Map updateUserError(User user){ Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", 0); return map; } @HystrixCommand(fallbackMethod = "deleteUserError") @Override public Map deleteUser(Long id) { restTemplate.delete("http://sc-eureka-client-provider:8200/user/deleteUser/{id}", id); Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); return map; } public Map deleteUserError(Long id){ Map map = new HashMap (); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", 0); return map; } }
添加HystrixCommand注解,對應(yīng)的參數(shù)fallbackMethod值為當(dāng)方式服務(wù)方無法調(diào)用時,返回預(yù)設(shè)值得方法名。
4、 新建配置文件bootstrap.yml和application.yml
bootstrap.yml
server: port: 5600 application.yml spring: application: name: sc-eureka-client-consumer-ribbon-hystrix eureka: instance: hostname: 127.0.0.1 client: #由于該應(yīng)用為注冊中心,所以設(shè)置為false,代表不向注冊中心注冊自己 registerWithEureka: true #由于注冊中心的職責(zé)就是維護服務(wù)實例,它并不需要去檢索服務(wù),所以也設(shè)置為false fetchRegistry: true serviceUrl: defaultZone: http://127.0.0.1:5001/eureka/
5、 其他項目文件參加下圖
6、 分別啟動注冊中心sc-eureka-server和服務(wù)提供者sc-eureka-client-provider
7、 啟動sc-eureka-client-consumer-ribbon-hystrix,并驗證是否啟動成功
方式一:查看日志
方式二:查看注冊中心是否注冊成功
8、 驗證斷路器是否起作用
(1) 服務(wù)提供者正常時訪問:
http://127.0.0.1:5600/cli/user/getUser/4
正常返回數(shù)據(jù)庫里的數(shù)據(jù)
(2) 服務(wù)提供者關(guān)閉時訪問:
http://127.0.0.1:5600/cli/user/getUser/4
對比兩個返回的數(shù)據(jù),可以看出服務(wù)提供者關(guān)閉時,返回的數(shù)據(jù)是在程序?qū)懰赖臄?shù)據(jù),如下圖:
其他方法可以自行按照以上方式進行驗證。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/76054.html
摘要:服務(wù)注冊中心一個服務(wù)注冊中心,所有的服務(wù)都在注冊中心注冊,負載均衡也是通過在注冊中心注冊的服務(wù)來使用一定策略來實現(xiàn)。在客戶端實現(xiàn)了負載均衡。 文章參考于史上最簡單的 SpringCloud 教程 | 終章 Spring Cloud 是一個微服務(wù)框架,與 Spring Boot 結(jié)合,開發(fā)簡單。將一個大工程項目,分成多個小 web 服務(wù)工程,可以分別獨立擴展,又可以共同合作。 環(huán)境 ...
摘要:多層服務(wù)調(diào)用常見于微服務(wù)架構(gòu)中較底層的服務(wù)如果出現(xiàn)故障,會導(dǎo)致連鎖故障。 Spring Cloud 體驗 簡介 Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)的一些工具,包括配置管理、服務(wù)發(fā)現(xiàn)、斷路器、路由、微代理、 事件總線、全局鎖、決策競選、分布式會話等等 基于Spring Boot,Spring Cloud將各公司成熟服務(wù)框架組合起來,通過Spring Boo...
摘要:當(dāng)服務(wù)宕機或者不可用時,即請求超時后會調(diào)用此方法。添加電影微服務(wù)啟動類電影微服務(wù)集成斷路器實現(xiàn)失敗快速響應(yīng),達到熔斷效果。 SpringCloud(第 014 篇)電影 Ribbon 微服務(wù)集成 Hystrix 斷路器實現(xiàn)失敗快速響應(yīng),達到熔斷效果 - 一、大致介紹 1、Hystrix 斷路器的原理很簡單,如同電力過載保護器。它可以實現(xiàn)快速失敗,如果它在一段時間內(nèi)偵測到許多類似的錯誤,...
摘要:公眾號樂園上編說了整合斷路器,這篇來看看如何整合斷路器,整合斷路器也是相對比較簡單的。默認已經(jīng)自帶斷路器,所以不需要像整合斷路器那樣需要在的啟動類添加注解。但是自帶斷路器并沒有打開,需要做些額外的配置。 公眾號: java樂園 上編說了《RestTemplate+Ribbon整合斷路器Hystrix》,這篇來看看如何Feign整合斷路器Hystrix,F(xiàn)eign整合斷路器Hystrix...
摘要:為了保證其高可用,單個服務(wù)又必須集群部署。為了解決這個問題,就出現(xiàn)斷路器模型。一斷路器簡介摘自官網(wǎng)已經(jīng)創(chuàng)建了一個名為的庫來實現(xiàn)斷路器模式。較底層的服務(wù)如果出現(xiàn)故障,會導(dǎo)致連鎖故障。當(dāng)對特定的服務(wù)的調(diào)用達到一個閥值是秒次斷路器將會被打開。 轉(zhuǎn)載請標(biāo)明出處: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 在微服務(wù)架構(gòu)中,我們將業(yè)務(wù)拆分成一個個的服務(wù),...
閱讀 2486·2021-11-17 09:33
閱讀 927·2021-10-13 09:40
閱讀 654·2019-08-30 15:54
閱讀 850·2019-08-29 15:38
閱讀 2489·2019-08-28 18:15
閱讀 2542·2019-08-26 13:38
閱讀 1900·2019-08-26 13:36
閱讀 2205·2019-08-26 11:36