# egg-test **Repository Path**: petter_pan/egg-test ## Basic Information - **Project Name**: egg-test - **Description**: 包含egg演示中的controller,service的demo - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-09-15 - **Last Updated**: 2022-10-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # egg的练习项目 查看所有测试用例 yarn run test-local 测试ES增删查改例子 npx egg-bin test test/app/controller/testES.test.js 路由说明 简单的接口,没连接数据的 /user 连接mongo数据的 /test 连接es的 /testES 静态文件访问的例子 / ## QuickStart see [egg docs][egg] for more detail. ### Development ```bash $ npm i $ npm run dev $ open http://localhost:7001/ # 测试并查看结果 $ yarn run test-local ``` ### Deploy ```bash $ npm start $ npm stop ``` ### npm scripts - Use `npm run lint` to check code style. - Use `npm test` to run unit test. - Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail. [egg]: https://eggjs.org ### FINISH ## 构建项目关键点 egg-swagger-doc ```bash npm install --save egg-swagger-doc ``` 配置 开启服务,然后访问http://127.0.0.1:7001/swagger-ui.html TODO:编写注释。但不知为什么错了 > @Response 200 body 正确响应 ## TODO - 错误拦截 ## docker的dockerfile构建 ### es @eggplugin/es是三年前的插件,依赖的es版本已经不存在,会出各种问题,es^8.x版本放弃了type,search方法传的query没有body,所以还是自己写一个插件。 ```bash npm i @eggplugin/es --save ``` ### 自己写一个egg-es-client插件 问题:依赖的@elastic/elasticsearch需要在最外层安装,使用的8.4版本 ### docker构建 由于容器中的es,mongo都要配置,如果启动容器再配置很繁琐,用dockerfile配置的话比较简单。 #### ES容器 配置了es的dockerfile。 参照官网构建镜像运行容器 ```bash docker build -t es -f es.Dockerfile . # docker run -p 9200:9200 -d --name es es:8.4.1 docker run -d --name es --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:8.3.1 ``` 拷贝配置文件修改再拷贝回容器 ```bash docker cp ./elasticsearch1.yml es:/usr/share/elasticsearch/config/elasticsearch.yml docker cp es:/usr/share/elasticsearch/config/elasticsearch.yml ./elasticsearch1.yml # 配置密码,用于项目连接 docker exec -it es bash /bin$ elasticsearch-reset-password -u elastic -i ``` #### mongo容器 ```bash docker build -f mongo.Dockerfile -t mongo . docker run -d --name mongo -p 27017:27017 --network somenetwork mongo ``` #### 项目容器 构建项目镜像 ```bash docker build -f Dockerfile -t egg . # docker run -d --name egg_test -p 8080:7001 egg_test # docker run -d --name egg_test -P egg_test docker run -d --name egg -P --network somenetwork egg ``` 项目内需要连接其他容器的网络,所以设置network,这样容器间才能通信。 ```bash docker network create somenetwork docker network connect somenetwork egg docker network connect somenetwork mongo docker network connect somenetwork es docker restart egg ``` 注意踩坑 Dockerfile中这样写是错的。 CMD [npm","run","start"] 当各种容器启动了却发现容器间无法通信。 所以加了network实现容器间通信,也可以通过宿主机连接 连接mongo错误导致项目启动不了 [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 所以配置改为这样 ```js config.mongoose = { url: 'mongodb://localhost:27017/test', options: { // user: 'admin', // 数据库用户名 // pass: '123456', // 数据库密码 // dbName: 'test' // 数据库名 useUnifiedTopology: true } } ``` 连接es失败,但有返回。 用window上的项目连接能,但容器中连接就失败,mongo也能连,就es不能。 应该网络连不上,网络不通。 容器间通信要了解下,可以用容器名连接,也可以用宿主机ip连接 ```bash > interface@1.0.0 start > egg-scripts start --daemon --title=egg-server-interface [egg-scripts] Starting egg application at E:\me\workplace\Nodejs\egg\interface_test [egg-scripts] Run node --no-deprecation --trace-warnings E:\me\workplace\Nodejs\egg\interface_test\node_modules\egg-scripts\lib\start-cluster {"title":"egg-server-interface","baseDir":"E:\\me\\workplace\\Nodejs\\egg\\interface_test","framework":"E:\\me\\workplace\\Nodejs\\egg\\interface_test\\node_modules\\egg"} --title=egg-server-interface [egg-scripts] Save log file to C:\Users\pgd\logs [egg-scripts] Wait Start: 1... [egg-scripts] Wait Start: 2... [egg-scripts] Wait Start: 3... [egg-scripts] Wait Start: 4... [egg-scripts] tail -n 100 C:\Users\pgd\logs\master-stderr.log [egg-scripts] Got error when startup: [egg-scripts] at SniffingTransport.request (E:\me\workplace\Nodejs\egg\interface_test\node_modules\@elastic\transport\lib\Transport.js:525:31) [egg-scripts] at processTicksAndRejections (node:internal/process/task_queues:96:5) [egg-scripts] at async Client.PingApi [as ping] (E:\me\workplace\Nodejs\egg\interface_test\node_modules\@elastic\elasticsearch\lib\api\api\ping.js:37:12) [egg-scripts] at async E:\me\workplace\Nodejs\egg\interface_test\lib\plugin\egg-es-client\lib\es.js:21:5 [egg-scripts] name: "ConnectionError" [egg-scripts] meta: {"statusCode":0,"headers":{},"meta":{"context":null,"request":{"params":{"method":"HEAD","path":"/","querystring":"","headers":{"user-agent":"elastic-transport-js/8.2.0 (win32 10.0.19044-x64; Node.js v16.17.0)","x-elastic-client-meta":"es=8.4.0,js=16.17.0,t=8.2.0,hc=16.17.0","accept":"application/vnd.elasticsearch+json; compatible-with=8,text/plain"}},"options":{},"id":1},"name":"elasticsearch-js","connection":{"url":"http://127.0.0.1:9200/","id":"http://127.0.0.1:9200/","headers":{},"status":"alive"},"attempts":3,"aborted":false},"warnings":null} [egg-scripts] pid: 9708 [egg-scripts] hostname: LAPTOP-HV0B8EC9 [egg-scripts] [egg-scripts] 2022-10-19 00:10:57,763 ERROR 9708 nodejs.ConnectionError: connect ECONNREFUSED 127.0.0.1:9200 [egg-scripts] at SniffingTransport.request (E:\me\workplace\Nodejs\egg\interface_test\node_modules\@elastic\transport\lib\Transport.js:525:31) [egg-scripts] at processTicksAndRejections (node:internal/process/task_queues:96:5) [egg-scripts] at async Client.PingApi [as ping] (E:\me\workplace\Nodejs\egg\interface_test\node_modules\@elastic\elasticsearch\lib\api\api\ping.js:37:12) [egg-scripts] at async E:\me\workplace\Nodejs\egg\interface_test\lib\plugin\egg-es-client\lib\es.js:21:5 [egg-scripts] name: "ConnectionError" [egg-scripts] meta: {"statusCode":0,"headers":{},"meta":{"context":null,"request":{"params":{"method":"HEAD","path":"/","querystring":"","headers":{"user-agent":"elastic-transport-js/8.2.0 (win32 10.0.19044-x64; Node.js v16.17.0)","x-elastic-client-meta":"es=8.4.0,js=16.17.0,t=8.2.0,hc=16.17.0","accept":"application/vnd.elasticsearch+json; compatible-with=8,text/plain"}},"options":{},"id":1},"name":"elasticsearch-js","connection":{"url":"http://127.0.0.1:9200/","id":"http://127.0.0.1:9200/","headers":{},"status":"alive"},"attempts":3,"aborted":false},"warnings":null} [egg-scripts] pid: 9708 [egg-scripts] hostname: LAPTOP-HV0B8EC9 ``` ## docker的docker-compose.yml构建 ### es 试想过的构建es步骤: 1 拉取镜像 2 配置证书 3 修改密码 4 将密码复制到egg config 参考官方文档 https://www.elastic.co/guide/en/elasticsearch/reference/8.4/docker.html#docker-file 修改了.env中密码 第一次报kibana不健康,所以把kibana的service去删掉。 第二次打印日志 发现 bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, in 参考了https://blog.csdn.net/qq_32331997/article/details/80019167 做了修改: 解决办法: 1、切换到root用户修改配置sysctl.conf vi /etc/sysctl.conf 添加下面配置(i编辑模式exc退出编辑wq保存并退出): vm.max_map_count=655360 并执行命令: sysctl -p 然后,重新启动elasticsearch,即可启动成功。 改eggconfig从.env读取密码 ```bash nodejs.ConnectionError: unable to verify the first certificate ``` @elastic/search客户端无法访问的情况,访问elasticsearch官网发现要配置证书路径,不然无法访问https接口。 将证书拷贝出来,配置路径。但是docker cp 似乎不支持容器名中带-的命令,所以修改docker-compose.yml文件配置容器名。 https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html 想想干脆用文件映射将容器内证书映射出来,config直接用路径就行。(试过不行,看不到证书文件) 拷贝-重启egg容器 ``` docker cp es01_c:/usr/share/elasticsearch/config/certs/es01/es01.crt ./config/certs/es01.crt ``` ```bash ERROR 50 [-/127.0.0.1/-/0ms GET /] nodejs.ConnectionError: Client network socket disconnected before secure TLS connection was established ``` 重启容器,打印日志,没问题,发现端口监听。 但是容器却退出了。 这篇博客有帮助: https://www.jianshu.com/p/8cd7ba8bc3d5 去掉 start命令中的--daemon ## 问题 ``` 这样连接可能不对 url: 'mongodb://mongo:27017/test' ```