Repository navigation
Expand file tree
/
Copy pathExpBlueGreen.java
More file actions
112 lines (90 loc) · 3.96 KB
/
Copy pathExpBlueGreen.java
File metadata and controls
112 lines (90 loc) · 3.96 KB
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
import java.util.*;
public class ExpBlueGreen {
static class Environment {
private final String name;
private String version;
private boolean live;
Environment(String name, String version, boolean live) {
this.name = name;
this.version = version;
this.live = live;
}
void deploy(String newVersion) {
System.out.printf(" [DEPLOY] %s ← version %s%n", name, newVersion);
this.version = newVersion;
}
// Simulated smoke test: always passes unless version contains "broken"
boolean smokeTest() {
boolean ok = !version.contains("broken");
System.out.printf(" [SMOKE TEST] %s (version=%s) → %s%n",
name, version, ok ? "PASS" : "FAIL");
return ok;
}
String getName() { return name; }
String getVersion() { return version; }
boolean isLive() { return live; }
void setLive(boolean live) { this.live = live; }
}
static class LoadBalancer {
private Environment active;
private Environment standby;
LoadBalancer(Environment blue, Environment green) {
if (blue.isLive()) { active = blue; standby = green; }
else { active = green; standby = blue; }
}
// One atomic switch: old active → standby, new active gets traffic
void switchTraffic() {
active.setLive(false);
standby.setLive(true);
Environment tmp = active;
active = standby;
standby = tmp;
System.out.printf(" [LB SWITCH] Tráfico → %s (version=%s) | standby: %s (version=%s)%n",
active.getName(), active.getVersion(),
standby.getName(), standby.getVersion());
}
void rollback() {
System.out.println(" [ROLLBACK] Revirtiendo al entorno anterior...");
switchTraffic();
}
void status() {
System.out.printf(" LB → active=%-5s (v=%s) standby=%-5s (v=%s)%n",
active.getName(), active.getVersion(),
standby.getName(), standby.getVersion());
}
Environment getActive() { return active; }
Environment getStandby() { return standby; }
}
public static void main(String[] args) {
System.out.println("═".repeat(60));
System.out.println(" BLUE-GREEN DEPLOYMENT — simulación");
System.out.println("═".repeat(60));
Environment blue = new Environment("blue", "v1.0", true); // initially active
Environment green = new Environment("green", "v1.0", false); // standby
LoadBalancer lb = new LoadBalancer(blue, green);
System.out.println("\n[Estado inicial]");
System.out.println("─".repeat(60));
lb.status();
// ── Deploy v2 a green (inactivo), luego switch ────────────
System.out.println("\n[Deploy v2 al entorno standby (green)]");
System.out.println("─".repeat(60));
lb.getStandby().deploy("v2.0");
boolean passed = lb.getStandby().smokeTest();
if (passed) {
System.out.println("\n[Switch de tráfico]");
System.out.println("─".repeat(60));
lb.switchTraffic();
} else {
System.out.println(" Smoke test falló — sin switch de tráfico.");
}
lb.status();
// ── Simular rollback: problema detectado en v2 ────────────
System.out.println("\n[Problema detectado en v2 → rollback]");
System.out.println("─".repeat(60));
lb.rollback();
lb.status();
System.out.println("\n── Conclusión ──");
System.out.println(" Blue-green: zero-downtime deployment.");
System.out.println(" El rollback es un switch atómico al entorno anterior — sin re-deploy.");
}
}