|
|
/**
|
|
|
* 云原生应用生命周期管理系统
|
|
|
* 支持应用的部署、扩展、更新和销毁等生命周期操作
|
|
|
*/
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
public class CloudNativeLifecycleManager {
|
|
|
// 应用实例存储
|
|
|
private final Map<String, Application> applications;
|
|
|
// 资源管理器
|
|
|
private final ResourceManager resourceManager;
|
|
|
// 事件监听器列表
|
|
|
private final List<LifecycleEventListener> eventListeners;
|
|
|
// 单例实例
|
|
|
private static volatile CloudNativeLifecycleManager instance;
|
|
|
|
|
|
// 生命周期事件监听器接口
|
|
|
public interface LifecycleEventListener {
|
|
|
void onApplicationCreated(Application app);
|
|
|
void onApplicationDeployed(Application app);
|
|
|
void onApplicationScaled(Application app, int oldReplicas, int newReplicas);
|
|
|
void onApplicationUpdated(Application app, ApplicationConfig oldConfig);
|
|
|
void onApplicationDestroyed(String appId);
|
|
|
}
|
|
|
|
|
|
// 应用配置类
|
|
|
public static class ApplicationConfig {
|
|
|
private String name;
|
|
|
private String image;
|
|
|
private int replicas;
|
|
|
private Map<String, String> envVars;
|
|
|
private Map<String, Integer> resources;
|
|
|
private List<PortMapping> ports;
|
|
|
|
|
|
public ApplicationConfig(String name, String image) {
|
|
|
this.name = name;
|
|
|
this.image = image;
|
|
|
this.replicas = 1;
|
|
|
this.envVars = new HashMap<>();
|
|
|
this.resources = new HashMap<>();
|
|
|
this.ports = new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
// Getters and Setters
|
|
|
public String getName() { return name; }
|
|
|
public void setName(String name) { this.name = name; }
|
|
|
public String getImage() { return image; }
|
|
|
public void setImage(String image) { this.image = image; }
|
|
|
public int getReplicas() { return replicas; }
|
|
|
public void setReplicas(int replicas) { this.replicas = replicas; }
|
|
|
public Map<String, String> getEnvVars() { return envVars; }
|
|
|
public void setEnvVars(Map<String, String> envVars) { this.envVars = envVars; }
|
|
|
public Map<String, Integer> getResources() { return resources; }
|
|
|
public void setResources(Map<String, Integer> resources) { this.resources = resources; }
|
|
|
public List<PortMapping> getPorts() { return ports; }
|
|
|
public void setPorts(List<PortMapping> ports) { this.ports = ports; }
|
|
|
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return "ApplicationConfig{name='" + name + "', image='" + image + "', replicas=" + replicas + "}";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 端口映射类
|
|
|
public static class PortMapping {
|
|
|
private int containerPort;
|
|
|
private int hostPort;
|
|
|
private String protocol;
|
|
|
|
|
|
public PortMapping(int containerPort, int hostPort, String protocol) {
|
|
|
this.containerPort = containerPort;
|
|
|
this.hostPort = hostPort;
|
|
|
this.protocol = protocol;
|
|
|
}
|
|
|
|
|
|
public int getContainerPort() { return containerPort; }
|
|
|
public int getHostPort() { return hostPort; }
|
|
|
public String getProtocol() { return protocol; }
|
|
|
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return hostPort + ":" + containerPort + "/" + protocol;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 应用实例类
|
|
|
public static class Application {
|
|
|
private final String id;
|
|
|
private ApplicationConfig config;
|
|
|
private String status;
|
|
|
private List<Pod> pods;
|
|
|
private final Map<String, String> metadata;
|
|
|
private final String createdAt;
|
|
|
private String updatedAt;
|
|
|
|
|
|
public Application(String id, ApplicationConfig config) {
|
|
|
this.id = id;
|
|
|
this.config = config;
|
|
|
this.status = "PENDING";
|
|
|
this.pods = new ArrayList<>();
|
|
|
this.metadata = new HashMap<>();
|
|
|
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
this.createdAt = timestamp;
|
|
|
this.updatedAt = timestamp;
|
|
|
}
|
|
|
|
|
|
// Getters and Setters
|
|
|
public String getId() { return id; }
|
|
|
public ApplicationConfig getConfig() { return config; }
|
|
|
public void setConfig(ApplicationConfig config) { this.config = config; }
|
|
|
public String getStatus() { return status; }
|
|
|
public void setStatus(String status) { this.status = status; }
|
|
|
public List<Pod> getPods() { return pods; }
|
|
|
public void setPods(List<Pod> pods) { this.pods = pods; }
|
|
|
public Map<String, String> getMetadata() { return metadata; }
|
|
|
public String getCreatedAt() { return createdAt; }
|
|
|
public String getUpdatedAt() { return updatedAt; }
|
|
|
public void setUpdatedAt(String updatedAt) { this.updatedAt = updatedAt; }
|
|
|
}
|
|
|
|
|
|
// Pod类(容器组)
|
|
|
public static class Pod {
|
|
|
private final String id;
|
|
|
private final String appId;
|
|
|
private String nodeId;
|
|
|
private String status;
|
|
|
private final String createdAt;
|
|
|
|
|
|
public Pod(String id, String appId) {
|
|
|
this.id = id;
|
|
|
this.appId = appId;
|
|
|
this.status = "PENDING";
|
|
|
this.createdAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
}
|
|
|
|
|
|
public String getId() { return id; }
|
|
|
public String getAppId() { return appId; }
|
|
|
public String getNodeId() { return nodeId; }
|
|
|
public void setNodeId(String nodeId) { this.nodeId = nodeId; }
|
|
|
public String getStatus() { return status; }
|
|
|
public void setStatus(String status) { this.status = status; }
|
|
|
public String getCreatedAt() { return createdAt; }
|
|
|
}
|
|
|
|
|
|
// 资源管理器
|
|
|
private static class ResourceManager {
|
|
|
private final Map<String, Integer> availableResources;
|
|
|
|
|
|
public ResourceManager() {
|
|
|
this.availableResources = new HashMap<>();
|
|
|
// 模拟初始资源
|
|
|
availableResources.put("cpu", 16);
|
|
|
availableResources.put("memory", 65536); // 64GB
|
|
|
availableResources.put("disk", 1048576); // 1TB
|
|
|
}
|
|
|
|
|
|
public synchronized boolean allocateResources(Map<String, Integer> required) {
|
|
|
// 检查资源是否足够
|
|
|
for (Map.Entry<String, Integer> entry : required.entrySet()) {
|
|
|
Integer available = availableResources.getOrDefault(entry.getKey(), 0);
|
|
|
if (available < entry.getValue()) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 分配资源
|
|
|
for (Map.Entry<String, Integer> entry : required.entrySet()) {
|
|
|
availableResources.put(entry.getKey(),
|
|
|
availableResources.get(entry.getKey()) - entry.getValue());
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public synchronized void releaseResources(Map<String, Integer> allocated) {
|
|
|
for (Map.Entry<String, Integer> entry : allocated.entrySet()) {
|
|
|
availableResources.put(entry.getKey(),
|
|
|
availableResources.getOrDefault(entry.getKey(), 0) + entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Map<String, Integer> getAvailableResources() {
|
|
|
return new HashMap<>(availableResources);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private CloudNativeLifecycleManager() {
|
|
|
this.applications = new ConcurrentHashMap<>();
|
|
|
this.resourceManager = new ResourceManager();
|
|
|
this.eventListeners = new ArrayList<>();
|
|
|
System.out.println("云原生应用生命周期管理系统初始化完成");
|
|
|
}
|
|
|
|
|
|
// 获取单例实例
|
|
|
public static CloudNativeLifecycleManager getInstance() {
|
|
|
if (instance == null) {
|
|
|
synchronized (CloudNativeLifecycleManager.class) {
|
|
|
if (instance == null) {
|
|
|
instance = new CloudNativeLifecycleManager();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return instance;
|
|
|
}
|
|
|
|
|
|
// 注册事件监听器
|
|
|
public void addEventListener(LifecycleEventListener listener) {
|
|
|
if (listener != null && !eventListeners.contains(listener)) {
|
|
|
eventListeners.add(listener);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 创建应用
|
|
|
public Application createApplication(ApplicationConfig config) {
|
|
|
String appId = "app-" + UUID.randomUUID().toString().substring(0, 8);
|
|
|
Application app = new Application(appId, config);
|
|
|
applications.put(appId, app);
|
|
|
|
|
|
System.out.println("应用创建: " + appId + ", 配置: " + config);
|
|
|
|
|
|
// 通知监听器
|
|
|
for (LifecycleEventListener listener : eventListeners) {
|
|
|
listener.onApplicationCreated(app);
|
|
|
}
|
|
|
|
|
|
return app;
|
|
|
}
|
|
|
|
|
|
// 部署应用
|
|
|
public boolean deployApplication(String appId) {
|
|
|
Application app = applications.get(appId);
|
|
|
if (app == null) {
|
|
|
System.err.println("应用不存在: " + appId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 分配资源
|
|
|
Map<String, Integer> resources = app.getConfig().getResources();
|
|
|
if (!resourceManager.allocateResources(resources)) {
|
|
|
System.err.println("资源不足,无法部署应用: " + appId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 创建Pod实例
|
|
|
List<Pod> pods = new ArrayList<>();
|
|
|
for (int i = 0; i < app.getConfig().getReplicas(); i++) {
|
|
|
String podId = "pod-" + UUID.randomUUID().toString().substring(0, 8);
|
|
|
Pod pod = new Pod(podId, appId);
|
|
|
pod.setNodeId("node-" + (i % 3 + 1)); // 模拟节点分配
|
|
|
pod.setStatus("RUNNING");
|
|
|
pods.add(pod);
|
|
|
System.out.println("Pod创建: " + podId + " 应用: " + appId + " 节点: " + pod.getNodeId());
|
|
|
}
|
|
|
|
|
|
app.setPods(pods);
|
|
|
app.setStatus("RUNNING");
|
|
|
app.setUpdatedAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
|
|
System.out.println("应用部署成功: " + appId + " 副本数: " + pods.size());
|
|
|
|
|
|
// 通知监听器
|
|
|
for (LifecycleEventListener listener : eventListeners) {
|
|
|
listener.onApplicationDeployed(app);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 扩展应用
|
|
|
public boolean scaleApplication(String appId, int replicas) {
|
|
|
Application app = applications.get(appId);
|
|
|
if (app == null) {
|
|
|
System.err.println("应用不存在: " + appId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (replicas <= 0) {
|
|
|
System.err.println("副本数必须大于0: " + replicas);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
int oldReplicas = app.getPods().size();
|
|
|
if (oldReplicas == replicas) {
|
|
|
System.out.println("应用已经是指定的副本数: " + appId + " replicas=" + replicas);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if (replicas > oldReplicas) {
|
|
|
// 扩容
|
|
|
for (int i = oldReplicas; i < replicas; i++) {
|
|
|
String podId = "pod-" + UUID.randomUUID().toString().substring(0, 8);
|
|
|
Pod pod = new Pod(podId, appId);
|
|
|
pod.setNodeId("node-" + (i % 3 + 1));
|
|
|
pod.setStatus("RUNNING");
|
|
|
app.getPods().add(pod);
|
|
|
System.out.println("扩容Pod创建: " + podId + " 应用: " + appId);
|
|
|
}
|
|
|
} else {
|
|
|
// 缩容
|
|
|
List<Pod> pods = app.getPods();
|
|
|
while (pods.size() > replicas) {
|
|
|
Pod podToRemove = pods.remove(pods.size() - 1);
|
|
|
System.out.println("缩容Pod删除: " + podToRemove.getId() + " 应用: " + appId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
app.getConfig().setReplicas(replicas);
|
|
|
app.setUpdatedAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
|
|
System.out.println("应用扩展成功: " + appId + " 从 " + oldReplicas + " 到 " + replicas + " 副本");
|
|
|
|
|
|
// 通知监听器
|
|
|
for (LifecycleEventListener listener : eventListeners) {
|
|
|
listener.onApplicationScaled(app, oldReplicas, replicas);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 更新应用配置
|
|
|
public boolean updateApplication(String appId, ApplicationConfig newConfig) {
|
|
|
Application app = applications.get(appId);
|
|
|
if (app == null) {
|
|
|
System.err.println("应用不存在: " + appId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
ApplicationConfig oldConfig = app.getConfig();
|
|
|
|
|
|
// 保存旧配置的副本
|
|
|
ApplicationConfig oldConfigCopy = new ApplicationConfig(oldConfig.getName(), oldConfig.getImage());
|
|
|
oldConfigCopy.setReplicas(oldConfig.getReplicas());
|
|
|
oldConfigCopy.setEnvVars(new HashMap<>(oldConfig.getEnvVars()));
|
|
|
oldConfigCopy.setResources(new HashMap<>(oldConfig.getResources()));
|
|
|
oldConfigCopy.setPorts(new ArrayList<>(oldConfig.getPorts()));
|
|
|
|
|
|
// 更新配置
|
|
|
app.setConfig(newConfig);
|
|
|
app.setUpdatedAt(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
|
|
// 如果副本数改变,重新调度
|
|
|
if (newConfig.getReplicas() != oldConfig.getReplicas()) {
|
|
|
scaleApplication(appId, newConfig.getReplicas());
|
|
|
}
|
|
|
|
|
|
System.out.println("应用更新成功: " + appId + " 配置从 " + oldConfig + " 更新到 " + newConfig);
|
|
|
|
|
|
// 通知监听器
|
|
|
for (LifecycleEventListener listener : eventListeners) {
|
|
|
listener.onApplicationUpdated(app, oldConfigCopy);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 销毁应用
|
|
|
public boolean destroyApplication(String appId) {
|
|
|
Application app = applications.remove(appId);
|
|
|
if (app == null) {
|
|
|
System.err.println("应用不存在: " + appId);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 释放资源
|
|
|
resourceManager.releaseResources(app.getConfig().getResources());
|
|
|
|
|
|
// 删除所有Pod
|
|
|
System.out.println("销毁应用: " + appId + " 删除Pod数: " + app.getPods().size());
|
|
|
|
|
|
// 通知监听器
|
|
|
for (LifecycleEventListener listener : eventListeners) {
|
|
|
listener.onApplicationDestroyed(appId);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 获取应用列表
|
|
|
public List<Application> getApplications() {
|
|
|
return new ArrayList<>(applications.values());
|
|
|
}
|
|
|
|
|
|
// 获取应用详情
|
|
|
public Application getApplication(String appId) {
|
|
|
return applications.get(appId);
|
|
|
}
|
|
|
|
|
|
// 获取系统资源状态
|
|
|
public Map<String, Integer> getResourceStatus() {
|
|
|
return resourceManager.getAvailableResources();
|
|
|
}
|
|
|
} |