問題描述
在搭建分布式應(yīng)用時,每個應(yīng)用通過nacos在網(wǎng)關(guān)出裝配了路由,我們希望網(wǎng)關(guān)也可以將所有的應(yīng)用的swagger界面聚合起來。這樣前端開發(fā)的時候只需要訪問網(wǎng)關(guān)的swagger就可以,而不用訪問每個應(yīng)用的swagger。
框架
springcloud+gateway+nacos+swagger
問題分析
swagger頁面是一個單頁面應(yīng)用,所有的顯示的數(shù)據(jù)都是通過和springfox.documentation.swagger.web.apiresponsecontroller進(jìn)行數(shù)據(jù)交互,首先通過/swagger-resources獲取swagger資源信息,獲取的信息格式如下:[{name: "default", url: "/v2/api-docs", swaggerversion: "2.0", location: "/v2/api-docs"}],其中name代表swagger生成的接口組的組名,如圖所示:
url
代表swagger接口組的詳細(xì)信息可以通過 localhost:8081/v2/api-docs來獲取,如下圖:
在網(wǎng)關(guān)處,如果訪問/swagger-resources能夠獲取到所有應(yīng)用的swagger的資源信息,那么我們的問題就可以解決了,所以我們需要做的是修改/swagger-resources接口的處理方式,使得這個接口能夠按照我們的需求返回swagger資源。
解決方案
我們首先在網(wǎng)關(guān)處引入swagger的相關(guān)依賴,然后實(shí)現(xiàn)一個獲取其他應(yīng)用的swagger資源的組件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
/** * 聚合各個服務(wù)的swagger接口 * * @author ksyzz * @since <pre>2019/04/09</pre> */ @component public class myswaggerresourceprovider implements swaggerresourcesprovider { /** * swagger2默認(rèn)的url后綴 */ private static final string swagger2url = "/v2/api-docs" ; /** * 網(wǎng)關(guān)路由 */ private final routelocator routelocator; /** * 網(wǎng)關(guān)應(yīng)用名稱 */ @value ( "${spring.application.name}" ) private string self; @autowired public myswaggerresourceprovider(routelocator routelocator) { this .routelocator = routelocator; } @override public list<swaggerresource> get() { list<swaggerresource> resources = new arraylist<>(); list<string> routehosts = new arraylist<>(); // 由于我的網(wǎng)關(guān)采用的是負(fù)載均衡的方式,因此我需要拿到所有應(yīng)用的serviceid // 獲取所有可用的host:serviceid routelocator.getroutes().filter(route -> route.geturi().gethost() != null ) .filter(route -> !self.equals(route.geturi().gethost())) .subscribe(route -> routehosts.add(route.geturi().gethost())); // 記錄已經(jīng)添加過的server,存在同一個應(yīng)用注冊了多個服務(wù)在nacos上 set<string> dealed = new hashset<>(); routehosts.foreach(instance -> { // 拼接url,樣式為/serviceid/v2/api-info,當(dāng)網(wǎng)關(guān)調(diào)用這個接口時,會自動通過負(fù)載均衡尋找對應(yīng)的主機(jī) string url = "/" + instance + swagger2url; if (!dealed.contains(url)) { dealed.add(url); swaggerresource swaggerresource = new swaggerresource(); swaggerresource.seturl(url); swaggerresource.setname(instance); resources.add(swaggerresource); } }); return resources; } } |
然后定義一個接口類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * swagger聚合接口,三個接口都是swagger-ui.html需要訪問的接口 * * @author ksyzz * @since <pre>2019/04/09</pre> */ @restcontroller @requestmapping ( "/swagger-resources" ) public class swaggerresourcecontroller { private myswaggerresourceprovider swaggerresourceprovider; @autowired public swaggerresourcecontroller(myswaggerresourceprovider swaggerresourceprovider) { this .swaggerresourceprovider = swaggerresourceprovider; } @requestmapping (value = "/configuration/security" ) public responseentity<securityconfiguration> securityconfiguration() { return new responseentity<>(securityconfigurationbuilder.builder().build(), httpstatus.ok); } @requestmapping (value = "/configuration/ui" ) public responseentity<uiconfiguration> uiconfiguration() { return new responseentity<>(uiconfigurationbuilder.builder().build(), httpstatus.ok); } @requestmapping public responseentity<list<swaggerresource>> swaggerresources() { return new responseentity<>(swaggerresourceprovider.get(), httpstatus.ok); } } |
然后啟動網(wǎng)關(guān),訪問 http://網(wǎng)關(guān)地址/swagger-ui.html,可以看到
在箭頭處,可以切換不同應(yīng)用的swagger頁面。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000018825356