当前位置: 首页 > news >正文

简单搭建qiankun的主应用和子应用并且用Docker进行服务器部署

在node18环境下,用react18创建qiankun主应用和两个子应用,react路由用V6版本,都在/main路由下访问子应用,用Dockerfile部署到腾讯云CentOS7.6服务器的8000端口进行访问,且在部署过程中进行nginx配置以进行合理的路由访问

要在 Node.js 18 环境下,用 React 18 创建一个基于 qiankun 的主应用和两个子应用,并使用 React Router v6 进行路由管理,所有子应用都在 /main 路由下访问,并使用 Docker 和 Nginx 部署到腾讯云 CentOS 7.6 服务器的 8000 端口,可以按照以下步骤进行:

1. 创建主应用和子应用

主应用

  1. 创建主应用:
npx create-react-app main-app --template typescript
cd main-app
  1. 安装 qiankun 和 react-router-dom:
npm install qiankun react-router-dom@6
  1. 配置主应用:
    在 index.tsx 中配置 qiankun:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import { registerMicroApps, start } from 'qiankun';
import App from './App';registerMicroApps([{name: 'sub-app-1',entry: '//localhost:7100',container: '#subapp-viewport',activeRule: '/main/sub-app-1',},{name: 'sub-app-2',entry: '//localhost:7200',container: '#subapp-viewport',activeRule: '/main/sub-app-2',},
]);start();ReactDOM.render(<React.StrictMode><Router><Routes><Route path="/*" element={<App />} /></Routes></Router></React.StrictMode>,document.getElementById('root')
);
  1. 修改 App.tsx:
import React from 'react';
import { Link } from 'react-router-dom';const App = () => (<div><nav><ul><li><Link to="/main/sub-app-1">Sub App 1</Link></li><li><Link to="/main/sub-app-2">Sub App 2</Link></li></ul></nav><div id="subapp-viewport"></div></div>
);export default App;

子应用 1

  1. 创建子应用 1:
npx create-react-app sub-app-1 --template typescript
cd sub-app-1
  1. 安装 qiankun 和 react-router-dom:
npm install qiankun react-router-dom@6
  1. 配置子应用 1:
    在 index.tsx 中添加 qiankun 的生命周期函数:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import App from './App';if (window.__POWERED_BY_QIANKUN__) {// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-ignore__webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}function render(props) {const { container } = props;ReactDOM.render(<React.StrictMode><Router basename={window.__POWERED_BY_QIANKUN__ ? '/main/sub-app-1' : '/'}><Routes><Route path="/*" element={<App />} /></Routes></Router></React.StrictMode>,container ? container.querySelector('#root') : document.getElementById('root'));
}if (!window.__POWERED_BY_QIANKUN__) {render({});
}export async function bootstrap() {console.log('sub-app-1 bootstraped');
}export async function mount(props) {console.log('sub-app-1 mounted');render(props);
}export async function unmount(props) {console.log('sub-app-1 unmounted');const { container } = props;ReactDOM.unmountComponentAtNode(container ? container.querySelector('#root') : document.getElementById('root'));
}
  1. 配置 package.json:
{"name": "sub-app-1","version": "0.1.0","private": true,"homepage": "/main/sub-app-1","dependencies": {"react": "^18.0.0","react-dom": "^18.0.0","react-scripts": "5.0.0","qiankun": "^2.4.0","react-router-dom": "^6.0.0"},"scripts": {"start": "PORT=7100 react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject"}
}

子应用 2

子应用 2 的步骤和 子应用 1 基本一致,项目名不一样和Router的 basename 不一样即可;端口为 7200,或者自行定义,和主应用和子应用1端口不一样即可

2. 创建 Dockerfile

为每个应用创建 Dockerfile,并使用 Nginx 作为静态文件服务器。

主应用 Dockerfile

在 main-app 目录下创建 Dockerfile:

