跟着《架构探险》学轻量级微服务架构 (二)

版权声明:叶梅树 https://blog.csdn.net/yemeishu6033022/article/details/81229905
6302-a61b919b5a3aaf7d.jpg
架构探险

回顾 —— 微服务

微服务是一种分布式系统架构,它建议我们将业务划分为更加细粒度的服务,并使每个服务的责任单一且可独立部署,服务内部高内聚,隐含内部细节,服务之间低耦合,彼此相互隔离。此外,我们根据面向服务的业务领域来建模,对外提供统一的 API 接口。微服务的思想不只是停留在开发阶段,它贯穿于设计、开发、测试、部署、运维等软件生命周期阶段。
引用于《架构探险》

写代码

上一篇主要简单搭建了 Spring Boot 框架,写了一个简单的路由/hello,Spring Boot 的其它功能根据后续的学习,再不断完善,接下来我们开始下一个概念:

1. 微服务开发框架 —— Spring Boot 框架
2. 微服务网关 —— Node.js
3. 微服务注册与发现 —— ZooKeeper
4. 微服务封装 —— Docker
5. 微服务部署 —— Jenkins, GitLab

微服务网关 —— Node.js

微服务网关是微服务架构中的核心组件,它是客户端请求的门户,它是调用具体服务端的桥梁。
来自于《架构探险》

6302-40d22f1031b3b749.jpg

简单的说,微服务网关是一个服务器,也可以说是进入系统的唯一入口。这与面向对象设计模式中的 Facade 模式很像。微服务网关封装内部系统的架构,并且提供 API 给各个客户端。它还可能还具备授权、监控、负载均衡、缓存、请求分片和管理、静态响应处理等功能。

《架构探险》一书使用 Node.js 实现服务网关的重要特性之一:反向代理。

安装 Node.js 就不在这里描述了,如果是 Mac 系统,可以直接使用 brew (http://brew.sh/) 命令安装。

1. 安装 npm 国内镜像

npm install cnpm -g --registry=https://registry.npm.taobao.org
6302-a6562c854cf9f30d.jpg

2. 安装反向代理插件 Http Proxy 模块:

cnpm install http-proxy --save

3. 编写 app.js:

var http = require('http');

var httpProxy = require('http-proxy');

var PORT = 1234;

var proxy = httpProxy.createProxyServer();
proxy.on('error', function( err, req, res) {
    res.end(); // 输出空白响应数据
});

var app = http.createServer(function (req, res) {
    proxy.web(req, res, {
        target: 'http://localhost:8080'  // 目标地址
    });
});

app.listen(PORT, function() {
    console.log('server is running at %d', PORT);
});

执行 app.js 应用程序:

node app.js
6302-02b7553af1115021.jpg

我们修改上一篇写的 Spring Boot 应用,让访问 http://localhost:8080/ 可以直接访回「你好 叶梅树」。

6302-5d258628bd0a9a24.jpg

这样我们可以直接访问 http://localhost:1234/ 看看「反向代理」能不能起到作用,反向到: http://localhost:8080/,如下:

6302-4ffb2b34023e0445.jpg

这就表明了我们的反向代理起到作用了。

Node.js 集群环境

Node.js 采用了单线程模型,且拥有基于事件驱动的异步非阻塞 I/O 特性,可高效利用 CPU 资源,但并不能说明 Node.js 只能运行在单核 CPU 下。事实上,Node.js 原生已支持集群特性。如下代码所示:

var http = require('http');

var cluster = require('cluster');

var os = require('os');

var PORT = 1234;

var CUPS = os.cpus().length; // 获取 CPU 内核数

if (cluster.isMaster) {
    // 当前进程为主进程
    for (var i = 0; i < CUPS; i++) {
        cluster.fork();
    }
} else {
    // 当前进程为子进程
    var app = http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<h1>Hello</h1>');
        res.end();
    })

    app.listen(PORT, function () {
        console.log('server is running at %d', PORT);
    })
}

注:通过 OS 模块获取主机的 CPU 内核数,利用 Cluster 模块来判断当前进程是否为主线程或者是子线程,如果是主线程则创建子线程,然后利用子线程来运行相关的程序代码,这里 CPU 内核数越多,创建的子线程越多,支持的并发量也就越高。

Node.js 运行工具比较

1. supervisor。主要用于开发阶段,能够实时监控源文件的变化,自动重新加载,查看运行结果,提供开发效率。

2. forever (https://github.com/foreverjs/forever)。

# 启动
forever start ./bin/www  #最简单的启动方式
forever start -l forever.log ./bin/www  #指定forever日志输出文件,默认路径~/.forever
forever start -l forever.log -a ./bin/www  #需要注意,如果第一次启动带日志输出文件,以后启动都需要加上 -a 参数,forever默认不覆盖原文件
forever start -o out.log -e err.log ./bin/www  #指定node.js应用的控制台输出文件和错误信息输出文件
forever start -w ./bin/www  #监听当前目录下文件改动,如有改动,立刻重启应用,不推荐的做法!如有日志文件,日志文件是频繁更改的

# 重启
forever restart ./bin/www  #重启单个应用
forever restart [pid]  #根据pid重启单个应用
forever restartall  #重启所有应用

# 停止(和重启很类似)
forever stop ./bin/www  #停止单个应用
forever stop [pid]  #根据pid停止单个应用
forever stopall  #停止所有应用

# 查看forever守护的应用列表
forever list

3. pm2 (https://github.com/Unitech/pm2)。

npm install -g pm2  # 安装pm2
# Fork mode
pm2 start app.js --name my-api # Name process

# Cluster mode
pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -i max      # Same as above, but deprecated.

# Listing

pm2 list               # Display all processes status
pm2 jlist              # Print process list in raw JSON
pm2 prettylist         # Print process list in beautified JSON

pm2 describe 0         # Display all informations about a specific process

pm2 monit              # Monitor all processes

# Logs

pm2 logs [--raw]       # Display all processes logs in streaming
pm2 flush              # Empty all log file
pm2 reloadLogs         # Reload all logs

# Actions

pm2 stop all           # Stop all processes
pm2 restart all        # Restart all processes

pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)

pm2 stop 0             # Stop specific process id
pm2 restart 0          # Restart specific process id

pm2 delete 0           # Will remove process from pm2 list
pm2 delete all         # Will remove all processes from pm2 list

# Misc

pm2 reset <process>    # Reset meta data (restarted time...)
pm2 updatePM2          # Update in memory pm2
pm2 ping               # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my-app # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart
feature forever pm2
keep alive
coffeescript
Log aggregation
api
terminal monitoring
clustering
Json configuration

从这表可以看出,pm2 相比较 forever,功能更加强大一些。在现实生产环境下,我们用 pm2更多一些,因为都是工具,更多的就看你用的顺不顺手,然后再根据每个工具的优劣,合理使用。

总结

学习 Node.js 的东西太多太多了,这只是冰山一角。这里主要是利用 Node.js 的「反向代理」来搭建 「微服务网关」,学习「微服务网关」的基本概念。


明天接着学习,coding01 值得您关注

6302-631497fa92c6566b.jpeg
qrcode

也很感谢您能看到这了

6302-54dfe3ece0a9ba25.jpg
qrcode

猜你喜欢

转载自blog.csdn.net/yemeishu6033022/article/details/81229905