服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用Springboot整合Apollo配置中心

使用Springboot整合Apollo配置中心

2021-10-29 11:15chen18677338530 Java教程

这篇文章主要介绍了使用Springboot整合Apollo配置中心的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

apollo简介

apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

官方网站 https://github.com/ctripcorp/apollo

apollo 环境搭建

详细步骤参见: https://github.com/ctripcorp/apollo/wiki/quick-start

使用Springboot整合Apollo配置中心

使用Springboot整合Apollo配置中心

官网已经很详细的说明了操作步骤。但是有很多前置条件才能完成安装。

1、必须要有git环境,才能下载apollo代码

  1. yum –y install git 

然后需要clone代码。

2、安装docker-compose环境

地址:https://github.com/docker/compose/releases

  1. curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose 
  2. chmod +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

  1. <dependency> 
  2.     <groupId>com.ctrip.framework.apollo</groupId> 
  3.     <artifactId>apollo-client</artifactId> 
  4.     <version>1.1.0</version> 
  5. </dependency> 
  6. <dependency> 
  7.     <groupId>com.ctrip.framework.apollo</groupId> 
  8.     <artifactId>apollo-core</artifactId> 
  9.     <version>1.1.0</version> 
  10. </dependency> 

3、创建apollo配置文件

  1. local.meta=http://192.168.75.50:8080 
  2. dev.meta=http://192.168.75.50:8080 
  3. fat.meta=${fat_meta} 
  4. uat.meta=${uat_meta} 
  5. lpt.meta=${lpt_meta} 
  6. pro.meta=${pro_meta} 

使用Springboot整合Apollo配置中心

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

  1. app.id=cyp_001 

使用Springboot整合Apollo配置中心

5、读取apollo中的配置信息,防止配置没有注入,一定要设置默认值!

  1. package com.chen.apolloconfig; 
  2. import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; 
  3. import org.springframework.boot.SpringApplication; 
  4. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  5. import org.springframework.context.annotation.Configuration; 
  6. @Configuration 
  7. @EnableApolloConfig 
  8. @SpringBootApplication 
  9. public class ApolloConfigApplication { 
  10.     public static void main(String[] args) { 
  11.         SpringApplication.run(ApolloConfigApplication.class, args); 
  12.     } 
  1. ``` 
  2. package com.chen.apolloconfig.controller; 
  3. import org.springframework.beans.factory.annotation.Value; 
  4. import org.springframework.web.bind.annotation.GetMapping; 
  5. import org.springframework.web.bind.annotation.RestController; 
  6. @RestController 
  7. public class IndexController { 
  8.     @Value("${name:chenyongpeng}"
  9.     private String name; 
  10.     @GetMapping("/getName"
  11.     public String getMyName(){ 
  12.         return name; 
  13.     } 
  14. ``` 
  15. ![在这里插入图片描述](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配置中心

  1. env=DEV 

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

原文链接:https://blog.csdn.net/chen18677338530/article/details/91047514

延伸 · 阅读

精彩推荐