前言
之前沒有覺得OpenApi有什麼特別了不得的,直到參加了花旗銀行的一次面試,對方部門老總對OpenApi是非常重視,認爲可以極大提升效率;後來在eBay,隨着微服務化的推行,應用之間RESTful調用指數級增長,應用內調用接口的代碼佔了很大一部分,複用程度不高,比如一個基礎應用A被10個上層應用調用,各寫各的後果如下:
- 10個應用有的用okHttp有的用apache http,不統一
- 單元測試覆蓋率要達到80%,意味着相同功能加10倍UT
- 缺乏重試、異常處理等機制
後來試用了OpenApi,將如上10個應用的代碼,代替爲OpenApi生成的jar包(jar包可以統一管理,10個應用引入即可),採用的技術統一,UT只要一份,代碼質量有保證!
OpenApi和swagger
OpenApi解析swagger的接口的規程,自動生成java類庫,非常方便!
網易鏈接口調用關係.md 裏面用到了自己編寫的okHttpClient,但是如果能夠自動生成,豈不是很方便?
swagger.md 原理是利用swagger.json,裏面有詳細的返回的類及哪些接口
20190924: 跑通了整體流程,最大改動:submitTime原來是offsetTime類型的,後來我改爲string還是不過,後來乾脆取消對這個字段的讀取,就通過了;另外發現一個jobType的問題,是haowen在qm上遺漏了,已經fix了
具體操作 hands-on
- 先把swagger.json 下載下來
- 在運行
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i workswagger.json -g java -o /var/tmp/javaClient4Workflow
上傳到ebay的qa庫中,小竅門是1.0-SNAPSHOT,否則報錯
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ openapi-workflow-client ---
Downloading: https://ebaycentral.qa.ebay.com/content/repositories/snapshots/com/ebay/marketing/opclients/openapi-workflow-client/1.0-SNAPSHOT/maven-metadata.xml
Uploading: https://ebaycentral.qa.ebay.com/content/repositories/snapshots/com/ebay/marketing/opclients/openapi-workflow-client/1.0-SNAPSHOT/openapi-workflow-client-1.0-20190918.062345-1.jar
Uploaded: https://ebaycentral.qa.ebay.com/content/repositories/snapshots/com/ebay/marketing/opclients/openapi-workflow-client/1.0-SNAPSHOT/openapi-workflow-client-1.0-20190918.062345-1.jar (102 KB at 45.3 KB/sec)
Uploading: https://ebaycentral.qa.ebay.com/content/repositories/snapshots/com/ebay/marketing/opclients/openapi-workflow-client/1.0-SNAPSHOT/openapi-workflow-client-1.0-20190918.062345-1.pom
Uploaded: https://ebaycentral.qa.ebay.com/content/repositories/snapshots/com/ebay/marketing/opclients/openapi-workflow-client/1.0-SNAPSHOT/openapi-workflow-client-1.0-20190918.062345-1.pom (11 KB at 15.3 KB/sec)
平常做spring-boot項目在服務間相互調用時一般都是RestTemplate,有了這個就不用了,可以生成client端的jar包,直接調用jar包中的方法就會進行服務調用
命令
git clone https://github.com/openapitools/openapi-generator.git
cd openapi-generator
mvn clean install
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i modules/openapi-generator/src/test/resources/2_0/petstore.yaml \
-l jaxrs-cxf-client \
-o /var/tmp/openapi-generator/jaxrs-cxf-client
openapi-generator-cli help
用list命令嘗試
config-help
meta
validate (先驗證一下給的petstore的例子)
openapi-generator validate -i petstore-v3.0.yaml
$ java -jar openapi-generator-cli.jar help
usage: openapi-generator-cli <command> [<args>]
The most commonly used openapi-generator-cli commands are:
config-help Config help for chosen lang
generate Generate code with the specified generator.
help Display help information
list Lists the available generators
meta MetaGenerator. Generator for creating a new template set and configuration for Codegen. The output will be based on the language you specify, and includes default templates to include.
validate Validate specification
version Show version information