使用Springboot整合Apollo配置中心

Apollo简介

Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
官方网站 https://github.com/ctripcorp/apollo
Apollo 环境搭建 【使用Springboot整合Apollo配置中心】详细步骤参见: https://github.com/ctripcorp/apollo/wiki/Quick-Start
使用Springboot整合Apollo配置中心
文章图片

使用Springboot整合Apollo配置中心
文章图片

官网已经很详细的说明了操作步骤。但是有很多前置条件才能完成安装。
1、必须要有git环境,才能下载Apollo代码
yum –y install git

然后需要clone代码。
2、安装docker-compose环境
地址:https://github.com/docker/compose/releases
curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-compose

使用Springboot整合Apollo配置中心
文章图片

测试结果
使用Springboot整合Apollo配置中心
文章图片

至此为止,就可以运行文档中的脚本了。
安装完成之后,需要等待两三分钟就可以访问了。
使用Springboot整合Apollo配置中心
文章图片

输入账号:apollo,密码:admin
使用Springboot整合Apollo配置中心
文章图片

点击创建项目
使用Springboot整合Apollo配置中心
文章图片

点击新建配置
使用Springboot整合Apollo配置中心
文章图片
使用Springboot整合Apollo配置中心
文章图片

点击发布
发布之后的配置才能生效
使用Springboot整合Apollo配置中心
文章图片
使用Springboot整合Apollo配置中心
文章图片

查看发布历史
使用Springboot整合Apollo配置中心
文章图片

整合SpringBoot项目
1、使用idea新建springboot项目
2、修改pom.xml
com.ctrip.framework.apolloapollo-client1.1.0com.ctrip.framework.apolloapollo-core1.1.0

3、创建apollo配置文件
local.meta=http://192.168.75.50:8080dev.meta=http://192.168.75.50:8080fat.meta=${fat_meta}uat.meta=${uat_meta}lpt.meta=${lpt_meta}pro.meta=${pro_meta}

使用Springboot整合Apollo配置中心
文章图片

4、创建app.id配置文件,每一个应用基本使用一个app.id
app.id=cyp_001

使用Springboot整合Apollo配置中心
文章图片

5、读取apollo中的配置信息,防止配置没有注入,一定要设置默认值!
package com.chen.apolloconfig; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; @Configuration@EnableApolloConfig@SpringBootApplicationpublic class ApolloConfigApplication {public static void main(String[] args) {SpringApplication.run(ApolloConfigApplication.class, args); }}

```package com.chen.apolloconfig.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class IndexController {@Value("${name:chenyongpeng}")private String name; @GetMapping("/getName")public String getMyName(){return name; }}```![在这里插入图片描述](https://img-blog.csdnimg.cn/20190606172204975.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW4xODY3NzMzODUzMA==,size_16,color_FFFFFF,t_70)

总结 至此,springboot整合apollo配置中心已经验证通过!
springcloud的配置中心是基于git或者gitte,gitlib等托管中心!
apollo是基于数据库和本地缓存!
采坑之处 项目启动之后一直说是找不到dev环境!!!
此处需要在本机下新建环境配置!
使用Springboot整合Apollo配置中心
文章图片

env=DEV

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读