# Build stage
FROM node:18 AS build# Set the working directory
WORKDIR /app# Copy the package.json and package-lock.json files
COPY package*.json ./# Install dependencies
RUN npm install# Copy the rest of the application code
COPY . .# Build the application
RUN npm run build# Production stage
FROM nginx:alpine# Copy the built files from the build stage
COPY --from=build /app/build /usr/share/nginx/html# Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/nginx.conf# Expose port 80
EXPOSE 80# Start Nginx
CMD ["nginx", "-g", "daemon off;"]

子应用 1 Dockerfile

在 sub-app-1 目录下创建 Dockerfile:

# Build stage
FROM node:18 AS build# Set the working directory
WORKDIR /app# Copy the package.json and package-lock.json files
COPY package*.json ./# Install dependencies
RUN npm install# Copy the rest of the application code
COPY . .# Build the application
RUN npm run build# Production stage
FROM nginx:alpine# Copy the built files from the build stage
COPY --from=build /app/build /usr/share/nginx/html# Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/nginx.conf# Expose port 80
EXPOSE 80# Start Nginx
CMD ["nginx", "-g", "daemon off;"]

子应用 2 Dockerfile

在 sub-app-2 目录下创建 Dockerfile:

# Build stage
FROM node:18 AS build# Set the working directory
WORKDIR /app# Copy the package.json and package-lock.json files
COPY package*.json ./# Install dependencies
RUN npm install# Copy the rest of the application code
COPY . .# Build the application
RUN npm run build# Production stage
FROM nginx:alpine# Copy the built files from the build stage
COPY --from=build /app/build /usr/share/nginx/html# Copy the Nginx configuration file
COPY nginx.conf /etc/nginx/nginx.conf# Expose port 80
EXPOSE 80# Start Nginx
CMD ["nginx", "-g", "daemon off;"]

3. 创建 Nginx 配置文件

为每个应用创建一个 Nginx 配置文件 nginx.conf。

主应用 Nginx 配置文件

在 main-app 目录下创建 nginx.conf:

server {listen 80;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;try_files $uri $uri/ /index.html;}location /main/sub-app-1/ {proxy_pass http://localhost:7100/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}location /main/sub-app-2/ {proxy_pass http://localhost:7200/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}
}

子应用 1 和子应用 2 Nginx 配置文件

在 sub-app-1 和 sub-app-2 目录下分别创建 nginx.conf:

server {listen 80;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;try_files $uri $uri/ /index.html;}
}

4. 构建和运行 Docker 容器

在每个应用的目录下,运行以下命令构建 Docker 镜像:

docker build -t main-app .
docker build -t sub-app-1 .
docker build -t sub-app-2 .

然后运行 Docker 容器:

docker run -d -p 8000:80 main-app
docker run -d -p 7100:80 sub-app-1
docker run -d -p 7200:80 sub-app-2

5. 部署到腾讯云 CentOS 7.6 服务器

  1. 连接到腾讯云服务器:
    使用 SSH 连接到你的腾讯云 CentOS 7.6 服务器。
ssh your-username@your-server-ip
  1. 安装 Docker:
    如果你的服务器上还没有安装 Docker,可以使用以下命令安装:
sudo yum update -y
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
  1. 将 Docker 镜像推送到 Docker Hub:
    在本地机器上,将构建的 Docker 镜像推送到 Docker Hub:
docker tag main-app your-dockerhub-username/main-app
docker tag sub-app-1 your-dockerhub-username/sub-app-1
docker tag sub-app-2 your-dockerhub-username/sub-app-2docker push your-dockerhub-username/main-app
docker push your-dockerhub-username/sub-app-1
docker push your-dockerhub-username/sub-app-2
  1. 在服务器上拉取并运行 Docker 镜像:
    在服务器上,拉取并运行 Docker 镜像:
docker pull your-dockerhub-username/main-app
docker pull your-dockerhub-username/sub-app-1
docker pull your-dockerhub-username/sub-app-2docker run -d -p 8000:80 your-dockerhub-username/main-app
docker run -d -p 7100:80 your-dockerhub-username/sub-app-1
docker run -d -p 7200:80 your-dockerhub-username/sub-app-2

