携程官网对apollo的使用讲解了很多种方式的使用,但是感觉一些细节还是没讲全,特别是eureka配置中心地址的配置

这里对springboot整合apollo说一下

>SpringBoot启动vm参数添加:
-Ddev_meta=http://18.16.200.107:8080 -Denv=DEV
其中-Ddev-meta连接的是配置管理eureka的url地址
-Denv配置的是具体的环境

>也可以在C:\opt\settings\server.properties中添加环境配置:
env=DEV
这个配置文件里面只能配置环境,不能配置url

>第一步配置app.id,在META-INF/app.properties里面添加app.id,类型字符串

代码:

package com.qhong.springboot;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* 使用Java Config方式
* 使用@ApolloConfig自动注入Config对象
* 使用@ApolloConfigChangeListener自动注入ConfigChangeListener对象
* 当监听到属性值发生变化后使用Config API修改属性值
*/
@ToString
@Component
@ConfigurationProperties
public class JavaConfigSample {
/**
* @ApolloConfig用来自动注入Config对象
*/
@ApolloConfig
private Config config;
/**
* @ApolloConfigChangeListener用来自动注册ConfigChangeListener
*/
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
changeEvent.changedKeys().forEach(key ->{
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format("Found JavaConfigSample change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
});
} @Value("${timeout:100}")
private int timeout;
private int batch; @Value("${batch:200}")
public void setBatch(int batch) {
this.batch = batch;
} public int getTimeout() {
if(config!=null) {
return config.getIntProperty("timeout", );
}else{
return timeout;
}
} public int getBatch() {
return this.batch;
}
}

这个类里面timeout获取值的方式直接调用getIntProperty

package com.qhong.springboot;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* 使用Spring Boot ConfigurationProperties方式
* <pre>
* redis.cache.enabled = true
* redis.cache.expireSeconds = 100
* redis.cache.clusterNodes = 1,2
* redis.cache.commandTimeout = 50
* redis.cache.someMap.key1 = a
* redis.cache.someMap.key2 = b
* redis.cache.someList[0] = c
* redis.cache.someList[1] = d
* </pre>
*/
@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(prefix = "redis.cache")
@EnableApolloConfig("application")
public class ConfigurationPropertiesSample { private int expireSeconds;
private String clusterNodes;
private int commandTimeout; private Map<String, String> someMap = Maps.newLinkedHashMap();
private List<String> someList = Lists.newLinkedList(); @PostConstruct
private void initialize() {
System.out.println(String.format(
"SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}, someMap: {}, someList: {}",
expireSeconds, clusterNodes, commandTimeout, someMap, someList));
} /**
* @ApolloConfig用来自动注入Config对象
*/
@ApolloConfig("application")
private Config config;
/**
* @ApolloConfigChangeListener用来自动注册ConfigChangeListener
*/
@ApolloConfigChangeListener("application")
private void someOnChange(ConfigChangeEvent changeEvent) {
changeEvent.changedKeys().forEach(key ->{
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
});
}
}

这个是各种属性类型的使用

apollo配置中心文本格式:

esb.app.url = http://18.16.200.10:3000/
endpoints.shutdown.enabled = true
endpoints.shutdown.sensitive = false
spring.http.multipart.maxFileSize = 100Mb
spring.http.multipart.max-request-size = 100Mb timeout =
batch =
redis.cache.enabled = false
redis.cache.expireSeconds =
redis.cache.clusterNodes = ,,,
redis.cache.commandTimeout =
redis.cache.someMap.key1 = a
redis.cache.someMap.key2 = b
redis.cache.someList[] = c
redis.cache.someList[] = d

上面两种都是默认的namespace,为application

下面添加一个datasource命名空间类型的类

package com.qhong.springboot;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* Created by qhong on 2018/5/8 16:10
**/
@ToString
@Component
@Data
@ConfigurationProperties
@EnableApolloConfig("datasource")
public class DataSourceConfig {
// 动态配置从esb config读取
private String url;
private String username;
private String password;
private String driverClassName; /**
* @ApolloConfig用来自动注入Config对象
*/
@ApolloConfig("datasource")
private Config config; /**
* @ApolloConfigChangeListener用来自动注册ConfigChangeListener
*/
@ApolloConfigChangeListener("datasource")
private void someOnChange(ConfigChangeEvent changeEvent) {
changeEvent.changedKeys().forEach(key ->{
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format("Found datasource change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
});
} }

注意EnableApolloConfig中定义datasource命名空间

上面类的具体文本:

url = jdbc:mysql://120.26.130.187:3306/huishi-server?useUnicode=true&characterEncoding=utf-8&useSSL=false
username = root
password = jsy2016memeda
driverClassName = com.mysql.jdbc.Driver

当然也可以使用ApolloConfig这个定义datasource命名空间的,然后一个个getProperty来获取值,就是有点不方便。

下面使用SpringBoot启动:

package com.qhong.springboot;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication(scanBasePackages = {"com.qhong.springboot"})
public class Application {
public static void main(String[] args) throws Exception {
ApplicationContext context = new SpringApplicationBuilder(Application.class).run(args);
JavaConfigSample javaConfigSample = context.getBean(JavaConfigSample.class);
ConfigurationPropertiesSample configurationPropertiesSample=context.getBean(ConfigurationPropertiesSample.class);
DataSourceConfig dataSourceConfig=context.getBean(DataSourceConfig.class);
System.out.println("Application Demo. Input any key except quit to print the values. Input quit to exit.");
while (true) {
System.out.print("> ");
String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
if (!Strings.isNullOrEmpty(input) && input.trim().equalsIgnoreCase("quit")) {
System.exit();
} if(javaConfigSample!=null){
System.out.println(javaConfigSample.toString());
}
if(configurationPropertiesSample!=null){
System.out.println(configurationPropertiesSample.toString());
}
if(dataSourceConfig!=null){
System.out.println(dataSourceConfig);
} }
}
}

随便输入字符就可以显示监控的字段

上面代码的地址:

https://gitee.com/hongdada/spring-boot-apollo-sample

https://blog.csdn.net/hry2015/article/details/72353994

https://blog.csdn.net/hry2015/article/details/72453920

参考:

https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97

SpringBoot 整合携程Apollo配置管理中心的更多相关文章

