构建分布式配置中心阿波罗

为什么要使用配置文件中心?

 为什么要使用分布式配置中心? 统一管理微服务配置文件,可以实现动态化刷新配置文件。

为什么我们要使用阿波罗? 不使用SpringCloudConfig

阿波罗配置文件存放在数据库中,SpringCloudConfig存放在Git里面。

(一)简单搭建分布式配置中心阿

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

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.将本地配置存入到阿波罗平台中

2.引入Maven依赖


        
            com.ctrip.framework.apollo
            apollo-client
            1.0.0
        

        
            com.ctrip.framework.apollo
            apollo-core
            1.0.0
        

 3.创建 application.properties

app.id= dm-mall-service-api

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

(三)网关服务集成阿波罗,拉取Swagger配置信息

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
@EnableApolloConfig
public class AppGateWay {// 获取ApolloConfig@ApolloConfigprivate Config appConfig;public static void main(String[] args) {SpringApplication.run(AppGateWay.class, args);}// 添加文档来源@Component@Primaryclass DocumentationConfig implements SwaggerResourcesProvider {@Overridepublic List get() {// 开启监听,配置文件发生改变需要更改appConfig.addChangeListener(new ConfigChangeListener() {@Overridepublic void onChange(ConfigChangeEvent changeEvent) {get();}});return resources();}/*** 从阿波罗服务器中获取resources* * @return*/private List resources() {List resources = new ArrayList<>();// dm-mall-service-api// 网关使用服务别名获取远程服务的SwaggerApiString 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("dm.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文档配置dm.zuul.swaggerDocument

[
    {
        "name": "app-dm-member",
        "location": "/app-dm-member/v2/api-docs",
        "version": "2.0"
    },
    {
        "name": "app-dm-weixin",
        "location": "/app-dm-weixin/v2/api-docs",
        "version": "2.0"
    }
]

(五)项目启动监听配置文件变化

@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {@ApolloConfigprivate Config config;@Overridepublic void run(String... args) throws Exception {config.addChangeListener(new ConfigChangeListener() {@Overridepublic void onChange(ConfigChangeEvent changeEvent) {log.debug("####分布式配置中心监听#####" + changeEvent.changedKeys().toString());}});}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部