一般 spring 集成apollo(Spring mvc 举例)

本文详细介绍如何在Spring MVC项目中集成Apollo配置中心,包括引入Apollo客户端依赖、自定义配置加载方式、设置环境特异性配置及部署注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(新建),内容如下:

d.直接启动日志文件可以看到-Denv=DEV  或者其他。注意,这里大小写敏感。默认配置:LOCAL, DEV, FWS, FAT, UAT, LPT, PRO, TOOLS, UNKNOWN;

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值