一般 spring 集成apollo(Spring mvc 举例)
spring mvc 项目集成apollo
a.配置文件引入apollo 客户端
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.1.0</version> </dependency>
b.继承游戏啊PropertyPlaceholderConfigurer 代码,并重写这个方法
public class apolloImple extends PropertyPlaceholderConfigurer { @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { try { //从apollo中获取所有配置信息 Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null Set<String> fieldnames = config.getPropertyNames(); //遍历配置信息 for(String fieldname : fieldnames){ String attributeName=fieldname; String attributeValue = config.getProperty(fieldname,""); props.put(attributeName,attributeValue); } } catch (Exception e) { e.printStackTrace(); logger.info("获取apollo配置失败"); } super.processProperties(beanFactoryToProcess, props); } }
新增加配置文件可以在类似的文件的位置添加:applicationContext.xml这个地方有些难理解,加个图,不明白的可以在评论区提问:
1.xmlns:apollo="http://www.ctrip.com/schema/apollo"
2.<beans xsi:schemaLocation=" http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd“>
3. <apollo:config/>
c.在web 工程的resource目录下新建目录:/META-INF/app.properties 内容如下:
app.id=xxx.test apollo.cacheDir=/opt/data/some-cache-dir
d.在META-INF同级目录下建立环境配置文件,apollo-env.properties 文件,内容如下:
dev.meta=http://1.1.1.1:8080 fat.meta=http://apollo.fat.xxx.com uat.meta=http://apollo.uat.xxx.com pro.meta=http://apollo.xxx.com
e.tomcat 部署:
linux环境:tomcat/bin/setenv.sh(新建),内容如下:
