Zuul网关服务集成阿波罗及Swagger文档配置

1.在Apollo配置中心创建项目,并导入配置。

在这里插入图片描述

[
    {
        "name": "app-member",
        "location": "/app-mayikt-member/v2/api-docs",
        "version": "2.0"
    },
    {
        "name": "app-weixin",
        "location": "/app-mayikt-weixin/v2/api-docs",
        "version": "2.0"
    }
]
2.在Zuul网关中,导入依赖。
<!--阿波罗配置中心依赖-->
        <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>
3.创建application.properties配置文件
app.id=app-mayikt-zuul #上图中的项目的appid
apollo.meta=http://172.20.10.3:8080  #阿波罗ip地址
4.在启动类上加上@EnableApolloConfig和@EnableSwagger2Doc
5.获取Swagger文档配置信息
    //获取ApolloConfig
    @ApolloConfig
    private Config config;
    
	// 添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            config.addChangeListener(new ConfigChangeListener() {
                // 开启监听,配置文件发生改变需要更改
                @Override
                public void onChange(ConfigChangeEvent configChangeEvent) {
                    get();
                }
            });
            return resources();
        }
        private List<SwaggerResource> resources(){
            List resources=new ArrayList();
            // 网关使用服务别名获取远程服务的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 = config.getProperty("mayikt.zuul.swaggerDocument", "");
            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;
        }

    }
6.阿波罗分布式配置中心监听
@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {
	@ApolloConfig
	private Config config;

	@Override
	public void run(String... args) throws Exception {
		config.addChangeListener(new ConfigChangeListener() {

			@Override
			public void onChange(ConfigChangeEvent changeEvent) {
				log.debug("####分布式配置中心监听#####" + changeEvent.changedKeys().toString());
			}
		});
	}

}