Skip to content

Commit

Permalink
使用vuepress
Browse files Browse the repository at this point in the history
  • Loading branch information
yunnysunny committed Jan 20, 2024
1 parent 30d8ad1 commit 416b730
Show file tree
Hide file tree
Showing 16 changed files with 9,241 additions and 3 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Pages

on:
push:
branches:
- master # default branch
env:
CI: github
jobs:
pages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Restore file update time
run: git ls-files -z | while read -d '' path; do touch -d $(git log -1 --format="@%ct" "$path") "$path"; done
- name: Use Node.js 16.x
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: yarn install
- name: Clean
run: rm -rf output
- name: Build
run: npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./output
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2

with:
fetch-depth: 0
- name: Set up Docker Buildx
id: buildx
# Use the action from the master, as we've seen some inconsistencies with @v1
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
.sass-cache
/config/kitabu.yml
/_book
/text/.obsidian
node_modules
*.bkp
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "nodebook",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"docs:dev": "vuepress dev text",
"dev": "npm run docs:dev",
"docs:build": "vuepress build text",
"build": "npm run docs:build",
"preview": "cd output && anywhere 8888"
},
"repository": "[email protected]:yunnysunny/nodebook.git",
"author": "yunnysunny <[email protected]>",
"license": "MIT",
"devDependencies": {
"markdown-it-disable-url-encode": "^1.0.1",
"vuepress": "^1.9.10"
}
}
33 changes: 33 additions & 0 deletions text/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const toc = require('./toc')
const base = process.env.CI ? '/nodebook/' : '/'
module.exports = {
 title: 'nodebook',
 description: 'Node 基础教程',
base,
dest: 'output',
markdown: {
lineNumbers: true,
extendMarkdown: md => {
md.use(require('markdown-it-disable-url-encode'));
}
},
 themeConfig: {
sidebar: [
{
title: '基础教程', // 必要的
// path: '/', // 可选的, 标题的跳转链接,应为绝对路径且必须存在
collapsable: false, // 可选的, 默认值是 true,
sidebarDepth: 3, // 可选的, 默认值是 1
children: toc
}
],
sidebarDepth: 3,
   logo: 'https://excalidraw.com/apple-touch-icon.png',
nav: [
    { text: '首页', link: '/' },
    { text: 'github', link: 'https://github.com/yunnysunny/nodeboo' },
    { text: '博客', link: 'https://blog.whyun.com' },
  ]
}

}
24 changes: 24 additions & 0 deletions text/.vuepress/enhanceApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default ({
Vue, // VuePress 正在使用的 Vue 构造函数
options, // 附加到根实例的一些选项
router, // 当前应用的路由实例
siteData, // 站点元数据
isServer
}) => {
if (isServer) {
return
}
console.log(siteData)
console.log('test==',location.href)
const hash = location.hash
const pages = siteData.pages
if (hash) {
for (let i = 0, len = pages.length; i < len; i++) {
const path = pages[i].regularPath
if ('#' + path === hash) {
location.href = siteData.base + path
break
}
}
}
}
13 changes: 13 additions & 0 deletions text/.vuepress/toc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require('fs')
const path = require('path')
const toc = []
const mks = fs.readdirSync(path.join(__dirname, '../')).forEach((file) => {
if (file === 'README.md' || file === 'SUMMARY.md') {
return
}
if (file.endsWith('.md')) {
toc.push(file.slice(0, -3))
}
})
console.log(toc)
module.exports = toc
2 changes: 1 addition & 1 deletion text/00_preface.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Node.js 是 JavaScript 后端开发语言。从诞生之初就备受关注,到如今技术已经日臻成熟稳定。正是 Node 的兴起,还带动了前端 JS 的热度,react.js vue.js 这些前端 JS 也火借东风,烧的很旺,甚至连 “全栈” 这个词也一度登上了招聘启事的热搜关键字。本书的写作风格更偏向于底层原理和最佳实践,不会展开评述市面上的所有 Node 编程框架,力求通过一些常用的技术栈来将整个 Node 体系结构串联起来。

