资料摘自:http://www.big-mouth.cn/blog/65.html
背景说明:在基于Spring MVC框架的开发的项目中,由于存在大量的数据库配置、第三方配置信息、环境路径配置信息,一直使用Maven的profile方式来对各种配置信息进行管理(该方式不多做介绍),在维护方面成本较高。针对以上情况,准备借助zookeeper来对不同配置信息进行统一管理(当然其他缓存工具也可以,原理相同)。
第一步:安装zookeeper服务端(资料较多,这里不多解释)
第二步:在java中引入apache.zookeeper.ZooKeeper第三方API。
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency>
第三步:编写zookeeper工具类(简单工具类)
package cn.com.timerbin.zookeeper.demo; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; public class ZookeeperClientUtil { private ZooKeeper zk; //zookeeper地址 private String servers; //链接超时时间 private int sessionTimeout = 40000; private String mainPath; public ZooKeeper getAliveZk() { ZooKeeper aliveZk = zk; if (aliveZk != null && aliveZk.getState().isAlive()) { return aliveZk; } else { zkReconnect(); return zk; } } public synchronized void zkReconnect() { close(); try { connect(); } catch (IOException e) { } } public synchronized void close() { if (zk != null) { try { zk.close(); } catch (InterruptedException e) { } zk = null; } } private synchronized void connect() throws IOException { if (zk == null && !StringUtils.isBlank(servers)) zk = new ZooKeeper(servers, sessionTimeout, null); } public String getData(String path) { String result = null; try { byte [] data = getAliveZk().getData(path, Boolean.TRUE,null); if(null != data){ result = new String(data, "UTF-8"); } } catch (KeeperException e) { } catch (InterruptedException e) { } catch (UnsupportedEncodingException e) { } return result ; } public List<String> getChildren(){ List<String> data = null; try { data = getAliveZk().getChildren(mainPath, Boolean.TRUE); } catch (KeeperException e) { } catch (InterruptedException e) { } return data; } public void setSessionTimeout(int sessionTimeout) { this.sessionTimeout = sessionTimeout; } public String getMainPath() { return mainPath; } public void setMainPath(String mainPath) { this.mainPath = mainPath; } public void setServers(String servers) { this.servers = servers; } }
注:此工具类仅供参考,请注意其中 zk.getChildren()/zk.getData()方法中的Watcher参数使用。
第四步:Spring配置文件中对ZookeeperClientUtil 配置定义
<bean id="zookeeperClient" class="cn.com.timerbin.zookeeper.demo.ZookeeperClientUtil"> <property name="servers" value="127.0.0.1:2181" /> <property name="mainPath" value="/configuration/allconfig" /> </bean>
第五步:重写配置文件配置器
package cn.com.timerbin.zookeeper.demo; import java.util.List; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyOverrideConfigurer; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import cn.com.timerbin.zookeeper.demo.ZookeeperClientUtil; public class ZooKeeperPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private ZookeeperClientUtil configurationClient; public void setConfigurationClient(ZookeeperClientUtil configurationClient) { this.configurationClient = configurationClient; } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { try { List<String> list= configurationClient.getChildren(); for(String key:list){ String value = configurationClient.getData(configurationClient.getMainPath()+"/"+key); if(!StringUtils.isBlank(value)){ props.put(key,value); } } } catch (Exception e) { } super.processProperties(beanFactoryToProcess, props); } }
第六步:在Spring配置文件中对ZooKeeperPropertyPlaceholderConfigurer进行配置定义
<bean id="zooKeeperPropertyPlaceholderConfigurer" class="cn.com.timerbin.zookeeper.demo.ZooKeeperPropertyPlaceholderConfigurer"> <property name="configurationClient" ref="zookeeperClient"></property> <property name="order" value="1" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean>
其中:ignoreUnresolvablePlaceholders:配置即当此PropertyPlaceholder不可解析时是否忽略。
注:如果在第六步中配置中配置了如下信息时
<property name="locations"> <list> <value>classpath:application.properties</value> </list> </property>
此时在第五部中的processProperties方法的props中就已经包含了application.properties配置中的所有配置信息,关于工具类(ZookeeperClientUtil)中的一些配置参数的设置也可以考虑基于props来进行定义设置!
相关推荐
dubbo项目的配置,以及dubbo与zookeeper的配合使用,dubbo结合spring mvc的使用
本软件为基于分布式的信号算法可视化数据检测系统,使用Spring MVC + Dubbo + Zookeeper技术,实现分布式环境下算法数据可视化检测 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都...
spring+spring mvc+mybatis+mysql+dubbo整合开发任务流程后台管理系统.zip
ftp服务端和客户端工具类,配置文件maven和web项目路径统一 1.1.0 soapui工具类(web版本) properties等工具类 1.1.1 工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 ...
视图框架:Spring MVC 4.3.5 任务调度:Spring + Quartz 2.2.3 持久层框架:MyBatis 3.4.2 + Mybatis-plus 2.0.1 数据库连接池:Alibaba Druid 1.0 缓存框架:Ehcache 2.6 + Redis 2.9.0 日志管理:SLF4J 1.7 + Log4...
2022年最新Java面试题整理,全网最全一份,包含各个技术栈,Java基础,Java集合,多线程,Java异常,spring,spring MVC,spring boot,spring cloud,netty,dubbo,activemq,Java web,jvm,kafka,MongoDB,...
dubbo作为国内顶尖大厂阿里的开源分布式服务框架,他有很多优势和用途,配合zookeeper整合入Spring中,相得益彰。自动发现,服务管理,提供者,消费者
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
使用Servlet容器运行(Tomcat、Jetty等),需要在web.xml中装配spring.xml、spring-dao、以及provider配置文件 缺点:增加复杂性(端口、管理、浪费资源(内存) 、需要占用多个端口和内存,不建议 第二种方式: ...
Spring+Spring MVC+Mybatis+dubbo+zookeeper项目搭建源码资料
6.1 提供 Spring MVC自动配置 11 6.2 对静态资源的支持 11 6.3 模板引擎 12 6.3.1 Thymeleaf模板引擎 12 ....... Springcloud: 通过整合SpringMVC+SpringBoot+Mybatis构建一个可用的基于SpringCloud的...
实现了对Zookeeper实例的统一管理。 前台使用了Bootstrap,基于gentelella主题实现。后台使用了Spring MVC,Mybatis,Curator等技术。Jar包管理通过Maven来实现。数据库选用了Mysql。 下载安装 下载 通过如下命令从...
dubbo+zookeeper+SpringMVC实现提供者消费者代码
本书系统介绍了Spring Boot 2的主要技术,侧重于两个方面,一方面是极速开发一个Web应用系统,详细介绍Spring Boot框架、Spring MVC、视图技术、数据库访问技术,并且介绍多环境部署、自动装配、单元测试等高级特性...
内容丰富,涵盖Spring Boot框架、Spring MVC、视图技术、数据库访问技术,并且介绍多环境部署、自动装配、单元测试等高级特性,包括使用Spring实现RESTful架构,在Spring Boot框架下使用Redis、MongoDB、ZooKeeper、...
1、Spring Boot 她其实就是整合了Spring MVC和Spring减少了配置的xml用注解和application.properties代替了.xml,其次他还内嵌可tomcat可以用Main函数替代了tomcat。 2、Dubbo又阿里开发的典型的分布式处理框架 ,...
ftp服务端和客户端工具类,配置文件maven和web项目路径统一 1.1.0 soapui工具类(web版本) properties等工具类 1.1.1 工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 ...
资源分为十九个模块,分别是: Java 基础、容器、多线程、反射、对象拷贝、Java Web 模块、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、Mybatis、RabbitMQ、Kafka、Zookeeper、...
视图框架:Spring MVC 定时器:Quartz 数据库连接池:Druid 日志管理:logback 页面交互:layui 下拉框:bootstrap-select 文件上传:Bootstrap File Input 热部署 jrebel 验证框架 hibernate-validator Java商城...
ftp服务端和客户端工具类,配置文件maven和web项目路径统一 1.1.0 soapui工具类(web版本) properties等工具类 1.1.1 工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 ...