springboot 集成disconf
关于分布式下的统一配置, 我们公司都用的是百度开源的disconf,虽然是一个半成品,但也能满足我们的需求,先看一下目录结构
首先 新建profile目录,下属两个文件夹,aliyun_prod 对应生产,test对应测试环境
test目录下采用yml格式来定义disconf的配置如下
disconf:
enable:
remote:
conf: true
conf_server_host: 127.0.0.1:3079
version: 1_0_0_0
app: paidui_soa
env: LOCAL
conf_server_url_retry_times: 2
conf_server_url_retry_sleep_seconds: 1
enable_local_download_dir_in_class_path: true
然后导入spring-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.xsd">
<!-- disconf 配置开始 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="smaug.service.provider"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
<bean id="configproperties_disconf"
class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<list>
<value>smaug_application.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="propertiesArray">
<list>
<ref bean="configproperties_disconf"/>
</list>
</property>
</bean>
</beans>
然后是spring-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-disconf.xml"/>
<import resource="classpath:spring-bean.xml"/>
</beans>
spring-bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bizConfig" class="smaug.config.bizConfig.BizConfig">
<property name="version" value="${smaug.version}"/>
</bean>
</beans>
导入启动类
package smaug.service.provider.starts;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import smaug.config.jerseyConfig.AnnotationJerseyConfig;
/**
* Created by naonao on 17/7/9.
*/
@SpringBootApplication
@ComponentScan(value = {"smaug.service", "smaug.util", "smaug.config"}) // 将接口在启动的时候注册进来
@EnableAutoConfiguration
@ImportResource({"classpath:spring-context.xml"})
public class TestStartApplication {
public static void main(String[] args) {
SpringApplication.run(TestStartApplication.class, args);
}
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, AnnotationJerseyConfig.class.getName());
return registration;
}
}
ImportResource 将xml引入即刻