  1. Spring Boot 2.0 整合携程Apollo配置中心

    原文:https://www.jianshu.com/p/23d695af7e80 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够 ...

  2. 携程Apollo配置中心架构深度剖析

    转自:http://www.uml.org.cn/wfw/201808153.asp 一.介绍 Apollo(阿波罗)[参考附录]是携程框架部研发并开源的一款生产级的配置中心产品,它能够集中管理应用在 ...

  3. 携程 Apollo 配置中心传统 .NET 项目集成实践

    官方文档存在的问题 可能由于 Apollo 配置中心的客户端源码一直处于更新中,导致其相关文档有些跟不上节奏,部分文档写的不规范,很容易给做对接的新手朋友造成误导. 比如,我在参考如下两个文档使用传统 ...

  4. 携程apollo配置中心服务端如何感知配置更新?

    引言 前面有写过一篇<分布式配置中心apollo是如何实时感知配置被修改>,也就是客户端client是如何知道配置被修改了,有不少读者私信我你既然说了client端是如何感知的,那服务端又 ...

  5. 携程apollo系列-客户端集成

    本文讲解如何在 Java 程序中集成 Apollo 配置, 主要涉及到一些基础用法. 对于一些高级用法, 比如如何加密/解密配置项 (可用于数据库密码配置), 如何动态切换数据源地址,如何动态切换日志 ...

  6. 分布式配置中心 携程(apollo)

    1.传统配置文件与分布式配置文件区别 传统配置文件:如果修改了配置文件,需要重新打包发布,重新发布服务,而且每个环境的变更配置文件,比较繁琐. 分布式配置文件:将配置文件注册到配置中心上去,可以使用分 ...

  7. 携程apollo系列-个人开发环境搭建

    本博客讲详细讲解如何在 Windows 搭建携程 Apollo 服务器 (用户个人开发). 开发服务器有多种搭建方式:(1) docker, 搭建过程非常简单, 推荐有 docker 环境(2) 部署 ...

  8. 携程Apollo统一配置中心的搭建和使用

    原文链接:https://blog.csdn.net/luhong327/article/details/81453001 一.Apollo配置中心介绍 1.What is Apollo 1.1 Ap ...

  9. CentOS 7 搭建基于携程Apollo(阿波罗)配置中心单机模式

    Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性.服务端基于Spring Boot ...

随机推荐

  1. jdk8新特性-亮瞎眼的lambda表达式

    jdk8之前,尤其是在写GUI程序的事件监听的时候,各种的匿名内部类,大把大把拖沓的代码,程序毫无美感可言!既然Java中一切皆为对象,那么,就类似于某些动态语言一样,函数也可以当成是对象啊!代码块也 ...

  2. SQL 跟据出生日期求年龄

    最近做项目时遇到一个问题. 跟据人员的生日与当前日期进行比较求出该人员实际年龄.这个看上去比较简单的问题,其实不细心去看也会有很多问题. 先看第一种: 一张人员信息表里有一人生日(Birthday)列 ...

  3. python多线程,多进程编程。

    进程,是目前计算机中为应用程序分配资源的最小单位: 线程,是目前计算机中运行应用程序的最小单位: 在实际系统中,其实进程都是被分为线程来实现的,所以参与时间片轮转的是线程: 但是管理应用程序的资源的单 ...

  4. 代码审查Code Review

    代码审查清单 常规项 代码能够工作么?它有没有实现预期的功能,逻辑是否正确等. 所有的代码是否简单易懂? 代码符合你所遵循的编程规范么?这通常包括大括号的位置,变量名和函数名,行的长度,缩进,格式和注 ...

  5. spring aop:aspectj-autoproxy 配置

    经常遇到aop-aspectJ的通知不被执行的问题 解决方法:http://blog.csdn.net/qwdafedv/article/details/53005210 首先,确保配置文件都已经是正 ...

  6. Yii2 Restful api搜索实现

  7. html5-figure和figcaption元素

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. OC 反射-->动态创建类

    系统方法 NSLog(@"%s", __func__); //打印出类的方法名称,如: //打印结果:2018-02-22 10:52:15.394575+0800 DemoRun ...

  9. Docker相关知识整理

    一.进入和退出docker容器 使用docker ps查看容器id docker exec -it containerId /bin/bash //进入容器 Ctrl + P + Q //退出容器 二 ...

  10. 4、CentOS6.5下安装php5.3

    操作系统:CentOS6.5 环境:Apache2.2安装成功.可查看:http://www.centoscn.com/image-text/install/2014/0505/2910.html M ...