淺談Ribbon、Feign和OpenFeign的區(qū)別
Ribbon
Ribbon 是 Netflix開源的基于HTTP和TCP等協(xié)議負(fù)載均衡組件
Ribbon 可以用來做客戶端負(fù)載均衡,調(diào)用注冊中心的服務(wù)
Ribbon的使用需要代碼里手動調(diào)用目標(biāo)服務(wù),請參考官方示例:https://github.com/Netflix/ribbon
Feign
Feign是Spring Cloud組件中的一個(gè)輕量級RESTful的HTTP服務(wù)客戶端
Feign內(nèi)置了Ribbon,用來做客戶端負(fù)載均衡,去調(diào)用服務(wù)注冊中心的服務(wù)。
Feign的使用方式是:使用Feign的注解定義接口,調(diào)用這個(gè)接口,就可以調(diào)用服務(wù)注冊中心的服務(wù)
Feign支持的注解和用法請參考官方文檔:https://github.com/OpenFeign/feign
Feign本身不支持Spring MVC的注解,它有一套自己的注解
OpenFeign
OpenFeign是Spring Cloud 在Feign的基礎(chǔ)上支持了Spring MVC的注解,如@RequesMapping等等。
OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,
并通過動態(tài)代理的方式產(chǎn)生實(shí)現(xiàn)類,實(shí)現(xiàn)類中做負(fù)載均衡并調(diào)用其他服務(wù)。
// user-api 子項(xiàng)目 public interface SysUserResource { @GetMapping("/test") Object getUser(); } // user-client 子項(xiàng)目 , 依賴了user-api 子項(xiàng)目 // 其他業(yè)務(wù)模塊可以直接依賴此模塊,通過調(diào)用接口即可完成服務(wù)的遠(yuǎn)程調(diào)用,open-feign會對此類做動態(tài)代理 // name = "user-center" 是被調(diào)用的服務(wù)實(shí)例名稱 @FeignClient(name = "user-center") public interface SysUserResourceClient extends SysUserResource { } // user-impl 子項(xiàng)目 @RestController public class SysUserResourceImpl implements SysUserResource { @Override Object getUser(){ // do something } } // role-impl 子項(xiàng)目 , 依賴了 user-client 子項(xiàng)目 @RestController public class SysRoleResourceImpl implements SysRoleResource { @Resource private SysUserResource sysUserResource; @Override Object test(){ sysUserResource.getUser(); } }
需要注意,@RequesMapping不能在類名上與@FeignClient同時(shí)使用
OpenFeign服務(wù)接口調(diào)用(與Feign的區(qū)別)
1簡介
Feign是聲明式的web service客戶端,它讓微服務(wù)之間的調(diào)用變得更簡單了,類似controller調(diào)用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign時(shí)提供負(fù)載均衡的http客戶端。
之前已經(jīng)創(chuàng)建好了用戶,訂單,商品微服務(wù),這三個(gè)微服務(wù)是互相隔離的,那么微服務(wù)和微服務(wù)之間如何互相調(diào)用呢,顯然三個(gè)微服務(wù)都可以采用http通信,也就是restTemplate進(jìn)行互相訪問,但是這種方式對參數(shù)傳遞和使用都不是很方便,所以棄用此方式。
采用feign進(jìn)行服務(wù)之間的調(diào)用,可以簡化調(diào)用流程,真正感覺到是在同一個(gè)項(xiàng)目中調(diào)用另一個(gè)類的方法的歡快感,類似controller調(diào)用service。
Feign旨在使編寫Java Http客戶端變得更容易。 前面在使用Ribbon+RestTemplate時(shí),利用RestTemplate對http請求的封裝處理,形成了一套模版化的調(diào)用方法。但是在實(shí)際開發(fā)中,由于對服務(wù)依賴的調(diào)用可能不止一處,往往一個(gè)接口會被多處調(diào)用,所以通常都會針對每個(gè)微服務(wù)自行封裝一些客戶端類來包裝這些依賴服務(wù)的調(diào)用。所以,F(xiàn)eign在此基礎(chǔ)上做了進(jìn)一步封裝,由他來幫助我們定義和實(shí)現(xiàn)依賴服務(wù)接口的定義。在Feign的實(shí)現(xiàn)下,我們只需創(chuàng)建一個(gè)接口并使用注解的方式來配置它(以前是Dao接口上面標(biāo)注Mapper注解,現(xiàn)在是一個(gè)微服務(wù)接口上面標(biāo)注一個(gè)Feign注解即可),即可完成對服務(wù)提供方的接口綁定,簡化了使用Spring cloud Ribbon時(shí),自動封裝服務(wù)調(diào)用客戶端的開發(fā)量。
2使用步驟
1新建cloud-consumer-order80-fegin 2POM
<?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"> <parent> <artifactId>cloud1000</artifactId> <groupId>com.zs.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-order80-feign</artifactId> <!--openfeign--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.zs.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project>
3YAML
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
4主啟動類
@SpringBootApplication @EnableFeignClients public class OrderMain80Feign { public static void main(String[] args) { SpringApplication.run(OrderMain80Feign.class, args); } }
5業(yè)務(wù)類
@Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { @GetMapping(value = "/payment/get/{id}") public CommonResult<?> getPaymentById(@PathVariable("id") Long id); }
@RestController public class OrderFeignController { @Autowired private PaymentFeignService paymentFeignService; @GetMapping(value = "/consumer/payment/get/{id}") public CommonResult<?> getPaymentById(@PathVariable("id") Long id) { return paymentFeignService.getPaymentById(id); } }
3OpenFeign超時(shí)控制
3.1服務(wù)提供方8001故意寫暫停程序
@GetMapping(value = "/payment/feign/timeout") public String paymentFeignTimeout(){ try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();} return serverPort; }
3.2服務(wù)消費(fèi)方80添加超時(shí)方法PaymentFeignService
@GetMapping(value = "/payment/feign/timeout") public String paymentFeignTimeout();
3.3服務(wù)消費(fèi)方80添加超時(shí)方法OrderFeignController
@GetMapping(value = "/consumer/payment/feign/timeout") public String paymentFeignTimeout(){ return paymentFeignService.paymentFeignTimeout(); }
3.4測試
http://localhost/consumer/payment/feign/timeout
3.5YML文件里需要開啟OpenFeign客戶端超時(shí)控制
ribbon: #根 ReadTimeout: 5000 ConnectTimeout: 5000
4OpenFeign日志打印功能
@Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
logging: level: com.zs.springcloud.service.PaymentFeignService: debug
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進(jìn)行校驗(yàn)的示例代碼
這篇文章主要介紹了SpringBoot結(jié)合JSR303對前端數(shù)據(jù)進(jìn)行校驗(yàn)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09泛型的類型擦除后fastjson反序列化時(shí)如何還原詳解
這篇文章主要為大家介紹了泛型的類型擦除后fastjson反序列化時(shí)如何還原詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔
這篇文章主要介紹了Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08