通过这些步骤,你可以在 Node.js 18 环境下,用 React 18 创建一个基于 qiankun 的主应用和两个子应用,并使用 Nginx 作为静态文件服务器,部署到腾讯云 CentOS 7.6 服务器的 8000 端口进行访问。这样可以确保各个应用的隔离性和独立性,同时通过 /main 路由访问子应用。

相关文章:

简单搭建qiankun的主应用和子应用并且用Docker进行服务器部署

在node18环境下&#xff0c;用react18创建qiankun主应用和两个子应用&#xff0c;react路由用V6版本&#xff0c;都在/main路由下访问子应用&#xff0c;用Dockerfile部署到腾讯云CentOS7.6服务器的8000端口进行访问&#xff0c;且在部署过程中进行nginx配置以进行合理的路由访…...

Python知识分享第十六天

“”" 故事7: 小明把煎饼果子技术传给徒弟的同时, 不想把独创配方传给他, 我们就要加私有. 问: 既然不想让子类用, 为什么要加私有? 答: 私有的目的不是不让子类用, 而是不让子类直接用, 而必须通过特定的 途径或者方式才能使用. 大白话: ATM机为啥要设计那么繁琐, 直接…...

管家婆财贸ERP BR045.大类存货库存数量明细表

最低适用版本&#xff1a; C系列 23.8 插件简要功能说明&#xff1a; 库存数量明细表支持按存货展示数据更多细节描述见下方详细文档 插件操作视频&#xff1a; 进销存类定制插件--大类存货库存数量明细表 插件详细功能文档&#xff1a; 应用中心增加菜单【大类存货库存数…...

Pytorch-GPU版本离线安装

最近在复现一项深度学习的工作&#xff0c;发现自己的pytorch是装的cpu版的(好像当时是直接加清华源&#xff0c;默认是cpu版本&#xff09;。从官网在线下载速度太慢&#xff0c;还时不时断开连接&#xff0c;我们可以配置conda的清华源去这个问题&#xff0c;但是考虑到是在用…...

k8s 1.28 二进制安装与部署

第一步 &#xff1a;配置Linux服务器 #借助梯子工具 192.168.196.100 1C8G kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubectl、haproxy、keepalived 192.168.196.101 1C8G kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubectl、…...

【C语言】扫雷游戏(一)

我们先设计一个简单的9*9棋盘并有10个雷的扫雷游戏。 1&#xff0c;可以用数组存放&#xff0c;如果有雷就用1表示&#xff0c;没雷就用0表示。 2&#xff0c;排查(2,5)这个坐标时&#xff0c;我们访问周围的⼀圈8个位置黄色统计周围雷的个数是1。排查(8,6)这个坐标时&#xf…...

二分法篇——于上下边界的扭转压缩间,窥见正解辉映之光(1)

前言 二分法&#xff0c;这一看似简单却又充满哲理的算法&#xff0c;犹如一道精巧的数学之门&#xff0c;带领我们在问题的迷雾中找到清晰的道路。它的名字虽简单&#xff0c;却深藏着智慧的光辉。在科学的浩瀚星空中&#xff0c;二分法如一颗璀璨的星辰&#xff0c;指引着我们…...

# 01_Python基础到实战一飞冲天(三)--python面向对象(一)--简单类

01_Python基础到实战一飞冲天&#xff08;三&#xff09;–python面向对象&#xff08;一&#xff09;–简单类 一、面向对象-01-基本概念 1、面向对象(OOP) 面向对象编程 —— Object Oriented Programming 简写 OOP。 2、面向对象(OOP) 学习目标 了解 面向对象 基本概念…...

sentinel使用手册

1.引入依赖 <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>2.yaml spring:cloud:sentinel:transport:dashboard: localhost:8090 #sentinel控制台地址…...

