apollo应用配置集成及使用
apollo应用配置集成及使用
1. 开发环境Apollo地址
用户名:apollo 密码:admin
开发环境Apollo管理台地址:http://localhost:8070/
开发环境Apollo Eureka地址:http://localhost:8080/
2. pom.xml引用客户端
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
3. springboot项目集成
- properties配置:
//配置唯一appid既项目创建时的唯一id
app.id=110000001
//指定配置中心Eureka地址既metaservice
apollo.meta=@apollo.meta.service@
//启动的bootstrap阶段,向Spring容器注入
apollo.bootstrap.enabled = true
//指定该项目关联的相关作用域
apollo.bootstrap.namespaces=application,TEST1.jdbc,部门.作用域
4. spring项目集成
创建/META-INF/app.properties 内容app.id=110000001
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:apollo="http://www.ctrip.com/schema/apollo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd">
<!-- application 的优先级别 -->
<apollo:config order="2"/>
<!-- 这个是最复杂的配置形式,指示Apollo注入TEST1.jdbc和TEST1.xxljob namespace的配置到Spring环境中,并且顺序在application前面 -->
<apollo:config namespaces="TEST1.xxljob,TEST1.jdbc" order="1"/>
</beans>
5. 使用及应用
//=========== springboot start ===========
// 启动类加上
@Configuration
@EnableApolloConfig
//=========== springboot end ===========
//=========== start ===========
// 获取注入某个作用域的配置
// 如TEST1.jdbc
@ApolloConfig("部门.作用域")
private Config config;
// 获取某个值
@Value("${key}")
private String value;
// 监听多个或单个作用域
@ApolloConfigChangeListener({"application", "TEST1.jdbc"})
private void anotherOnChange(ConfigChangeEvent changeEvent) {
System.out.println("===================");
// 获取key为test的change属性 包括新值老值等
ConfigChange change = changeEvent.getChange("test");
System.out.println(String.format("Found change - key: %s, oldValue: %s,"
+ " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
System.out.println("===================");
}
//监听所有作用域
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
//如果值发生修改了 获取属性
if (changeEvent.isChanged("batch")) {
batch = config.getIntProperty("batch", 100);
}
}
//获取指定类型属性
public int getTimeout() {
return config.getIntProperty("timeout", 200);
}
//=========== end ===========
6. 注意项
- XXXXXXConfigurer类 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer如使用以上类请替换成org.springframework.context.support.PropertySourcesPlaceholderConfigurer。
另外Spring 3.1以后就不建议使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer。
- 多环境eureka地址动态切换
1. VM options修改eureka地址
-apollo.meta.service=XXXXX.XXX.XXX
2. 使用maven profile
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<apollo.meta.service> http://localhost:8080 </apollo.meta.service>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<apollo.meta.service> http://devhost:38080 </apollo.meta.service>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<apollo.meta.service> http://prohost:38080 </apollo.meta.service>
</properties>
</profile>
</profiles>
apollo应用配置集成及使用的更多相关文章
- apollo 项目配置中心开源框架部署
apollo 于我带来的好处 1. 项目之前的配置信息全部都在 resources 目录下,当然这里我使用的是 Spring Boot 搭建的项目.使用 apollo 后,配置信息全部转移到 apol ...
- springboot集成Apollo分布式配置
安装Apollo服务 1.安装mysql 地址:https://www.cnblogs.com/xuaa/p/10782352.html 2.下载Apollo源码到本地 地址:https://gith ...
- 携程Apollo统一配置中心的搭建和使用
原文链接:https://blog.csdn.net/luhong327/article/details/81453001 一.Apollo配置中心介绍 1.What is Apollo 1.1 Ap ...
- 携程阿波罗(Apollo)配置中心
携程阿波罗(Apollo) https://www.cnblogs.com/xiaxiaolu/p/10025597.html 一.瞎扯点什么 1.1 阿波罗 阿波罗是希腊神话中的光明之神.文艺之 ...
- python访问Apollo获取配置
操作系统 : CentOS7.3.1611_x64 Python 版本 : 3.6.8 Apollo源码地址: https://github.com/ctripcorp/apollo 访问Apollo ...
- netcore项目中使用 SpringCloudConfig 和apollo做配置中心
版权所有,转载请注明出处 https://www.cnblogs.com/netqq/p/14251403.html 一.使用apollo作为配置中心 首先apollo 项目简介和安装请自行百度,本文 ...
- Apollo 中配置String、Map和List和默认值
摘要:在Apollo 中,配置String.Map和List等类型的信息,同时设置默认值. 综述 随着业务需求的变更,需要在Apollo中配置一个Map<String, List>类型 ...
- apollo与springboot集成实现动态刷新配置
分布式apollo简介 Apollo(阿波罗)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性. 本 ...
- 携程apollo系列-客户端集成
本文讲解如何在 Java 程序中集成 Apollo 配置, 主要涉及到一些基础用法. 对于一些高级用法, 比如如何加密/解密配置项 (可用于数据库密码配置), 如何动态切换数据源地址,如何动态切换日志 ...
随机推荐
- [Postgres]postgresql.conf : Permission denied处理一法
使用yum安装完postgresql,没有使用默认的DATA地址,自己配置了DATA地址以后,使用root权限启动service service postgresql start ,报出了" ...
- Varnish 学习资料收集
高性能HTTP加速器Varnish(安装配置篇) 利用Varnish构建Cache服务器笔记 Varnish代理服务器部署 Varnish基础概念详解 Varnish的配置语言VCL及其内置变量介绍 ...
- 修改mysql的时间/时区
# 背景 往db中insert数据发现时间不对,因为是新DB,所以猜测是mysql设置不对 # 解决方法 方法一:通过mysql命令行模式下动态修改 show variables like " ...
- angular 响应式表单指令
响应式表单都是以 form开头的指令 第一列指令(不以name结尾)在html模版中,用 [ ] 第二列指令(以name结尾)在html模版中,不用 [ ]
- (zxing.net)一维码UPC A的简介、实现与解码
一.简介 UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定.连续性的条 码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码. U ...
- nginx反向代理后abp的webapi host如何获取客户端ip?
dotnet core 跨平台是微软伟大的创举,脱离iis后服务器成本都降低了. 问题 这不,采用abp搞了个小项目,部署到centos后发现审计日志里面的ip信息不对. 解决 这个问题在.net 4 ...
- mysql--对行(表中数据)的增删改查
一.插入数据(增加)insert 1.插入数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); #指定字段来插入数据,插 ...
- “全栈2019”Java异常第十四章:将异常输出到文本文件中
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- CentOS中vsftpd的主动和被动方式
网址http://blog.csdn.net/nyunyuzhao/article/details/5734978,学习了. FTP是File Transfer Protocol(文件传输协议)的缩写 ...
- 虚拟机搭建ftp环境
引用http://www.cnblogs.com/xiangxiaodong/archive/2013/12/23/3487028.html,学习. 本人是在windows8系统下,Oracle VM ...