1. 程式人生 > >SpringBoot構建微服務實戰 之 熱部署(二)

SpringBoot構建微服務實戰 之 熱部署(二)

在使用SpringBoot熱部署時可能會遇到一些異常比如熱部署設定完成生效之後,專案重啟會遇到快取重複存在的問題。

  • 在部署時會遇見一下異常:

    The source of the existing CacheManager is: InputStreamConfigurationSource [[email protected]]
    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 檔案。
    所以在熱部署完成重啟的過程中原來的快取並未被清除掉,所以會報快取重複存在異常。