搜索二维矩阵 II(java)

题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性&#xff1a; 每行的元素从左到右升序排列。每列的元素从上到下升序排列。 代码思路&#xff1a; 用暴力算法&#xff1a; class Solution {public boolean searchMatrix(…...

Python语法基础(四)

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 高阶函数之map 高阶函数就是说&#xff0c;A函数作为B函数的参数&#xff0c;B函数就是高阶函数 map&#xff1a;映射 map(func,iterable) 这个是map的基本语法&#xff0c;…...

03_Django视图

三、Django模板 模板Templates 在Django框架中&#xff0c;模板是可以帮助开发者快速生成呈现给用户页面的工具 模板的设计方式实现了我们MVT中VT的解耦(M:Model&#xff0c;V:View&#xff0c;T:Template)&#xff0c;VT有着N:M的关系&#xff0c;一个V可以调用任意T&#xf…...

如何从 Hugging Face 数据集中随机采样数据并保存为新的 Arrow 文件

如何从 Hugging Face 数据集中随机采样数据并保存为新的 Arrow 文件 在使用 Hugging Face 的数据集进行模型训练时&#xff0c;有时我们并不需要整个数据集&#xff0c;尤其是当数据集非常大时。为了节省存储空间和提高训练效率&#xff0c;我们可以从数据集中随机采样一部分数…...

11 设计模式之代理模式(送资料案例)

一、什么是代理模式&#xff1f; 在现实生活中&#xff0c;我们常常遇到这样的场景&#xff1a;由于某些原因&#xff0c;我们可能无法亲自完成某个任务&#xff0c;便会委托他人代为执行。在设计模式中&#xff0c;代理模式 就是用来解决这种“委托”问题的&#xff0…...

MongoDB聚合操作

1.聚合操作 聚合操作处理数据记录并返回计算结果。聚合操作组值来自多个文档&#xff0c;可以对分组数据执行各种操作以返回单个结果。聚合操作包含三类&#xff1a;单一作用聚合、聚合管道、MapReduce。 单一作用聚合&#xff1a;提供了对常见聚合过程的简单访问&#xff0c…...

第二十三周周报:High-fidelity Person-centric Subject-to-Image Synthesis

目录 摘要 Abstract TDM SDM SNF 测试时的人物细节捕捉 主要贡献 总结 摘要 本周阅读了一篇2024年CVPR的关于高保真度、以人物为中心的图像合成方法的论文&#xff1a;High-fidelity Person-centric Subject-to-Image Synthesis。该论文提出了一种名为Face-diffuser的…...

Cesium 与 Leaflet:地理信息可视化技术比较

在现代地理信息系统(GIS)和空间数据可视化领域,Cesium 和 Leaflet 是两种非常常见的地理可视化库,它们各自适用于不同的应用场景。Cesium 专注于三维地球视图和复杂空间分析,而 Leaflet 则注重轻量级的二维地图展示。本文将对这两种技术进行详细的对比,帮助开发者根据具体…...

Linux 服务器使用指南:诞生与演进以及版本(一)

一、引言 在当今信息技术的浪潮中&#xff0c;Linux 操作系统无疑是一个关键的支柱&#x1f60e;。无论是在服务器管理、软件开发还是大数据处理领域&#xff0c;Linux 都以其卓越的适应性和优势脱颖而出&#x1f44d;。然而&#xff0c;对于许多新手而言&#xff0c;Linux 系统…...

龙蜥 Linux 安装 JDK

龙蜥 Linux 安装 JDK 下载安装解压到目标路径设置环境变量直接在启动脚本中临时设置 参考资料 下载 这个就不赘述了&#xff0c;参考资料中的另外两篇安装帖&#xff0c;都有。 如果不能上网&#xff0c;也可以去内网其他之前装过JDK的服务器&#xff0c;直接复制过来。 tar …...

Python小白语法基础20(模块与包)

