SpringBoot构建微服务实战 之 热部署(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012437781/article/details/83622202

在使用SpringBoot热部署时可能会遇到一些异常比如热部署设置完成生效之后,项目重启会遇到缓存重复存在的问题。

  • 在部署时会遇见一下异常:

    The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@13ceee8c]
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRealm' defined in class path resource [cn/mirak/framework/config/ShiroConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.mirak.framework.shiro.realm.UserRealm]: Factory method 'userRealm' threw exception; nested exception is org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another CacheManager with same name 'em' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
    1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
    2. Shutdown the earlier cacheManager before creating new one with same name.
    
  • 解决办法:

     @Bean
        public EhCacheManager getEhCacheManager()
        {
        	net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("impact");
        	EhCacheManager em = new EhCacheManager();
        	if(ObjectUtils.isEmpty(cacheManager)) {
                em.setCacheManagerConfigFile("classpath:ehcache/ehcache-shiro.xml");
                return em;
        	} else {
        		em.setCacheManager(cacheManager);
        		return em;
        	}
    
  • 原因:
    这里要特别注意下:由于热部署是监听 Class 文件的变化,它自身不会主动去编译 Java 文件,所以我们得在 Java 文件改动时,自动编译成 Class 文件,然后热部署工具创造的新的类加载器才会加载改变后的 Class 文件。
    所以在热部署完成重启的过程中原来的缓存并未被清除掉,所以会报缓存重复存在异常。

猜你喜欢

转载自blog.csdn.net/u012437781/article/details/83622202