FeignClient接口封裝

Feign是一種聲明式、模塊化的HTTP客戶端。在SpringCloud中使用Feign,可以做到使用HTTP請(qǐng)求訪問遠(yuǎn)程服務(wù),就像調(diào)用本地方法一樣,開發(fā)者完全無(wú)感知在進(jìn)行HTTP請(qǐng)求調(diào)用。

1.接口定義

首先在單獨(dú)的module中定義接口:

@FeignClient(name ="com.johar.feign-test", path ="/user", url="${account-service-endpoint}")

public interface User {

@GetMapping("/{userId}")

public BaseResponsefindById(@PathVariable("userId")int userId);

? ? @PostMapping("/add")

public BaseResponsecreateUser(@RequestBody UserDto userDto);

}

FeignClient常用的注解屬性如下:

(1)name:指定FeignClient的名稱,如果項(xiàng)目使用了Ribbon,name屬性會(huì)作為微服務(wù)的名稱,用于服務(wù)發(fā)現(xiàn)。

(2)url:一般用于調(diào)試,手動(dòng)指定@FeignClient的調(diào)用地址。

(3)path:定義當(dāng)前的FeignClient的統(tǒng)一前綴。

(4)configuration:Feign的配置類,可以自定義Feign的Encoder,Decode,LogLevel,Contract。

(5)fallbckFactory:實(shí)現(xiàn)每個(gè)接口通用的容錯(cuò)邏輯,減少重復(fù)代碼。

2.實(shí)現(xiàn)定義的接口

在另外一個(gè)module中實(shí)現(xiàn)定義的接口:

@RestController

@RequestMapping("/user")

public class UserControllerimplements User {

@GetMapping("/{userId}")

@Override

? ? public BaseResponsefindById(@PathVariable("userId")int userId) {

UserDto userDto = UserDto.builder().age(29).id(userId).name("johar").sex(1).build();

? ? ? ? BaseResponse result =new BaseResponse();

? ? ? ? result.setData(userDto);

? ? ? ? return result;

? ? }

@PostMapping("/add")

@Override

? ? public BaseResponsecreateUser(@RequestBody UserDto userDto) {

userDto.setId(1);

? ? ? ? BaseResponse result =new BaseResponse();

? ? ? ? result.setData(userDto);

? ? ? ? return result;

? ? }

}

3.使用FeignClient調(diào)用接口

3.1 引入接口包

? <groupId>org.springframework.cloud

? <artifactId>spring-cloud-starter-netflix-ribbon

? <groupId>com.johar

? <artifactId>feign-api

? <version>0.0.1-SNAPSHOT

</dependency>

3.2 在啟動(dòng)類開啟FeignClients

@EnableFeignClients(basePackages = "com.johar.feignapi")

3.3 FeignClient接口調(diào)用

@SpringBootApplication

@EnableFeignClients(basePackages ="com.johar.feignapi")

public class FeignClientApplicationimplements CommandLineRunner {

public static void main(String[] args) {

SpringApplication.run(FeignClientApplication.class, args);

? ? }

@Autowired

? ? private Useruser;

? ? @Override

? ? public void run(String... args)throws Exception {

System.out.println(user.findById(1));

? ? ? ? System.out.println(user.createUser(UserDto.builder().sex(2).name("Anna").age(28).build()));

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 懶得處理樣式了, 將就著看吧. 官網(wǎng)地址: https://developer.android.com/topic...
    Reddington_604e閱讀 1,922評(píng)論 0 1
  • Feign是一個(gè)聲明式的Web服務(wù)客戶端。這使得Web服務(wù)客戶端的寫入更加方便 要使用Feign創(chuàng)建一個(gè)界面并對(duì)其...
    咔啡閱讀 453評(píng)論 0 1
  • Feign是一個(gè)聲明式的Web服務(wù)客戶端。這使得Web服務(wù)客戶端的寫入更加方便 要使用Feign創(chuàng)建一個(gè)界面并對(duì)其...
    咔啡閱讀 212評(píng)論 0 1
  • 1.音頻,1b 2.視頻,無(wú) 3.游戲和拓展:無(wú) 4.閱讀,英文,海尼曼6.牛津樹6,中文書:迪士尼寶寶自己讀、神...
    紫夜1606閱讀 222評(píng)論 0 0
  • 七月十四日晨 達(dá)武旦 風(fēng)從南邊吹來(lái) 帶著海的氣息 夢(mèng)境般—— 吻過(guò)我的臉龐 我想著那是你 鳥鳴打開的窗戶 飛翔著變...
    達(dá)武旦閱讀 115評(píng)論 0 1

友情鏈接更多精彩內(nèi)容