0) 参考文章 python的模块(module)、包(package)及pip_python package-CSDN博客Python之函数、模块、包库_python函数、模块和包-CSDN博客Python函数模块自定义封装及模块嵌套导入&#xff08;手把手教程&#xff09;_python如何封装一个模块-CSDN博客 1) 模块与包说明 软件…...

Qt Widget类解析与代码注释

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码&#xff0c;写上注释 当然可以&#xff01;这段代码是 Qt …...

1688商品列表API与其他数据源的对接思路

将1688商品列表API与其他数据源对接时&#xff0c;需结合业务场景设计数据流转链路&#xff0c;重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点&#xff1a; 一、核心对接场景与目标 商品数据同步 场景&#xff1a;将1688商品信息…...

IoT/HCIP实验-3/LiteOS操作系统内核实验(任务、内存、信号量、CMSIS..)

文章目录 概述HelloWorld 工程C/C配置编译器主配置Makefile脚本烧录器主配置运行结果程序调用栈 任务管理实验实验结果osal 系统适配层osal_task_create 其他实验实验源码内存管理实验互斥锁实验信号量实验 CMISIS接口实验还是得JlINKCMSIS 简介LiteOS->CMSIS任务间消息交互…...

大语言模型(LLM)中的KV缓存压缩与动态稀疏注意力机制设计

随着大语言模型&#xff08;LLM&#xff09;参数规模的增长&#xff0c;推理阶段的内存占用和计算复杂度成为核心挑战。传统注意力机制的计算复杂度随序列长度呈二次方增长&#xff0c;而KV缓存的内存消耗可能高达数十GB&#xff08;例如Llama2-7B处理100K token时需50GB内存&a…...

GitHub 趋势日报 (2025年06月06日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 590 cognee 551 onlook 399 project-based-learning 348 build-your-own-x 320 ne…...

BLEU评分:机器翻译质量评估的黄金标准

BLEU评分&#xff1a;机器翻译质量评估的黄金标准 1. 引言 在自然语言处理(NLP)领域&#xff0c;衡量一个机器翻译模型的性能至关重要。BLEU (Bilingual Evaluation Understudy) 作为一种自动化评估指标&#xff0c;自2002年由IBM的Kishore Papineni等人提出以来&#xff0c;…...

【Elasticsearch】Elasticsearch 在大数据生态圈的地位 实践经验

Elasticsearch 在大数据生态圈的地位 & 实践经验 1.Elasticsearch 的优势1.1 Elasticsearch 解决的核心问题1.1.1 传统方案的短板1.1.2 Elasticsearch 的解决方案 1.2 与大数据组件的对比优势1.3 关键优势技术支撑1.4 Elasticsearch 的竞品1.4.1 全文搜索领域1.4.2 日志分析…...

ui框架-文件列表展示

ui框架-文件列表展示 介绍 UI框架的文件列表展示组件&#xff0c;可以展示文件夹&#xff0c;支持列表展示和图标展示模式。组件提供了丰富的功能和可配置选项&#xff0c;适用于文件管理、文件上传等场景。 功能特性 支持列表模式和网格模式的切换展示支持文件和文件夹的层…...

CTF show 数学不及格

拿到题目先查一下壳&#xff0c;看一下信息 发现是一个ELF文件&#xff0c;64位的 ​ 用IDA Pro 64 打开这个文件 ​ 然后点击F5进行伪代码转换 可以看到有五个if判断&#xff0c;第一个argc ! 5这个判断并没有起太大作用&#xff0c;主要是下面四个if判断 ​ 根据题目…...

网页端 js 读取发票里的二维码信息(图片和PDF格式)

起因 为了实现在报销流程中&#xff0c;发票不能重用的限制&#xff0c;发票上传后&#xff0c;希望能读出发票号&#xff0c;并记录发票号已用&#xff0c;下次不再可用于报销。 基于上面的需求&#xff0c;研究了OCR 的方式和读PDF的方式&#xff0c;实际是可行的&#xff…...