您好,登錄后才能下訂單哦!
Spring Cloud如何遠(yuǎn)程調(diào)用Feign和整合負(fù)載均衡Ribbon熔斷器Hystrix),針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
Feign 是spring cloud全家桶一個(gè)成員,用于遠(yuǎn)程調(diào)用。
特點(diǎn):聲明式、模板化HTTP客戶端。使遠(yuǎn)程調(diào)用,在使用時(shí),感覺(jué)像“本地方法”
步驟一:修改pom文件,添加Feign依賴
步驟二:修改啟動(dòng)類,添加開啟Feign注解
步驟三:編寫Feign接口,完成遠(yuǎn)程調(diào)用,取代dao層
步驟四:修改controller, 將調(diào)用dao修改成feign
步驟一:修改pom文件,添加Feign依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
步驟二:修改啟動(dòng)類,添加開啟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接口,完成遠(yuǎn)程調(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 ResponseEntity<String> test() ; }
步驟四:修改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 ResponseEntity<String> data(){ //return dataDao.data(); return dataFeign.test(); } }
Spring Cloud 完成遠(yuǎn)程調(diào)用,并進(jìn)行負(fù)載均衡
方式1:使用RestTemplate,并添加額外注解 @LoadBalanced
方式2:使用Feign,集成Ribbon,自動(dòng)負(fù)載均衡
如果需要單獨(dú)給服務(wù)配置ribbon,可以參考(可選)
#負(fù)載均衡器策略配置 service4: ribbon: #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #隨機(jī) #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.BestAvailableRule #并發(fā)最少 NFLoadBalancerRuleClassName : com.netflix.loadbalancer.WeightedResponseTimeRule #請(qǐng)求時(shí)間權(quán)重 ConnectTimeout: 250 # Ribbon的連接超時(shí)時(shí)間 ReadTimeout: 1000 # Ribbon的數(shù)據(jù)讀取超時(shí)時(shí)間 OkToRetryOnAllOperations: true # 是否對(duì)所有操作都進(jìn)行重試 MaxAutoRetriesNextServer: 1 # 切換實(shí)例的重試次數(shù) MaxAutoRetries: 1 # 對(duì)當(dāng)前實(shí)例的重試次數(shù)
步驟一:修改yml文件,開啟feign熔斷機(jī)制
步驟二:創(chuàng)建feign接口實(shí)現(xiàn)類,提供備選方案
步驟三:feign調(diào)用,指定fallback確定備選方案
步驟一:修改yml文件,開啟feign熔斷機(jī)制
feign: hystrix: enabled: true #開啟feign熔斷
步驟二:創(chuàng)建feign接口實(shí)現(xiàn)類,提供備選方案
package com.czxy.feign; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; @Component public class DataFeignFallback implements DataFeign { @Override public ResponseEntity<String> test() { 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 ResponseEntity<String> test() ; }
關(guān)于Spring Cloud如何遠(yuǎn)程調(diào)用Feign和整合負(fù)載均衡Ribbon熔斷器Hystrix)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。