学习

搭建微服务架构配置中心阿波罗(Apollo)

2021-07-20  本文已影响0人  喝口你的蛋白粉

1、阿波罗(Apollo)介绍

Apollo(阿菠萝)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。Apollo支持4个维度管理Key-Value格式的配置:Namespace(名称空间)、Cluster(集群)、Environment(环境)、Application(应用)。

2、阿波罗(Apollo)特性

• 统一管理不同环境(支持有限的环境关键词)、不同集群的配置

• 配置修改实时生效

• 版本发布管理

• 灰度发布

• 权限管理、发布审核、操作审计

• 客户端配置信息监控

• 提供Java和.Net原生客户端,且支持HTTP接口

3、阿波罗(Apollo)架构

Apollo架构

4、阿波罗Apollo搭建整体流程

1. 下载aploll配置中心 https://github.com/nobodyiam/apollo-build-scripts

2. 上传apollo-build-scripts-master文件到服务器中

3.  unzip apollo-build-scripts-master.zip 解压配置文件

如果没有unzip命令的话,安装zip插件yum -y install zip unzip

4. 配置数据策略

修改demo.sh账号:cdb-8y8qmojr.gz.tencentcdb.com:10010 root Ww861642669+

5. 启动阿波罗 ./demo.sh start

6.Apollo配置中心介绍

https://github.com/ctripcorp/apollo/wiki/Apollo%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E4%BB%8B%E7%BB%8D

systemctl stop firewalld.service  

默认账号密码 Apollo  admin

服务客户端集成配置文件

1.将本地配置存入到阿波罗平台中。

转换工具

http://www.toyaml.com/index.html

2.引入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>

3.创建 application.properties

app.id=mayikt644064779

apollo.meta=http://192.168.212.236:8080

4.项目启动开启阿波罗配置文件

@EnableApolloConfig

5.修改/opt/settings/server.properties(Mac/Linux)或C:\opt\settings\server.properties(Windows)文件,设置env为DEV:

env=DEV

网关服务集成波罗

@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;

}

}

}

自定义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,区分不同团队的配置文件。

注意:不是所有的配置文件都会在阿波罗平台修改后,就会立马生效,因为没有采用监听刷新配置文件。

上一篇 下一篇

猜你喜欢

热点阅读