apollo整合java客户端的简单的springmvc的项目

Apollo(阿波罗)是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。

Apollo支持4个维度管理Key-Value格式的配置:

  1. application (应用)
  2. environment (环境)
  3. cluster (集群)
  4. namespace (命名空间)

1.pom的引入


			
				com.google.guava
				guava
				20.0
			
			
				com.ctrip.framework.apollo
				apollo-client
				0.11.0
			
			
				com.ctrip.framework.apollo
				apollo-core
				0.11.0
			
			
				com.google.inject
				guice
				3.0
			
			
				javax.inject
				javax.inject
				1
			
			
				com.google.code.gson
				gson
				1.6
			

2.配置appid和环境。连接等信息

apollo整合java客户端的简单的springmvc的项目_第1张图片

-Dapp.id=jeedan  -Dapollo.meta=http://172.16.37.143:8080  -Denv=DEV 

3.配置springconfig.xml

 apollo整合java客户端的简单的springmvc的项目_第2张图片

apollo整合java客户端的简单的springmvc的项目_第3张图片

注意:1.头文件要声明

           2.配置apollo的信息,其中namespace是你要读取的配置的在apollo中的namespace

          3.这里是两种方式。第一种bean是将apollo的所有配置信息全部读取,第二种是将配置信息注入到bean中

package statement.base;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;
import java.util.Set;

/**
 * Created by admin on 2020/4/9.
 */
public class AppPropertyPlaceholderConfigurer 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 fieldnames = config.getPropertyNames();
            //遍历配置信息
            for(String fieldname : fieldnames){
                String attributeName=fieldname;
                String attributeValue = config.getProperty(fieldname,"");
                System.out.println("attributeName:"+attributeName + "; attributeValue:" + attributeValue );
                props.put(attributeName,attributeValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("获取apollo配置失败");
        }
        super.processProperties(beanFactoryToProcess, props);
    }
}

 

package com.jeedan.action.domain.po;

import org.springframework.stereotype.Component;


/**
 * Created by admin on 2020/4/23.
 */
@Component("TestXmlBean")
public class TestXmlBean {
    private int timeout;
    private int batch;

    public void setTimeout(int timeout) {
        this.timeout = timeout;
        System.out.print(timeout);
    }

    public void setBatch(int batch) {
        this.batch = batch;
        System.out.print(batch);
    }

    public int getTimeout() {
        return timeout;
    }

    public int getBatch() {
        return batch;
    }

    @Override
    public String toString() {
        return String.format("[XmlBean] timeout: %d, batch: %d", timeout, batch);
    }
}

 4.启动你的项目即可。

 

 

你可能感兴趣的:(学习)