-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart-services.sh
More file actions
executable file
Β·170 lines (136 loc) Β· 5.15 KB
/
start-services.sh
File metadata and controls
executable file
Β·170 lines (136 loc) Β· 5.15 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
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
#!/bin/bash
# Microservices Startup Script - Updated for Dynamic Ports
echo "π Starting Microservices with Dynamic Port Configuration..."
# Function to check if a fixed port is in use (only for discovery and gateway)
check_port() {
if lsof -Pi :$1 -sTCP:LISTEN -t >/dev/null ; then
echo "β οΈ Port $1 is already in use"
return 1
else
return 0
fi
}
# Function to start a service with fixed port
start_service_fixed_port() {
local service_name=$1
local port=$2
local directory=$3
echo "π¦ Starting $service_name on fixed port $port..."
if check_port $port; then
cd "$directory"
# Create pids directory if it doesn't exist
mkdir -p "$BASE_DIR/pids"
# Start service in background
nohup mvn spring-boot:run > "$BASE_DIR/logs/${service_name}.log" 2>&1 &
local pid=$!
echo $pid > "$BASE_DIR/pids/${service_name}.pid"
echo "β
$service_name started with PID $pid"
sleep 2
else
echo "β Cannot start $service_name - port $port is in use"
fi
}
# Function to start a service with dynamic port
start_service_dynamic_port() {
local service_name=$1
local directory=$2
echo "π¦ Starting $service_name with dynamic port (will be assigned by OS)..."
cd "$directory"
# Create pids directory if it doesn't exist
mkdir -p "$BASE_DIR/pids"
# Start service in background
nohup mvn spring-boot:run > "$BASE_DIR/logs/${service_name}.log" 2>&1 &
local pid=$!
echo $pid > "$BASE_DIR/pids/${service_name}.pid"
echo "β
$service_name started with PID $pid (port will be discovered via Eureka)"
sleep 2
}
# Create logs and pids directories
mkdir -p logs pids
# Base directory
BASE_DIR="/opt/projects/MicroservicesApiGateway"
echo "π Checking prerequisites..."
# Check if Java and Maven are available
if ! command -v java &> /dev/null; then
echo "β Java is not installed"
exit 1
fi
if ! command -v mvn &> /dev/null; then
echo "β Maven is not installed"
exit 1
fi
echo "β
Prerequisites check passed"
echo ""
echo "π― Starting services in dependency order..."
echo ""
# 1. Discovery Server (must start first - fixed port for service registration)
start_service_fixed_port "discovery-server" 8761 "$BASE_DIR/discovery-server"
sleep 10 # Give discovery server time to start
# 2. API Gateway (fixed port for external access)
start_service_fixed_port "api-gateway" 8080 "$BASE_DIR/api-gateway"
sleep 5
# 3. Auth Service (dynamic port - discovered via Eureka)
start_service_dynamic_port "auth-service" "$BASE_DIR/auth-service"
sleep 5
# 4. User Service (dynamic port - discovered via Eureka)
start_service_dynamic_port "user-service" "$BASE_DIR/user-service"
sleep 5
# 5. Product Service (dynamic port - discovered via Eureka)
start_service_dynamic_port "product-service" "$BASE_DIR/product-service"
sleep 5
echo ""
echo "β³ Waiting for all services to register with Eureka..."
sleep 20
echo ""
echo "π Checking service health via API Gateway..."
# Check fixed port services directly
gateway_health="http://localhost:8080/actuator/health"
if curl -s -f "$gateway_health" > /dev/null 2>&1; then
echo "β
API Gateway is healthy"
else
echo "β οΈ API Gateway may not be ready yet"
fi
discovery_health="http://localhost:8761/actuator/health"
if curl -s -f "$discovery_health" > /dev/null 2>&1; then
echo "β
Discovery Server is healthy"
else
echo "β οΈ Discovery Server may not be ready yet"
fi
# Check dynamic port services via API Gateway
services_via_gateway=(
"auth-service"
"user-service"
"product-service"
)
for service in "${services_via_gateway[@]}"; do
health_url="http://localhost:8080/$service/actuator/health"
if curl -s -f "$health_url" > /dev/null 2>&1; then
echo "β
$service is healthy (via Gateway)"
else
echo "β οΈ $service may not be ready yet (check Eureka registration)"
fi
done
echo ""
echo "π Microservices startup complete!"
echo ""
echo "π Service URLs:"
echo " Discovery Server: http://localhost:8761 (View registered services)"
echo " API Gateway: http://localhost:8080 (Entry point for all requests)"
echo " Auth Service: http://localhost:8080/auth-service (via Gateway)"
echo " User Service: http://localhost:8080/user-service (via Gateway)"
echo " Product Service: http://localhost:8080/product-service (via Gateway)"
echo ""
echo "π Monitoring URLs (if monitoring stack is running):"
echo " Prometheus: http://localhost:9090"
echo " Grafana: http://localhost:3000"
echo " Zipkin: http://localhost:9411"
echo ""
echo "π Service management:"
echo " View logs: tail -f logs/<service-name>.log"
echo " Stop service: kill \$(cat pids/<service-name>.pid)"
echo " Stop all: ./stop-services.sh"
echo " Service manager: ./service-manager.sh status all"
echo " Test APIs: ./test-apis.sh"
echo ""
echo "βΉοΈ Note: Services with dynamic ports are accessed through the API Gateway."
echo " Use the Discovery Server dashboard to see actual assigned ports."