背景:不少老舊項目改造時,須要接入配置中心; 通過團隊評估,藉助disconf來實現過渡到其餘配置中心比較簡單; 因此博主在此作個分享;css
1、springboot項目html
(1).pom中引入JAR包依賴java
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency>
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> </properties> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> </properties> </profile> </profiles>
<build> <resources> <resource> <directory>${basedir}/src/main/resources</directory> <includes> <include>*.properties</include> <include>*.xml</include> <include>sqlmap/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>${basedir}/Config/${profileActive}</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
(2).項目Config目錄配置node
建立文件夾:/Config/${profileActive} ----其中profileActive爲pom中配置的環境參數:dev/test/pre/prod 目錄下disconf.properties配置文件內容以下: disconf.enable.remote.conf=true disconf.conf_server_host=config-online.xxxx.com disconf.version=0.0.1 disconf.app=louvre-order disconf.env=prod disconf.enable_local_download_dir_in_class_path=true
(3).代碼中編寫disconf配置web
package com.xxx.xxx.config; import java.io.IOException; import java.util.Arrays; import java.util.List; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.baidu.disconf.client.DisconfMgrBean; import com.baidu.disconf.client.DisconfMgrBeanSecond; import com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean; import com.baidu.disconf.client.config.DisClientConfig; /** * Disconf配置中心 * @author aaron * @date 2019/08/28 */ @Configuration public class DisconfConfig { @Bean(destroyMethod = "destroy") public DisconfMgrBean disconfMgrBean() { DisconfMgrBean mgr = new DisconfMgrBean(); mgr.setScanPackage("com.xxx.xxx"); return mgr; } @Bean(initMethod = "init", destroyMethod = "destroy") public DisconfMgrBeanSecond disconfMgrBeanSecond() { return new DisconfMgrBeanSecond(); } @Bean public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean() { ReloadablePropertiesFactoryBean prop = new ReloadablePropertiesFactoryBean(); String env=DisClientConfig.getInstance().ENV; String props[]={"classpath:xxx-xxx-conf-"+env+".properties"}; List<String> sList= Arrays.asList(props); prop.setLocations(sList); return prop; } @Bean public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { PropertyPlaceholderConfigurer prop = new PropertyPlaceholderConfigurer(); prop.setIgnoreResourceNotFound(true); prop.setIgnoreUnresolvablePlaceholders(true); try { prop.setPropertiesArray(reloadablePropertiesFactoryBean().getObject()); } catch (IOException e) { e.printStackTrace(); } return prop; } }
(4).啓動類spring
@SpringBootApplication public class StartApplication { public static void main(String[] args) { registerDisconfBean();//適用於無配置化註解方式注入bean new SpringApplicationBuilder(StartApplication.class).properties("spring.config.name:xxx-xxx-conf").build().run(args); } public static void registerDisconfBean() { GenericApplicationContext context = new AnnotationConfigApplicationContext(DisconfConfig.class); context.registerShutdownHook(); } }
(5).在須要用配置內容的地方注入sql
@Value("${xxx.url}") private String xxxUrl;
2、spring項目apache
(1).pom中引入JAR包依賴spring-mvc
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency>
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> </properties> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> </properties> </profile> </profiles>
<build> <finalName>xxx_xxx-${package.target}-${project.version}</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 協助maven打包的plugin --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>timestamp-property</id> <goals> <goal>timestamp-property</goal> </goals> </execution> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${basedir}/src/main/tools</source> <source>${basedir}/src/test/java</source> <!-- 咱們能夠經過在這裏添加多個source節點,來添加任意多個源文件夾 --> </sources> </configuration> </execution> </executions> <configuration> <name>current.time</name> <pattern>yyyy-MM-dd HH:mm:ss</pattern> <timeZone>GMT+8</timeZone> </configuration> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/classes</outputDirectory> <resources> <resource> <directory>Config/${package.target}</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> <execution> <id>copy-resources-main</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/classes</outputDirectory> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.1</version> <executions> <execution> <id>scss-replacement</id> <phase>prepare-package</phase> <goals> <goal>replace</goal> </goals> <configuration> <file>src/main/webapp/index.html</file> <!-- <outputFile>target/xxxx/index.html</outputFile> --> <token>#versionInfo</token> <!-- Replace to --> <value> 版本:${version} 時間:${current.time} 環境:${package.target} <!-- Maven自帶時間戳使用${maven.build.timestamp},可是時區是UTC --> </value> <outputFile>${project.build.directory}/index.html</outputFile> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <webResources> <resource> <directory>${project.build.directory}</directory> <filtering>true</filtering> <includes> <include>index.html</include> </includes> </resource> </webResources> </configuration> </plugin> </plugins> </build>
(2).項目Config目錄配置springboot
disconf.enable.remote.conf=true disconf.conf_server_host=config-dev.xxxx.net disconf.version=0.1.0 disconf.app=xxx-xxx disconf.env=dev disconf.enable_local_download_dir_in_class_path=true
(3).disconf.xml內容
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!--新增disconf配置--> <!-- 使用disconf必須添加如下配置 --> <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean" destroy-method="destroy"> <property name="scanPackage" value="xxx.xxx.xxx.xxx"/> </bean> <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond" init-method="init" destroy-method="destroy"> </bean> <!-- 使用託管方式的disconf配置(無代碼侵入, 配置更改不會自動reload)--> <bean id="configproperties_no_reloadable_disconf" class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean"> <property name="locations"> <list> <value>xxx-xxx-conf.properties</value> </list> </property> </bean> <bean id="propertyConfigurerForProject" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="propertiesArray"> <list> <ref bean="configproperties_no_reloadable_disconf"/> </list> </property> </bean> </beans>
(4).spring-init.xml內容
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName"> <!-- <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean> --> <import resource="classpath:spring/disconf.xml" /> <!-- 自動掃描的包名 --> <context:component-scan base-package="xxx.xxx.xxx.xxx" /> </beans>
(5).在須要用配置內容的地方注入
Spring XML配置中引用方式:${xxx.node-group} JAVA代碼中引用方式: @Value("${xxx.url}") private String xxxUrl;
注意:因爲spring和springmvc是兩個分開的容器;因此在controller引用時,須要在spring-mvc.xml配置文件中也引入disconf.xml;另外,springboot也可使用xml的方式引入disconf。
最後寄語,以上是博主本次文章的所有內容,若是你們以爲博主的文章還不錯,請點贊;若是您對博主其它服務器大數據技術或者博主本人感興趣,請關注博主博客,而且歡迎隨時跟博主溝通交流。 另外,下一期博主將爲你們帶來從disconf遷移到apollo的分享。