背景:很多老旧项目改造时,需要接入配置中心; 经过团队评估,借助disconf来实现过渡到其他配置中心比较简单; 所以博主在此做个分享;http://www.fu-w.com/a/63818.html
一、springboot项目
(1).pom中引入JAR包依赖
<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目录配置
创建文件夹:/Config/${profileActive} ----其中profileActive为pom中配置的环境参数:dev/test/pre/prod
目录下disconf.properties配置文件内容如下:
disconf.enable.remote.conf=true
disconf.conf_server_host=config-online.xxxx.comhttp://www.fu-w.com/a/63817.html
disconf.version=0.0.1 disconf.app=louvre-order disconf.env=prod disconf.enable_local_download_dir_in_class_path=true
(3).代码中编写disconf配置
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).启动类
@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).在需要用配置内容的地方注入
@Value("${xxx.url}")
private String xxxUrl;
二、spring项目
(1).pom中引入JAR包依赖
<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> </