Nodejs注册到Eureka注册中心

正文

初始化

1
npm init

注册中心注册

Eureka注册中心默认是30s检测一下服务是否可用。

https://github.com/arthas001/eureka-node-client

安装eureka-node-client

1
npm install eureka-node-client --save

注册服务

register.js

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
let Eureka = require('eureka-node-client');
let hostname = "127.0.0.1";
let port = 3000;

let eureka_address = process.env.EUREKA_ADDRESS || '110.110.110.110';
let eureka_port = process.env.EUREKA_PORT || '8302';
let eureka_username = process.env.EUREKA_USERNAME || 'admin';
let eureka_password = process.env.EUREKA_PWD || 'admin';
const eureka_client = new Eureka({
eureka: {
host: `${eureka_username}:${eureka_password}@${eureka_address}`,
port: eureka_port,
servicePath: "/eureka/apps/"
},
instance: {
app: "sliverecord",
port: {'$': port, '@enabled': 'true'},
homePageUrl: `http://${hostname}:${port}/`,
healthCheckUrl: `http://${hostname}:${port}/health`,
statusPageUrl: `http://${hostname}:${port}/health`,
metadata: {
zone: 'primary',
env: process.env.NODE_ENV,
version: "1.0"
}
}
});

/**
* 设置日志级别
*/
eureka_client.logger.level('debug');

//******************** 测试监听 ********************//
let updatedListener = function (apps) {
console.log("更新:" + JSON.stringify(apps));
}
eureka_client.onUpdated(updatedListener);
eureka_client.start(function (error) {
console.log(error || '启动成功!');
});

其中

  • eureka 这是注册中心的地址 如果注册中心不需要账号密码直接写上也不影响。
  • instance 这是服务的地址

注册地址简写,关闭时注销注册

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
let Eureka = require('eureka-node-client');
let hostname = "127.0.0.1";
let port = 3000;

const eureka_client = new Eureka({
eureka: {
"serviceUrls": {
"default": [
"http://admin:admin@49.4.26.249:8302/eureka/apps/",
]
}
},
instance: {
app: "sliverecord",
port: {'$': port, '@enabled': 'true'},
homePageUrl: `http://${hostname}:${port}/`,
healthCheckUrl: `http://${hostname}:${port}/health`,
statusPageUrl: `http://${hostname}:${port}/health`,
}
});

/**
* 设置日志级别
*/
eureka_client.logger.level('debug');

//******************** 测试监听 ********************//
let updatedListener = function (apps) {
console.log("更新:" + JSON.stringify(apps));
}
eureka_client.onUpdated(updatedListener);
eureka_client.start(function (error) {
console.log(error || '启动成功!');
});

process.on('SIGINT', function() {
eureka_client.stop();
setTimeout(()=>{process.exit();},300)
});

注意

程序退出要延迟执行,否则取消注册不成功。

服务

安装express

1
npm install express --save

使用express挂起服务

1
2
3
4
5
6
7
8
9
10
11
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.json({
status: 'UP'
});
});
app.get('/', (req, res, next) => {
res.json({code: 0, msg: "success!"});
});
app.listen(3000);

测试Eureka服务

启动服务的时候启动注册服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const express = require('express');
const app = express();
app.get('/health', (req, res) => {
res.json({
status: 'UP'
});
});
app.get('/', (req, res, next) => {
res.json({code: 0, msg: "success!"});
});
app.listen(3000);

const { spawn } = require('child_process');
const grep = spawn('node', ['register.js']);

grep.on('close', (code, signal) => {
console.log(`子进程终止`);
process.exit();
});

在收到SIGINT信号后,会先结束子进程,所以我们可以监听子进程的关闭事件再关闭主进程

1
2
3
4
grep.on('close', (code, signal) => {
console.log(`子进程终止`);
process.exit();
});