Destiny.Lin
6 months ago
13 changed files with 307 additions and 93 deletions
@ -1,4 +1,4 @@
|
||||
package com.fr.start; |
||||
package com.fr.design.backup; |
||||
|
||||
import com.fanruan.carina.context.CarinaApplicationContext; |
||||
import com.fanruan.carina.standard.PartitionManager; |
@ -0,0 +1,117 @@
|
||||
package com.fr.design.backup; |
||||
|
||||
import com.fanruan.carina.Carina; |
||||
import com.fanruan.config.realm.ConfigRepositoryFactory; |
||||
import com.fanruan.config.realm.local.LocalConfigRepositoryBuilder; |
||||
import com.fr.design.ConfigHelper; |
||||
import com.fr.design.mem.MemConfigBackupManager; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.workspace.WorkContext; |
||||
import com.fr.workspace.Workspace; |
||||
|
||||
import java.util.Properties; |
||||
|
||||
/** |
||||
* 切换环境对应的帮助类,主要负责环境切换的备份与环境属性的设置 |
||||
* |
||||
* @author Destiny.Lin |
||||
* @since 11.0 |
||||
* Created on 2024/7/12 |
||||
*/ |
||||
public class EnvBackupHelper { |
||||
private static final EnvBackupHelper INSTANCE = new EnvBackupHelper(); |
||||
private boolean swtiching = false; |
||||
private boolean local = true; |
||||
private Properties properties = null; |
||||
private String path = null; |
||||
private Workspace target; |
||||
private Workspace origin; |
||||
/** |
||||
* 获取单例 |
||||
*/ |
||||
public static EnvBackupHelper getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 备份 |
||||
* <p>切换前需要判断,为什么?——当前的配置框架需要在切换前做好备份,防止切换失败,失败的话要切回备份</p> |
||||
*/ |
||||
public void backup() { |
||||
FineLoggerFactory.getLogger().info("[EnvBackup] 1.back start..."); |
||||
this.swtiching = true; |
||||
local = WorkContext.getCurrent().isLocal(); |
||||
if (local) { |
||||
properties = ConfigHelper.getEnvProperties(); |
||||
path = ConfigHelper.getEnvPropertiesPath(); |
||||
} |
||||
origin = WorkContext.getCurrent(); |
||||
} |
||||
|
||||
/** |
||||
* 切换前的准备 |
||||
* <p>设置目标环境</p> |
||||
* <p>设置新的工程路径</p> |
||||
*/ |
||||
public void prepare4switch(Workspace workspace) { |
||||
this.target = workspace; |
||||
((DesignContext) Carina.getApplicationContext()).setDesignWebInfPath(workspace.getPath()); |
||||
} |
||||
|
||||
/** |
||||
* 切换 |
||||
*/ |
||||
public void do4Switch4Config() { |
||||
FineLoggerFactory.getLogger().info("[EnvBackup] 2.do config switch"); |
||||
// 切换环境的时候新配置框架启动前要先reset原来的
|
||||
ConfigRepositoryFactory.getInstance().reset(); |
||||
if (target.isLocal()) { |
||||
//如果目标是本地,就清空然后重新注册新的本地配置
|
||||
ConfigRepositoryFactory.getInstance().registerConfigRepositoryBuilder(new LocalConfigRepositoryBuilder(ConfigHelper.getEnvProperties(target), ConfigHelper.getEnvPropertiesPath(target))); |
||||
} else { |
||||
// 如果是切到远程,先暂时用备份的内存配置,成功后再转到新配置,失败则切回原内存配置
|
||||
MemConfigBackupManager.getInstance().startSwitch(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 切换成功后的处理 |
||||
*/ |
||||
public void success() { |
||||
FineLoggerFactory.getLogger().info("[EnvBackup] 3.switch all" ); |
||||
//切换成功,如果原来是本地,则清空备份,不需要干别的,此时配置走的就是新环境的本地
|
||||
if (local) { |
||||
properties = null; |
||||
path = null; |
||||
} else { |
||||
// 如果原来是远程,就把内存的新环境配置替换掉原来旧的,然后新配置清空
|
||||
MemConfigBackupManager.getInstance().success(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 切换失败后的处理 |
||||
*/ |
||||
public void failed() { |
||||
FineLoggerFactory.getLogger().info("[EnvBackup] 3.handle failed" ); |
||||
((DesignContext) Carina.getApplicationContext()).setDesignWebInfPath(origin.getPath()); |
||||
// 切换失败,如果原来是本地,则切回去
|
||||
ConfigRepositoryFactory.getInstance().reset(); |
||||
if (local) { |
||||
ConfigRepositoryFactory.getInstance().registerConfigRepositoryBuilder(new LocalConfigRepositoryBuilder(properties, path)); |
||||
} else { |
||||
// 如果是远程,则新环境的配置全部弃用,直接走老的
|
||||
MemConfigBackupManager.getInstance().failed(); |
||||
} |
||||
|
||||
} |
||||
|
||||
public boolean isSwtiching() { |
||||
return swtiching; |
||||
} |
||||
|
||||
public EnvBackupHelper setSwtiching(boolean swtiching) { |
||||
this.swtiching = swtiching; |
||||
return this; |
||||
} |
||||
} |
@ -0,0 +1,88 @@
|
||||
package com.fr.design.mem; |
||||
|
||||
import com.fanruan.config.realm.ConfigRepositoryProvider; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* 内存配置框架备份管理 |
||||
* |
||||
* @author Destiny.Lin |
||||
* @since 11.0 |
||||
* Created on 2024/7/11 |
||||
*/ |
||||
public class MemConfigBackupManager { |
||||
|
||||
private static final MemConfigBackupManager INSTANCE = new MemConfigBackupManager(); |
||||
|
||||
private Map<String, ConfigRepositoryProvider> oldEnvConfig = new ConcurrentHashMap<>(); |
||||
|
||||
private Map<String, ConfigRepositoryProvider> newEnvConfig = new ConcurrentHashMap<>(); |
||||
|
||||
private boolean switching = false; |
||||
|
||||
/** |
||||
* 获取备份 |
||||
*/ |
||||
public static MemConfigBackupManager getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 获取配置 |
||||
*/ |
||||
public ConfigRepositoryProvider get(String namespace) { |
||||
if (switching) { |
||||
return newEnvConfig.getOrDefault(namespace, null); |
||||
} |
||||
return oldEnvConfig.getOrDefault(namespace, null); |
||||
} |
||||
|
||||
/** |
||||
* 放入配置 |
||||
*/ |
||||
public void put(String namespace, ConfigRepositoryProvider provider) { |
||||
if (switching) { |
||||
newEnvConfig.put(namespace, provider); |
||||
} else { |
||||
oldEnvConfig.put(namespace, provider); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 清理 |
||||
*/ |
||||
public void clear() { |
||||
oldEnvConfig.clear(); |
||||
newEnvConfig.clear(); |
||||
switching = false; |
||||
} |
||||
|
||||
/** |
||||
* 切换成功则将切换过程中的配置移动到旧配置,新的切换过程中的配置置空 |
||||
*/ |
||||
public void success() { |
||||
oldEnvConfig = new HashMap<>(newEnvConfig); |
||||
newEnvConfig.clear(); |
||||
switching = false; |
||||
} |
||||
|
||||
public void failed() { |
||||
newEnvConfig.clear(); |
||||
switching = false; |
||||
} |
||||
|
||||
public boolean isSwitching() { |
||||
return switching; |
||||
} |
||||
|
||||
/** |
||||
* 开始切换 |
||||
*/ |
||||
public void startSwitch() { |
||||
switching = true; |
||||
} |
||||
} |
@ -1,4 +1,4 @@
|
||||
package com.fanruan.boot.mem; |
||||
package com.fr.design.mem; |
||||
|
||||
import com.fanruan.config.event.ConfigListener; |
||||
import com.fanruan.config.realm.ConfigRepositoryProvider; |
@ -0,0 +1,30 @@
|
||||
package com.fr.design.mem; |
||||
|
||||
import com.fanruan.config.realm.ConfigRepositoryBuilder; |
||||
import com.fanruan.config.realm.ConfigRepositoryProvider; |
||||
|
||||
|
||||
|
||||
/** |
||||
* 设计器MemConfigRepositoryBuilder |
||||
* |
||||
* @author Destiny.Lin |
||||
* @since 11.0 |
||||
* Created on 2024/5/29 |
||||
*/ |
||||
public class MemConfigRepositoryBuilder implements ConfigRepositoryBuilder { |
||||
@Override |
||||
public ConfigRepositoryProvider build(String namespace) throws Exception { |
||||
ConfigRepositoryProvider provider = MemConfigBackupManager.getInstance().get(namespace); |
||||
if (provider != null) { |
||||
// 如果备份中有说明就是当前要的配置
|
||||
return provider; |
||||
} else { |
||||
// 如果没有,说明是全新的远程环境,新建一个
|
||||
provider = new MemConfigRepository(namespace); |
||||
// 备份也得放一个
|
||||
MemConfigBackupManager.getInstance().put(namespace, provider); |
||||
} |
||||
return provider; |
||||
} |
||||
} |
@ -1,20 +0,0 @@
|
||||
package com.fanruan.boot.mem; |
||||
|
||||
import com.fanruan.config.realm.ConfigRepositoryBuilder; |
||||
import com.fanruan.config.realm.ConfigRepositoryProvider; |
||||
|
||||
|
||||
|
||||
/** |
||||
* 设计器MemConfigRepositoryBuilder |
||||
* |
||||
* @author Destiny.Lin |
||||
* @since 11.0 |
||||
* Created on 2024/5/29 |
||||
*/ |
||||
public class MemConfigRepositoryBuilder implements ConfigRepositoryBuilder { |
||||
@Override |
||||
public ConfigRepositoryProvider build(String namespace) throws Exception { |
||||
return new MemConfigRepository(namespace); |
||||
} |
||||
} |
Loading…
Reference in new issue