Skip to content

Commit

Permalink
feat: eureka등록
Browse files Browse the repository at this point in the history
  • Loading branch information
Zero-1016 committed Feb 27, 2025
1 parent 91bacea commit 946b419
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 3 deletions.
107 changes: 107 additions & 0 deletions src/backend/signaling-node-server/src/libs/eureka.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { EUREKA_DISABLED } from "./constants";

export class EurekaClient {
config;
heartbeatInterval;
baseUrl;

constructor(config) {
this.config = config;
this.baseUrl = `http://${this.config.eureka.host}:${this.config.eureka.port}${this.config.eureka.servicePath}`;
console.log(`baseUrl: ${this.baseUrl}`);
}

async register() {
try {
if (EUREKA_DISABLED) {
console.log("Eureka 등록 비활성화");
return;
}

const response = await fetch(
`${this.baseUrl}/${this.config.instance.app}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
instance: this.config.instance,
}),
}
);

if (!response.ok) {
const errorText = await response.text();
throw new Error(
`HTTP error! status: ${response.status} - ${errorText}`
);
}

console.log("서비스가 Eureka에 등록되었습니다");
this.startHeartbeat();
} catch (error) {
console.error("Eureka 등록 실패:", error);
throw error;
}
}

startHeartbeat() {
this.heartbeatInterval = setInterval(async () => {
try {
const response = await fetch(
`${this.baseUrl}/${this.config.instance.app}/${this.config.instance.hostName}`,
{
method: "PUT",
}
);

if (!response.ok) {
throw new Error(`하트비트 실패! status: ${response.status}`);
}
} catch (error) {
console.error("하트비트 전송 실패:", error);
}
}, 30000); // 30초마다 하트비트 전송
}

async deregister() {
try {
if (this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
}

const response = await fetch(
`${this.baseUrl}/${this.config.instance.app}/${this.config.instance.hostName}`,
{
method: "DELETE",
}
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

console.log("서비스가 Eureka에서 제거되었습니다");
} catch (error) {
console.error("Eureka 제거 실패:", error);
throw error;
}
}

async getInstances(appName) {
try {
const response = await fetch(`${this.baseUrl}/${appName}`);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
return data.application?.instance || [];
} catch (error) {
console.error(`${appName} 인스턴스 조회 실패:`, error);
return [];
}
}
}
42 changes: 39 additions & 3 deletions src/backend/signaling-node-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ const wsServer = new Server(httpServer, {
},
});

const eurekaConfig = {
instance: {
app: "SIGNALING-NODE-SERVER",
hostName: "signaling—node-server",
ipAddr: "signaling-node-server",
status: "UP",
port: {
$: 9090,
"@enabled": "true",
},
vipAddress: "signaling-node-server",
statusPageUrl: "http://signaling-node-server:9090/",
dataCenterInfo: {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
name: "MyOwn",
},
},
eureka: {
host: "discovery-server",
port: 8761,
servicePath: "/eureka/apps",
},
};

const eurekaClient = new EurekaClient(eurekaConfig);

wsServer.on("connection", (socket) => {
console.log("✅ A client connected:", socket.id);

Expand Down Expand Up @@ -65,6 +91,16 @@ wsServer.on("connection", (socket) => {
});
});

httpServer.listen(PORT, () => {
console.log(`✅ Server is running on http://localhost:${PORT}`);
});
const start = async () => {
try {
await eurekaClient.register();
httpServer.listen(PORT, () => {
console.log(`✅ Server is running on http://localhost:${PORT}`);
});
} catch (error) {
eurekaClient.deregister();
process.exit(1);
}
};

start();

0 comments on commit 946b419

Please sign in to comment.