淺談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)文章
Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a
這篇文章主要介紹了Java框架學(xué)習(xí)Struts2復(fù)選框?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
解決Idea報(bào)錯(cuò)There is not enough memory
在使用Idea開發(fā)過程中,可能會遇到因內(nèi)存不足導(dǎo)致的閃退問題,出現(xiàn)"There is not enough memory to perform the requested operation"錯(cuò)誤時(shí),可以通過調(diào)整Idea的虛擬機(jī)選項(xiàng)來解決,方法是在Idea的Help菜單中選擇Edit Custom VM Options2024-11-11
Spring Cloud Feign的文件上傳實(shí)現(xiàn)的示例代碼
這篇文章主要介紹了Spring Cloud Feign的文件上傳實(shí)現(xiàn)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
SpringBoot項(xiàng)目實(shí)現(xiàn)短信發(fā)送接口開發(fā)的實(shí)踐
本文主要介紹了SpringBoot項(xiàng)目實(shí)現(xiàn)短信發(fā)送接口開發(fā)的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
idea創(chuàng)建springboot項(xiàng)目,Application.java不能運(yùn)行問題及解決
這篇文章主要介紹了idea創(chuàng)建springboot項(xiàng)目,Application.java不能運(yùn)行問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java多線程并發(fā)的指令重排序問題及volatile寫屏障原理詳解
這篇文章主要介紹了Java多線程并發(fā)的指令重排序問題及volatile寫屏障原理詳解,指令重排序是編譯器或處理器為了提高性能而對指令執(zhí)行順序進(jìn)行重新排列的優(yōu)化技術(shù),需要的朋友可以參考下2024-01-01
Eclipse開發(fā)JavaWeb項(xiàng)目配置Tomcat的方法步驟
本文主要介紹了Eclipse開發(fā)JavaWeb項(xiàng)目配置Tomcat的方法步驟,首先介紹eclipse開發(fā)JavaWeb項(xiàng)目需要配置的相關(guān)環(huán)境,使用tomcat軟件在本地搭建服務(wù)器,然后再在eclipse環(huán)境下配置tomcat,感興趣的可以了解一下2021-08-08

