结合Zookeeper实现配置管理中心

深入理解Zookeeper

Zookeeper本质上是提供一种集中式存储服务,可以作为注册中心,将服务地址注册在Zookeeper中以暴露给其他微服务,也可以作为统一的配置信息管理中心,利用Zookeeper的Watch机制可以实现对配置信息修改事件的监听并实时更新系统内已加载的配置项

第一,首先安装zkUI开源框架实时查看zookeeper中的数据

具体安装过程可以参考这篇博客:
https://www.cnblogs.com/zhangchengzi/p/10907595.html

第二步,结合SpringBoot完成配置管理中心

(1)引入pom依赖

        <!--java zookeeper 客户端-->
        <dependency>
            <groupId
异常提示:Servlet.service() for servlet [dispatcher] in context with path [/comsed1] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Bean object must not be null; nested exception is java.lang.IllegalArgumentException: Bean object must not be null] with root cause java.lang.IllegalArgumentException: Bean object must not be null at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:213) at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:202) at org.springframework.beans.BeanWrapperImpl.<init>(BeanWrapperImpl.java:151) at org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.<init>(DirectFieldAccessFallbackBeanWrapper.java:35) at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.getId(JpaMetamodelEntityInformation.java:117) at org.springframework.data.repository.core.support.AbstractEntityInformation.isNew(AbstractEntityInformation.java:51) at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.isNew(JpaMetamodelEntityInformation.java:196) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:376) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:405) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:390) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy136.save(Unknown Source) at iot.service.ProductMasterService.modifyProductMaster(ProductMasterService.java:32) at iot.controller.productMasterController.modifyProductMaster(productMasterController.java:95) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 代码部分: @RequestMapping(value = "modifyProductMasterInit") public String modifyProductMasterInit(@RequestParam("productId") String productId,Model model){ // Iterable<ProductMaster> productMasterList = productMasterDAO.findByProductId(productId); // model.addAttribute("productMasterList", productMasterList); ProductMaster productMaster = this.productMasterService.findByProductId(productId); model.addAttribute("productMaster", productMaster); return "productMaster/Pro_Master_modify"; } @RequestMapping(value = "modifyProductMaster") public String modifyProductMaster(ProductMaster productMaster,@RequestParam("productId") String productId){ ProductMaster productMasterOld = this.productMasterService.findByProductId(productId);//经断点调试productMasterOld为空 try { BeanUtils.copyProperties(productMaster, productMasterOld); } catch (Exception e) { e.printStackTrace(); } this.productMasterService.modifyProductMaster(productMasterOld); return "redirect:/productMaster/loadProductMaster"; } Service方法: @Service public class ProductMasterService { @Autowired(required = false) private ProductMasterDAO productMasterDAO; public void addproductMaster(ProductMaster productMaster) { this.productMasterDAO.save(productMaster); } public ProductMaster findByProductId(String productId) { return this.productMasterDAO.findByProductId(productId); } public void modifyProductMaster(ProductMaster productMaster) { this.productMasterDAO.save(productMaster); } } ProductMasterDAO部分: public interface ProductMasterDAO extends PagingAndSortingRepository<ProductMaster, String>,JpaSpecificationExecutor<ProductMaster>{ @Query("SELECT p FROM ProductMaster p WHERE p.productId = ?1") public ProductMaster findByProductId(String productId); }
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页