feign接口自動生成工具


 

最近發現開發spring cloud時,編寫feign接口是一件痛苦的事,不僅要編寫feign接口,還有fallback、請求參數和返回值等,大量重復工作,很浪費時間。

於是便想到可以編寫工具自動生成feign接口。

實現起來並不復雜,就是把提供方工程的類加載進來,掃描controller和model生成meta信息,使用模板生成源代碼,保存到本地或集中管理feign接口源代碼的服務。

本文就簡單介紹一下這個自動化工具的使用和核心技術。

 

spring boot工程feign接口自動工具,含maven插件和cli工具。

 

一、下載編譯

git clone https://gitee.com/xuguofeng2020/feign-generator.git

cd feign-generator

mvn install

 

二、Maven插件版

您可以把插件引入到工程中,插件會在編譯階段掃描controller類,生成feign接口元數據並將其發送給中央管理server服務進行集中管理,或在本地保存feign源代碼。

 

1、插件依賴

<plugin>
  <groupId>org.net5ijy.cloud</groupId>
  <artifactId>feign-generator-plugin</artifactId>
  <version>1.0.0</version>
  <executions>
    <execution>
      <phase>compile</phase>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <scanPackage>org.net5ijy.mall.account.controller</scanPackage>
        <modelScanPackage>org.net5ijy.mall.account</modelScanPackage>
        <manageServerUrl>http://localhost:10001/api/v1/feign/generate</manageServerUrl>
        <!-- <local>true</local> -->
      </configuration>
    </execution>
  </executions>
</plugin>

 

2、參數說明

參數 說明 默認值
scanPackage 配置掃描controller的基礎包名 org.net5ijy.cloud
modelScanPackage 配置掃描參數、返回值的基礎包名。如果接口參數、返回值類型不在這個包下面,不會加入到model元數據中 org.net5ijy.cloud
manageServerUrl 集中管理服務接收元數據的接口地址,如果local配置為true,該參數就不會起作用 -
local 本地保存 false

 

3、Maven編譯工程

mvn clean compile

 

 

三、Cli命令行版

1、命令示例

cd feign-generator-cli

copy scripts\run.bat target

cd target

## 本地保存feign接口源代碼
run.bat D:\workspace\net5ijy-mall-accountservice\target\net5ijy-mall-accountservice.jar org.net5ijy.mall.account.controller org.net5ijy.mall.account

# 或者將feign接口元數據發送給集中管理服務
run.bat D:\workspace\net5ijy-mall-accountservice\target\net5ijy-mall-accountservice.jar org.net5ijy.mall.account.controller org.net5ijy.mall.account remote http://localhost:10001/api/v1/feign/generate

 

2、參數說明

參數 說明 默認值
1 工程jar路徑 -
2 配置掃描controller的基礎包名 -
3 配置掃描參數、返回值的基礎包名。如果接口參數、返回值類型不在這個包下面,不會加入到model元數據中 -
4 如何保存feign接口元數據,local - 本地保存,remote - 發送給中央管理server服務 local
5 集中管理服務接收元數據的接口地址 http://localhost:10001/api/v1/feign/generate

 

 

四、集中管理服務

用於集中管理工程、feign接口源代碼、model源代碼,提供工程查看、feign接口源代碼下載、feign接口及fallback類源代碼在線查看、model源代碼在線查看等功能。

 

1、示例服務

數據庫配置,編輯application.yml文件,修改datasource相關配置:

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql-dba-1:3306/feign
    username: system
    password: ******
    druid:
      filters: stat
      maxActive: 5
      initialSize: 5
      maxWait: 60000
      minIdle: 1
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 5

 

2、打包及運行

cd feign-generator-server-demo

mvn clean package

cd target

java -jar feign-generator-server-demo.jar

 

3、工程管理頁面

 

 

 

4、FeignClient管理頁面

 

 

 

5、Model管理頁面

 

 

6、集成

引入feign-generator-server依賴:

<!-- feign-generator-server -->
<dependency>
  <groupId>org.net5ijy.cloud</groupId>
  <artifactId>feign-generator-server</artifactId>
  <version>1.0.0</version>
</dependency>

 

在啟動類配置組件掃描:

@SpringBootApplication(scanBasePackages = {"org.net5ijy.cloud.feign.demo", "org.net5ijy.cloud.plugin.feign.server"})
@MapperScan({"org.net5ijy.cloud.plugin.feign.server.mapper"})

 

feign-generator-server會提供查看工程、feign接口、model類的相關接口,如下:

接口 請求方式 作用 參數
/api/v1/feign/{id} GET 根據ID查詢工程信息 -
/api/v1/feign/search GET 根據groupId、projectName分頁查詢工程 groupId、projectName、page、size
/api/v1/feign/clients GET 根據工程ID分頁查詢feign client projectId、page、size
/api/v1/feign/client/{id} GET 根據feign client id查詢client -
/api/v1/feign/models GET 根據工程ID分頁查詢model projectId、page、size
/api/v1/feign/model/{id} GET 根據ID查詢model -
/api/v1/feign/download/{id} POST 下載工程的feign client源代碼zip文件 -
/api/v1/feign/project/all GET 獲取全部工程,用於下拉列表等功能 -

 

您可以在已有的管理平台開發前端,使用以上接口獲取后台數據進行展示。

 

五、核心組件

  • feign-generator-core - core組件提供封裝工程、feign client、model元數據的類,free marker模板解析器,controller掃描器,以及用於生成源代碼的free marker模板。

  • feign-generator-plugin - Maven插件。

  • feign-generator-server - 為集中管理服務提供接口能力。

  • feign-generator-cli - 命令行工具,提供的能力和maven插件基本相同。

  • feign-generator-server-demo - 集中管理服務示例。

 

六、核心技術

  • maven插件開發

  • 動態類加載

  • java反射技術

  • free marker模板技術

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM