使用Springboot整合Apollo配置中心
Apollo简介
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。官方网站 https://github.com/ctripcorp/apollo
Apollo 环境搭建 【使用Springboot整合Apollo配置中心】详细步骤参见: https://github.com/ctripcorp/apollo/wiki/Quick-Start
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/345774db9aa74b58920fcd32f3a8a34c.jpg)
文章图片
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/bddf1b66d91042dcb61683ad97e1e759.jpg)
文章图片
官网已经很详细的说明了操作步骤。但是有很多前置条件才能完成安装。
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配置中心](https://img.it610.com/image/info11/2e3aff71cea04e1c89eaad1bee91642e.jpg)
文章图片
测试结果
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/c4b4c7a00b464926bad9ca624ae26d50.png)
文章图片
至此为止,就可以运行文档中的脚本了。
安装完成之后,需要等待两三分钟就可以访问了。
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/40899656c3dc4acfbf5bf30edea8c0a8.jpg)
文章图片
输入账号:apollo,密码:admin
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/263357a960b44280b2b8d43f3502314b.jpg)
文章图片
点击创建项目
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/968479a964e8414fa5947eab7070dda8.jpg)
文章图片
点击新建配置
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/bf143c19aa4f4c8fa37c7b3ef92d6a5e.jpg)
文章图片
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/ebb098d7286d4f648cd7408bee05daf5.jpg)
文章图片
点击发布
发布之后的配置才能生效
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/c517c2fcffa9463b94f9fab9724b939a.jpg)
文章图片
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/700bd7f283b9486fbc563ff42364979b.jpg)
文章图片
查看发布历史
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/25e2ca718ce649258c3840d172e8caae.jpg)
文章图片
整合SpringBoot项目
1、使用idea新建springboot项目
2、修改pom.xml
com.ctrip.framework.apollo apollo-client1.1.0 com.ctrip.framework.apollo apollo-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配置中心](https://img.it610.com/image/info11/8157d9e6113049918082b700db7c9a27.jpg)
文章图片
4、创建app.id配置文件,每一个应用基本使用一个app.id
app.id=cyp_001
![使用Springboot整合Apollo配置中心](https://img.it610.com/image/info11/74b1df9739224b459f1f430eb6591254.jpg)
文章图片
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配置中心](https://img.it610.com/image/info11/ae6a2dcfd70f4cb3b191588e21fc66f2.jpg)
文章图片
env=DEV
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- Activiti(一)SpringBoot2集成Activiti6
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程