**声明**
nodejs台湾协作电子书(源码参见[github地址](https://github.com/nodejs-tw/nodejs-wiki-book) ),[台湾nodejs社区](http://nodejs.tw)发起的一套nodejs入门教程,本人初学nodejs之时,受其启发颇深。现在台湾社区的兄弟们对其已经停止维护,想想这么优秀的教程就这样白白流逝掉,是一件让人惋惜的事情的事情,于是乎我利用业余时间将其复活。
nodejs台湾协作电子书(源码参见[github地址](https://github.com/nodejs-tw/nodejs-wiki-book) ),是台湾 nodejs 社区发起的一套nodejs入门教程,本人初学nodejs之时,受其启发颇深。现在台湾社区的兄弟们对其已经停止维护,想想这么优秀的教程就这样白白流逝掉,是一件让人惋惜的事情的事情,于是乎我利用业余时间将其复活。

但是在整理的过程中发现了若干问题,首先繁体中文和简体中文的语言使用上还是有差别,之前耳熟能详的词语到了繁体中文中就变了个叫法,搞的自己差点摸不着头脑;其次,当时书籍写作的时候 Node 的版本还是0.6,如今时过境迁 Node 连10.0都出来了,好多知识点也发生了变化;最后,我也对原书的有些章节安排不是很满意。正是以上三点原因促成了我重写此书的决定。

Expand Down
2 changes: 1 addition & 1 deletion text/06_node_express_basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ express处理网络请求,是以一种被称之为 `middlewave(也翻译为中
上面讲了 express-generator 默认添加的若干 middleware, 不过如果是做网站项目的话,还缺少对于session的支持,这个要在后面单独讲。接下来是很重要的路由映射部分,因为项目中的url映射都是
在这里配置的。我们这里只看 `routes/index.js` 文件:

```javascripts
```javascript
var express = require('express');
var router = express.Router();

Expand Down
59 changes: 59 additions & 0 deletions text/a7_easy_monitor_setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## A7 easy-monitor 环境搭建

easy-monitor 是一个 alinode 的平替开源项目。

### A 7.1 准备环境

easy-monitor 需要 MySQL 和 redis 两个外部服务来存储数据和缓存。所以在开始配置之前起码需要准备一台 MySQL 和 redis。
为了方便使用,笔者将用到服务制作成了 docker 镜像,所以这里还需要用到 docker 运行环境。

### A 7.2 拓扑结构

easy-monitor 的组件比较复杂,所以需要先讲清楚其拓扑结构才能将其正确的部署。

![](images/easy_monitor_data_flow.drawio.png)

> 这里面 xtransit-server 是一个 websocket 服务器,其他两个服务是 http 服务器。xtransit-server 用来和 xtransit 进程建立长连接,xprofiler-console 服务用来提供管理后台,xtransit-mannager 用来处理性能日志数据。
> 虽然 xtransit 部分的功能,可以合并到应用服务代码中,但是这样有可能会干扰应用服务程序本身,所以这里给出拓扑图的时候,将其和应用服务进程分离开。
整个架构图中有两条数据流线,一条是日志收集数据流,一条是指令控制数据流。前者是将应用服务中采集到的性能指标日志发送到服务器端。后者是通过 console 控制台中发送生成 CPU Profile 或者堆快照的指令到应用程序中,同时还可以通过发送指令来命令 xtransit 进程将收集到日志文件上传到服务器端。

考虑到不同公司的网络拓扑结构会有不同,比如说有的公司内部所有服务之间网络是互通的,可以通过 IP 地址直接访问;但是有的公司会将公共服务单独隔离到一个子网中,外部必须通过网络边缘的反向代理服务器来访问。

对于需要配置反向代理服务器的情况,我们一般会选择使用 nginx,这种情况下,xtransit 进程会通过 nginx 来访问 xtransit-server 和 xprofiler-console 。下面只画出三者之间的拓扑结构:
![](images/easy_monitor_with_nginx_data_flow.drawio.png)
为了实现上图的拓扑结构,需要在 nginx 中配置不同的域名来实现反代到不同服务的目的。
### A 7.3 运行服务器端
#### A 7.3.1 数据库初始化

`xprofiler_console` 使用 [xprofiler-console/db/init.sql](https://github.com/X-Profiler/xprofiler-console/blob/master/db/init.sql) 进行初始化,库 `xprofiler_logs` 使用 [xtransit-manager/db/init.sql](https://github.com/X-Profiler/xtransit-manager/blob/master/db/init.sql) 以及 [xtransit-manager/db/date.sql](https://github.com/X-Profiler/xtransit-manager/blob/master/db/date.sql) 进行初始化。
> 使用 dbear 等工具的时候,新建完连接后,默认只能执行单条 SQL 语句,将上面的 SQL 代码全选复制到其控制台中执行时,会报语法错误。需要设置驱动属性的 `allowMultiQueries` 参数为 true :![](images/allow_multi_sql.png)

这里需要注意的是,easy-monitor 后端服务使用的 MySQL 库是 [egg-mysql](https://github.com/eggjs/egg-mysql),其最终是对 [mysql](https://github.com/mysqljs/mysql)的封装,但是这个库有一个缺陷,MySQL 8 默认使用的 caching_sha2_password 的鉴权方式,但是这个库不支持这种鉴权模式。所以在如果你使用的数据库是 MySQL 8 的话(使用 docker 创建的 MySQL 容器,默认版本就是 8.x 的版本),在不修改默认配置的情况下,得用超级管理员新建一个用户用来提供对 `xprofiler_console``xprofiler_logs` 两个库的访问:
```sql
CREATE USER 'easy'@'%' IDENTIFIED WITH mysql_native_password BY '********';
grant all privileges on xprofiler_console.* to 'easy'@'%';
grant all privileges on xprofiler_logs.* to 'easy'@'%';
flush privileges;
```
#### 7.3.2 启动服务器端
为了简化部署,笔者做了一个集成 xtransit-server xprofiler-console xtransit-mannager 三个服务的 docker 容器,是基于官方的 [all-in-one](https://github.com/X-Profiler/all-in-one) 项目优化而来。之所以重新做一个镜像出来,是由于官方镜像将配置信息采用磁盘文件映射的方式来提供,不是很贴合传统运维使用习惯,所以这里提供了一版全部通过环境变量来设置的镜像。
> 项目地址为 [whyun-docker/node (github.com)](https://github.com/whyun-docker/node)
由于牵扯到的环境变量比较多,所以这里直接给出一个 .env 文件:
```env
CONSOLE_MYSQL_HOST=mysql 访问 ip 或者域名
CONSOLE_MYSQL_PORT=mysql 端口号
CONSOLE_MYSQL_USER=mysql 用户名
CONSOLE_MYSQL_PASSWORD=mysql 密码
LOGS_MYSQL_HOST=mysql 访问 ip 或者域名
LOGS_MYSQL_PORT=mysql 端口号
LOGS_MYSQL_USER=mysql 用户名
LOGS_MYSQL_PASSWORD=mysql 密码
REDIS_SERVER=reidsIp1:redisPort1[,redisIp2:redisPort2]
REDIS_PASSWORD=redis 密码
CONSOLE_BASE_URL=console 服务的访问地址,例如 http://xxx-console.domian.com
```
然后我们可以通过 `sudo docker run --env-file ./.env -p 8443:8443 -p 8543:8543 -p 9190:9190 --name=easy-monitor -d yunnysunny/easy-monitor` 来启动服务。
Binary file added text/images/allow_multi_sql.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 416b730

Please sign in to comment.