偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

項目終于用上了動態(tài)Feign,真香!

開發(fā)
Feign在微服務(wù)框架中使得服務(wù)直接的調(diào)用變得很簡潔、簡單,而不需要再編寫Java Http調(diào)用其他微服務(wù)的接口。

Feign在微服務(wù)框架中使得服務(wù)直接的調(diào)用變得很簡潔、簡單,而不需要再編寫Java Http調(diào)用其他微服務(wù)的接口。

動態(tài)feign

對于fegin調(diào)用,我們一般的用法:為每個微服務(wù)都創(chuàng)建對應(yīng)的feignclient接口,然后為每個微服務(wù)的controller接口,一一編寫對應(yīng)的方法,去調(diào)用對應(yīng)微服務(wù)的接口。

例如下面這樣:

//system
@FeignClient(name = "system")
publicinterface SystemClient {
    @GetMapping("/system/test1")
    JsonResult test1(String test1);
    
    @GetMapping("/system/test2")
    JsonResult test2(String test2);
    
    ....
}

//user
@FeignClient(name = "user")
publicinterface UserClient {
    @GetMapping("/user/test1")
    JsonResult test1(String test1);
    
    @GetMapping("/user/test2")
    JsonResult test2(String test2);
    
    ....
}

這樣寫的話,可能會有些累贅,那么我們能不能創(chuàng)建一個動態(tài)的feign;當(dāng)調(diào)用sytem微服務(wù)的時候,傳遞一個feignclient的name為system進(jìn)去,然后定義一個通用的方法,指定調(diào)用的url,傳遞的參數(shù),就可以了呢?

答案是可以的?。。_^

定義一個通用的接口,通用的get,post方法:

public interface DynamicService {
    
    @PostMapping("{url}")
    Object executePostApi(@PathVariable("url") String url, @RequestBody Object params);

    @GetMapping("{url}")
    Object executeGetApi(@PathVariable("url") String url, @SpringQueryMap Object params);
}

executePostApi:(post方法)

  • url,表示你要調(diào)用微服務(wù)的接口url,一般來說是對應(yīng)controller接口的url;
  • params,為調(diào)用該接口所傳遞的參數(shù),這里加了@RequestBody,那對應(yīng)的controller接口,接收參數(shù)也需要加上該注解。

定義一個動態(tài)feignclient:

@Component
publicclass DynamicClient {

    @Autowired
    private DynamicFeignClientFactory<DynamicService> dynamicFeignClientFactory;

    public Object executePostApi(String feignName, String url, Object params) {
        DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName);
        return dynamicService.executePostApi(url, params);
    }

    public Object executeGetApi(String feignName, String url, Object params) {
        DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName);
        return dynamicService.executeGetApi(url, params);
    }
}

executePostApi:(post方法)

  • feignName,表示需要調(diào)用的微服務(wù)的名稱,一般對應(yīng)application.name,例如:system
  • url,表示你要調(diào)用微服務(wù)的接口url,一般來說是對應(yīng)controller接口的url;
  • params,為調(diào)用該接口所傳遞的參數(shù),這里加了@RequestBody,那對應(yīng)的controller接口,接收參數(shù)也需要加上該注解。

定義一個動態(tài)feignclient工廠類:

@Component
public class DynamicFeignClientFactory<T> {

    private FeignClientBuilder feignClientBuilder;

    public DynamicFeignClientFactory(ApplicationContext appContext) {
        this.feignClientBuilder = new FeignClientBuilder(appContext);
    }

    public T getFeignClient(final Class<T> type, String serviceId) {
        return this.feignClientBuilder.forType(type, serviceId).build();
    }
}

主要的作用:是幫我們動態(tài)的創(chuàng)建一個feignclient對象

好了,具體的操作步驟,就是上面所說的了!??!是不是很通用了呢?

通用是通用了,那怎么玩呢(如何使用)?

使用的方式,也是十分的簡單啦:^_^

DynamicClient dynamicClient = SpringUtil.getBean(DynamicClient.class);
Object result = dynamicClient.executePostApi("system", "/system/test", new HashMap<>());
System.out.println("==========>"+JSONObject.toJSONString(result));

先獲取到DynamicClient對象,然后直接調(diào)用executePostApi方法:

  • "system",表示調(diào)用微服務(wù)的名稱,一般對應(yīng)application.name
  • "/system/test",表示調(diào)用的url
  • new HashMap<>(),為需要傳遞的參數(shù)

好了,這樣就實現(xiàn)了一個通用版的feignclient,那我們就可以愉快的編寫代碼了?。?!^_^

責(zé)任編輯:趙寧寧 來源: 碼猿技術(shù)專欄
相關(guān)推薦

2022-12-19 08:32:57

項目Feign框架

2022-12-13 08:29:13

項目插入式注解

2024-09-14 09:59:04

2025-02-18 14:08:14

2021-04-23 08:29:47

SkyWalking監(jiān)控系統(tǒng)

2021-05-27 15:43:29

鴻蒙安卓和iOS

2023-09-14 15:15:36

2012-04-23 13:28:41

Voice AnsweSiriAppstore

2020-02-21 08:00:00

網(wǎng)頁廣告診斷

2020-04-09 08:29:50

編程語言事件驅(qū)動

2024-02-21 11:33:25

Serilog.NET日志庫

2018-04-24 14:12:29

蘋果iPhone手機

2022-01-25 10:40:30

Windows 10微軟升級

2020-07-23 10:51:29

NginxWebApache

2018-07-24 15:23:18

2020-10-14 14:06:32

iPhone 12

2023-12-16 12:47:59

2024-06-28 08:21:20

前端自動化部署

2025-02-08 13:40:00

DeepSeek模型AI
點贊
收藏

51CTO技術(shù)棧公眾號