线上部署面试蛙

线上部署面试蛙

1.核心部署

一 本地准备

  1. 修改代码:去掉es、sentinel、hotkey第三方代码,保证核心代码能够线上运行
  2. 修改 application-prod.yml ,主要配置线上mysql、redis信息
  3. maven打包
  4. 上传jar包到宝塔

二 宝塔部署-后端

  1. 添加项目启动命令:(等待半分钟后刷新页面查看是否显示端口)
 --server.port=8102 --spring.profiles.active=prod
  1. nginx 转发:
  • 在php项目新建,域名是公网ip,目录是mianshiwa
  • php版本必须是80
  • 其他默认
  1. php项目创建后,在其nginx添加配置:(用于访问api路径打头)
    location /api {
      proxy_pass  http://127.0.0.1:8102;
      proxy_set_header Host $proxy_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_buffering off;
      proxy_set_header Connection "";
    }
  1. 将nginx默认静态配置注释:
  # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    # {
    #     expires      30d;
    #     error_log /dev/null;
    #     access_log /dev/null;
    # }

    # location ~ .*\.(js|css)?$
    # {
    #     expires      12h;
    #     error_log /dev/null;
    #     access_log /dev/null;
    # }
  1. 测试nginx转发是否成功,如果成功,则能访问后端swagger界面:

三 宝塔部署-前端

  1. 修改前端代码:request.js
// 创建 Axios 示例
// 区分开发和生产环境
const DEV_BASE_URL = "http://localhost:8102";
const PROD_BASE_URL = "http://线上ip";
const myAxios = axios.create({
    baseURL: PROD_BASE_URL,
    timeout: 10000,
    withCredentials: true,// cookie 自动携带
});
  1. 修改next.config.mjs,next单机部署
/** @type {import('next').NextConfig} */
const nextConfig = {
    output: "standalone",
};

export default nextConfig;

  1. build 前端项目,生成

注意:如果有报错,可以直接一键忽略:在next.config.mjs添加以下内容

/** @type {import('next').NextConfig} */
const nextConfig = {
    output: "standalone",
    typescript: {
        // !! WARN !!
        // Dangerously allow production builds to successfully complete even if
        // your project has type errors.
        // !! WARN !!
        ignoreBuildErrors: true,
    },
};

export default nextConfig;
  1. build成功效果:(有个小bug,○代表静态内容,如果管理员修改题库图片,并不会修改首页的图片显示)
Route (app)                                       Size     First Load JS
┌ ○ /                                             6.22 kB         277 kB
├ ○ /_not-found                                   883 B          88.6 kB
├ ○ /admin/bank                                   3.13 kB         681 kB
├ ○ /admin/question                               4.13 kB         874 kB
├ ○ /admin/user                                   2.57 kB         681 kB
├ ƒ /bank/[questionBankId]                        664 B           279 kB
├ ƒ /bank/[questionBankId]/question/[questionId]  4.47 kB         456 kB
├ ○ /banks                                        2.42 kB         270 kB
├ ƒ /question/[questionId]                        1.44 kB         437 kB
├ ƒ /questions                                    3.69 kB         674 kB
├ ○ /user/center                                  338 kB          588 kB
├ ○ /user/login                                   1.78 kB         513 kB
└ ○ /user/register                                1.52 kB         504 kB
+ First Load JS shared by all                     87.7 kB
  ├ chunks/7023-357c18e1abddc78a.js               31.7 kB
  ├ chunks/fd9d1056-6091cd8ebb63e356.js           53.7 kB
  └ other shared chunks (total)                   2.36 kB


○  (Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand
  1. 修第一个bug:
// 强制主页动态渲染
export const dynamic = 'force-dynamic';
  1. 修改打包后的文件位置
  • 在 .next 目录下会生成 standalone 目录,该目录就是可以独立部署的前端包。但是必须按照如下模式组织目录,一定不能有错!
    1. 将项目根目录下的 public 目录移动到 .next/standalone 内
    2. 将 .next/static 目录移动到 .next/standalone/.next 内
  1. 将standalone里的文件都压缩并复制到宝塔
  2. 上传到服务器后,添加 Node 项目。注意修改启动选项(start 或者自定义命令 node server.js)和项目端口(3000)
  3. 修改 Nginx 配置,访问前端资源时,反向代理到 Node.js 服务:
location / {
  proxy_pass  http://127.0.0.1:3000;
  proxy_set_header Host $proxy_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_buffering off;
  proxy_set_header Connection "";
}
  1. 测试:http://线上ip

至此,已经部署好一个基础的核心面试蛙项目,但是还未集成es分词检索、sentinel流量控制、hotkey热点检测

2.es部署

代码接口:题目搜索接口、ES 定时同步任务

修改代码

安装es

开放端口

测试es

后台运行:

nohup ./bin/elasticsearch > elasticsearch.log 2>&1 &

安装kibana

安装ik 分词器:

bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/7.17.24

重启es

测试ik

bug:打开题目详情页的任意一道题都需要登陆

4

3.sentinel部署

4.hotKey部署


posted @ 2024-11-06 23:09  gdxstart  阅读(92)  评论(0)    收藏  举报