spring|springboot项目接入disconf配置中心

  背景:很多老旧项目改造时,需要接入配置中心; 经过团队评估,借助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> </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目录配置

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.fu-w.com/a/63815.html" xsi:schemaLocation="http://www.fu-w.com/a/63812.html 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.fu-w.com/a/63810.html 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的分享。

http://www.fu-w.com/a/63818.html

猜你喜欢

转载自www.cnblogs.com/meilideni/p/13160829.html