目录
2.3 配置文件application.properties
2.4 网关项目开启阿波罗@EnableApolloConfig
2.5 自定义Swagger文档配置mayikt.zuul.swaggerDocument
1、下载安装
- 下载aploll配置中心 https://github.com/nobodyiam/apollo-build-scripts
- 上传apollo-build-scripts-master文件到服务器中
- unzip apollo-build-scripts-master.zip 解压配置文件
如果没有unzip命令的话,安装zip插件 yum -y install zip unzip
配置数据策略
修改demo.sh账号:cdb-8y8qmojr.gz.tencentcdb.com:10010 root Ww861642669+
启动阿波罗 ./demo.sh start
systemctl stop firewalld.service
默认账号密码 Apollo admin
2、服务客户端集成配置文件
2.1 将本地配置文件配置到阿波罗
转换地址:http://www.toyaml.com/index.html
2.2 引入依赖
<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>
2.3 配置文件application.properties
app.id=mayikt644064779
apollo.meta=http://192.168.212.236:8080
2.4 网关项目开启阿波罗@EnableApolloConfig
@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 {
@Override
public List<SwaggerResource> get() {
// 开启监听,配置文件发生改变需要更改
appConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
get();
}
});
return resources();
}
/**
* 从阿波罗服务器中获取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("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;
}
}
}
2.5 自定义Swagger文档配置mayikt.zuul.swaggerDocument
[
{
"name": "app-mayikt-member",
"location": "/app-mayikt-member/v2/api-docs",
"version": "2.0"
},
{
"name": "app-mayikt-weixin",
"location": "/app-mayikt-weixin/v2/api-docs",
"version": "2.0"
}
]
每个项目通过appid来区分配置
监听配置:
@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());
}
});
}
}