文章目录

引言

在上一节​​《淘东电商项目(14) -Apollo分布式配置中心管理application.yml》​​,主要讲解了把微服务项目的application.yml配置托管到Apollo分布式配置中心托管。

本文继续讲解分布式基础设施环境的搭建,主要讲解如何把网关的Swagger配置托管至Apollo配置中心。

1. Apollo管理网关项目的application.yml

step1: 网关项目添加apollo的maven依赖:

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>1.0.0</version>
</dependency>

step2: 启动类允许Apollo配置**@EnableApolloConfig**

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
@EnableApolloConfig
public class AppGateWay {
//省略此处代码。。。
}

step3: Apollo新建项目
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_分布式

step4: 配置文件转换后,粘贴至Apollo网关项目,并发布:
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_分布式_02

step5: 新增application.properties,内容如下:

app.id=app-basics-zuul
apollo.meta=http://192.168.10.130:8080

step6: 启动项目,可以看到网关服务正常启动:
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_json_03

2. Apollo管理Swagger

我们看一下网关服务的启动类代码,可以看到swagger的配置代码都是写死的,那么如何配置到Apollo呢?

《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_json_04

2.1 Apollo配置Swagger

可以自定义Swagger文档配置:​​guoranxinxian-shop.zuul.swagger.document​​,每个微服务的配置文件,以json数组的格式保存内容如下。

[
{
"name": "guoranxinxian-shop-service-member",
"location": "/guoranxinxian-shop-service-member/v2/api-docs",
"version": "2.0"
},
{
"name": "guoranxinxian-shop-service-weixin",
"location": "/guoranxinxian-shop-service-weixin/v2/api-docs",
"version": "2.0"
}
]

然后在Apollo添加配置:
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_spring_05
发布:
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_spring_06

2.2 项目代码配置动态获取Swagger信息

Apollo的配置已经完成了,那么代码该如何配置呢?下面直接贴上代码:

package com.guoranxinxian;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
@EnableApolloConfig
public class AppGateWay {

// 获取ApolloConfig
@ApolloConfig
private Config appConfig;

public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}

// 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider, ConfigChangeListener {
@Override
public List<SwaggerResource> get() {
// 开启监听,配置文件发生改变需要更改,onChange()回调
appConfig.addChangeListener(DocumentationConfig.this);
return resources();
}

@Override
public void onChange(ConfigChangeEvent configChangeEvent) {
get();
}

/**
* 从阿波罗服务器中获取resources
*
* @return
*/
private List<SwaggerResource> resources() {

List resources = new ArrayList<>();
// app-itmayiedu-order
// 网关使用服务别名获取远程服务的SwaggerApi
String swaggerDocJson = swaggerDocument();
JSONArray jsonArray = JSONArray.parseArray(swaggerDocJson);
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
String name = jsonObject.getString("name");
String location = jsonObject.getString("location");
String version = jsonObject.getString("version");
resources.add(swaggerResource(name, location, version));
}
return resources;
}

/**
* 获取swaggerDocument配置
*
* @return
*/
private String swaggerDocument() {
String property = appConfig.getProperty("guoranxinxian-shop.zuul.swagger.document", "");
return property;
}

private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}

}

3. 测试

step1:依次启动微信服务、会员服务、网关服务

step2:浏览器访问​​http://localhost/swagger-ui.html#/​​​,可以看到Swagger能正常显示两个微服务的文档,配置已经成功托管到了Apollo。
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_json_07
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_分布式_08
step3:在配置中心里移除掉会员服务,就是把配置信息改为如下,并发布:

[
{
"name": "guoranxinxian-shop-service-weixin",
"location": "/guoranxinxian-shop-service-weixin/v2/api-docs",
"version": "2.0"
}
]

《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_json_09

step4:浏览器访问​​http://localhost/swagger-ui.html#​​​ ,可以看到会员服务变化了
《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_分布式_10

4.总结

《果然新鲜》电商项目(15)- Apollo分布式配置中心管理Swagger_json_11