Spring Cloud如何遠程調(diào)用Feign和整合負載均衡Ribbon熔斷器Hystrix),針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站托管維護、企業(yè)網(wǎng)站設(shè)計、龍口網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Feign 是spring cloud全家桶一個成員,用于遠程調(diào)用。
特點:聲明式、模板化HTTP客戶端。使遠程調(diào)用,在使用時,感覺像“本地方法”
步驟一:修改pom文件,添加Feign依賴
步驟二:修改啟動類,添加開啟Feign注解
步驟三:編寫Feign接口,完成遠程調(diào)用,取代dao層
步驟四:修改controller, 將調(diào)用dao修改成feign
步驟一:修改pom文件,添加Feign依賴
org.springframework.cloud spring-cloud-starter-openfeign
步驟二:修改啟動類,添加開啟Feign注解
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; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableHystrix //開啟熔斷器 @EnableFeignClients //開啟Feign客戶端 public class Client4Application { public static void main(String[] args) { SpringApplication.run(Client4Application.class,args); } }
步驟三:編寫Feign接口,完成遠程調(diào)用,取代dao層
@FeignClient(value="服務(wù)名",path="controller前綴") public interface 接口名{ //與controller方法一致 }
package com.czxy.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest; @FeignClient(value="service4",path="/test") public interface DataFeign { @GetMapping public ResponseEntitytest() ; }
步驟四:修改controller, 將調(diào)用dao修改成feign
package com.czxy.controller; import com.czxy.dao.DataDao; import com.czxy.feign.DataFeign; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/data") public class DataController { @Resource //private DataDao dataDao; private DataFeign dataFeign; @GetMapping public ResponseEntitydata(){ //return dataDao.data(); return dataFeign.test(); } }
Spring Cloud 完成遠程調(diào)用,并進行負載均衡
方式1:使用RestTemplate,并添加額外注解 @LoadBalanced
方式2:使用Feign,集成Ribbon,自動負載均衡
如果需要單獨給服務(wù)配置ribbon,可以參考(可選)
#負載均衡器策略配置 service4: ribbon: #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #隨機 #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.BestAvailableRule #并發(fā)最少 NFLoadBalancerRuleClassName : com.netflix.loadbalancer.WeightedResponseTimeRule #請求時間權(quán)重 ConnectTimeout: 250 # Ribbon的連接超時時間 ReadTimeout: 1000 # Ribbon的數(shù)據(jù)讀取超時時間 OkToRetryOnAllOperations: true # 是否對所有操作都進行重試 MaxAutoRetriesNextServer: 1 # 切換實例的重試次數(shù) MaxAutoRetries: 1 # 對當(dāng)前實例的重試次數(shù)
步驟一:修改yml文件,開啟feign熔斷機制
步驟二:創(chuàng)建feign接口實現(xiàn)類,提供備選方案
步驟三:feign調(diào)用,指定fallback確定備選方案
步驟一:修改yml文件,開啟feign熔斷機制
feign: hystrix: enabled: true #開啟feign熔斷
步驟二:創(chuàng)建feign接口實現(xiàn)類,提供備選方案
package com.czxy.feign; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @Component public class DataFeignFallback implements DataFeign { @Override public ResponseEntitytest() { return ResponseEntity.ok("feign備選方案"); } }
步驟三:feign調(diào)用,指定fallback確定備選方案
@FeignClient(value="服務(wù)名",path="前綴路徑",fallback=備選方案類.class) public interface 接口名 {
package com.czxy.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest; @FeignClient(value="service4",path="/test",fallback=DataFeignFallback.class) public interface DataFeign { @GetMapping public ResponseEntitytest() ; }
關(guān)于Spring Cloud如何遠程調(diào)用Feign和整合負載均衡Ribbon熔斷器Hystrix)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。