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 配置, 主要涉及到一些基础用法. 对于一些高级用法, 比如如何加密/解密配置项 (可用于数据库密码配置), 如何动态切换数据源地址,如何动态切换日志 ...
随机推荐
- underscore arrays
1._.first():返回array(数组)的第一个元素.传递 n参数将返回数组中从第一个元素开始的n个元素 _.first = _.head = _.take = function(array, ...
- 寻找最大的K个数(上)
这是一道很经典的题目,有太多方法了,今天写了两种方法,分别是快排和堆排序 #include <iostream> using namespace std; #define N 25 //初 ...
- Objective-C 学习笔记(三) Numbers/数字
Objective-C Numbers/数字 Objective-C中提供了一系列的NSNumber和重要的工作方法: + (NSNumber *)numberWithBool:(BOOL)value ...
- [Erlang28]使用匿名函数灵活组合不同的case
cowboy_http.erl里面的date1/2 启示: 以前一般写case里都是这样子: date1(Date) -> case month1(Date) of {error,badarg} ...
- 学习React前端框架,报错 'render' is not defined no-undef
报错 'render' is not defined no-undef 原因没有 写 import { render } from 'react-dom'
- cesium编程入门(九)实体 Entity
cesium编程入门(九)实体 Entity 在cesium编程入门(五)绘制形状提到过添加实体的方法,这一节聊一聊实体相关的一些内容: 先来看 Entity 的各个属性 id 唯一标志,如果没设置, ...
- how to remote debug in vs 2013
first download the debugger tools "rtools_setup_x64" start C:\Program Files\Microsoft Visu ...
- 调用阿里云API 的demo示例(java/python)
Java 示例 // 创建DefaultAcsClient实例并初始化 DefaultProfile profile = DefaultProfile.getProfile(vo.getAliRegi ...
- Python3.5 学习五
心灵鸡汤电影推荐: 阿甘正传.辛德勒名单.肖申克的救赎.勇敢的心.角斗士.美国丽人.教父.钢琴师.指环王.西雅图不眠夜.廊桥遗梦.可可西里的美丽传说.放牛班的春天.血钻.战争之王.上帝之城.中央车站. ...
- 栈实现 C语言
最近上来写了一下栈,理解数据结构的栈. 头文件:stack.h 初始化栈结构与函数定义: #include<stdlib.h> #include <stdio.h> #incl ...