1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
@RestController @RequestMapping("/swarm") public class DockerSwarmController { @Autowired private DockerSwarmService swarmService; @Autowired private DockerSwarmNetworkService networkService; @Autowired private DockerSwarmMonitoringService monitoringService;
@PostMapping("/init") public ResponseEntity<Map<String, Object>> initSwarm() { try { SwarmInitResult result = swarmService.initSwarm(); Map<String, Object> response = new HashMap<>(); response.put("success", result.isSuccess()); response.put("swarmId", result.getSwarmId()); response.put("message", result.getMessage()); return ResponseEntity.ok(response); } catch (Exception e) { log.error("初始化Swarm集群失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "初始化集群失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@PostMapping("/service/create") public ResponseEntity<Map<String, Object>> createService(@RequestBody SwarmServiceRequest request) { try { SwarmService service = swarmService.createService(request); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("service", service); response.put("message", "服务创建成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("创建Swarm服务失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "创建服务失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@PostMapping("/service/scale") public ResponseEntity<Map<String, Object>> scaleService( @RequestParam String serviceId, @RequestParam int replicas) { try { SwarmScaleResult result = swarmService.scaleService(serviceId, replicas); Map<String, Object> response = new HashMap<>(); response.put("success", result.isSuccess()); response.put("replicas", result.getReplicas()); response.put("message", result.getMessage()); return ResponseEntity.ok(response); } catch (Exception e) { log.error("扩缩容Swarm服务失败: serviceId={}", serviceId, e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "扩缩容失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/services") public ResponseEntity<Map<String, Object>> getServices() { try { List<SwarmService> services = swarmService.getServices(); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("services", services); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取Swarm服务列表失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取服务列表失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/service/{serviceId}") public ResponseEntity<Map<String, Object>> getServiceDetail(@PathVariable String serviceId) { try { SwarmServiceDetail serviceDetail = swarmService.getServiceDetail(serviceId); Map<String, Object> response = new HashMap<>(); response.put("success", serviceDetail != null); response.put("serviceDetail", serviceDetail); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取Swarm服务详情失败: serviceId={}", serviceId, e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取服务详情失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@PostMapping("/network/create") public ResponseEntity<Map<String, Object>> createNetwork(@RequestBody SwarmNetworkRequest request) { try { SwarmNetwork network = networkService.createOverlayNetwork(request); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("network", network); response.put("message", "网络创建成功"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("创建覆盖网络失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "创建网络失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/networks") public ResponseEntity<Map<String, Object>> getNetworks() { try { List<SwarmNetwork> networks = networkService.getNetworks(); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("networks", networks); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取Swarm网络列表失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取网络列表失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/stats") public ResponseEntity<Map<String, Object>> getClusterStats() { try { SwarmClusterStats stats = monitoringService.getClusterStats(); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("stats", stats); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取Swarm集群统计信息失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取统计信息失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } }
|