From cf5e84da0a5d9f81260e0f85dd4bcdcfa93153cd Mon Sep 17 00:00:00 2001 From: kangwenjing <1138819403@qq.com> Date: Thu, 29 Oct 2026 15:21:42 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E6=B7=BB=E5=8A=A0=20all-in-one=20?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E6=96=B9=E6=A1=88=E5=8F=8A=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增内容包括: - all-in-one 镜像构建脚本与 Dockerfile,整合前后端及 Nginx - K3s 部署清单与最小化版本 - 健康检查脚本、Nginx 代理配置 - 生产环境数据库连接池优化配置 - 序列同步 SQL 脚本与清单校验工具 - .dockerignore、.gitignore 及部署文档 --- .dockerignore | 32 ++ .gitignore | 3 +- .../src/main/resources/application-prod.yml | 34 +- deploy/README.md | 113 ++++++ deploy/all-in-one/Dockerfile | 59 +++ deploy/all-in-one/healthcheck.sh | 7 + deploy/all-in-one/nginx/proxy-backend.conf | 10 + deploy/all-in-one/nginx/unis-scc.conf | 94 +++++ deploy/all-in-one/start.sh | 44 +++ deploy/build-and-push.sh | 71 ++++ deploy/check-manifests.py | 42 +++ deploy/k3s-unis-scc-allinone.min.yaml | 121 +++++++ deploy/k3s-unis-scc-allinone.yaml | 171 +++++++++ deploy/k3s-unis-scc.yaml | 342 ++++++++++++++++++ sql/20261029_prod_sequence_sync_pg16.sql | 146 ++++++++ 15 files changed, 1281 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 deploy/README.md create mode 100644 deploy/all-in-one/Dockerfile create mode 100755 deploy/all-in-one/healthcheck.sh create mode 100644 deploy/all-in-one/nginx/proxy-backend.conf create mode 100644 deploy/all-in-one/nginx/unis-scc.conf create mode 100755 deploy/all-in-one/start.sh create mode 100755 deploy/build-and-push.sh create mode 100755 deploy/check-manifests.py create mode 100644 deploy/k3s-unis-scc-allinone.min.yaml create mode 100644 deploy/k3s-unis-scc-allinone.yaml create mode 100644 deploy/k3s-unis-scc.yaml create mode 100644 sql/20261029_prod_sequence_sync_pg16.sql diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..92e22585 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,32 @@ +# 仓库根目录作为构建上下文时使用(见 deploy/all-in-one/Dockerfile) +.git +.idea +.vscode +.claude +.workbuddy +.qoder +docs +doc +dist +sql +tmp +uploads +deploy/*.tar +node_modules +frontend/node_modules +frontend1/node_modules +frontend/dist +frontend1/dist +frontend/.cert +frontend1/.cert +backend/src +backend/target/classes +backend/target/generated-sources +backend/target/generated-test-sources +backend/target/maven-archiver +backend/target/maven-status +backend/target/surefire-reports +backend/target/test-classes +backend/target/unis-crm-backend-1.0.0-SNAPSHOT.jar.original +**/*.log +**/.DS_Store diff --git a/.gitignore b/.gitignore index ba5322d1..1ce21244 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ frontend1/.cert/ *.log tmp/ -docs/ \ No newline at end of file +docs/ +deploy/*.tar diff --git a/backend/src/main/resources/application-prod.yml b/backend/src/main/resources/application-prod.yml index ade6b1de..48c95183 100644 --- a/backend/src/main/resources/application-prod.yml +++ b/backend/src/main/resources/application-prod.yml @@ -17,16 +17,36 @@ spring: max-file-size: 500MB max-request-size: 600MB datasource: - url: jdbc:postgresql://192.168.124.202:5432/unis_crm - username: postgres + # 生产 DB / Redis 走 K8s NodePort,空闲连接会被静默回收,需要显式超时兜底 + url: jdbc:postgresql://10.100.51.227:30532/unis_scc?ApplicationName=unis-crm-prod&connectTimeout=10&socketTimeout=120&tcpKeepAlive=true + username: root password: unis@123 driver-class-name: org.postgresql.Driver + hikari: + pool-name: crm-prod + # 实例 max_connections=100 且与 rda/unis 库共用,池子保持保守 + maximum-pool-size: 10 + minimum-idle: 2 + # 拿不到连接时 10s 快速失败,避免请求线程堆积 + connection-timeout: 10000 + # 连接最长存活 5min,短于 NodePort 空闲连接回收时间 + max-lifetime: 300000 + keepalive-time: 120000 + # 2min 未归还判定为泄漏;阈值留出大文件上传等正常慢操作余量 + leak-detection-threshold: 120000 + # 单条 SQL 60s 上限;事务内空闲 2min 判定为泄漏,避免长事务占锁并阻塞 vacuum + connection-init-sql: "SET statement_timeout = 60000; SET idle_in_transaction_session_timeout = 120000;" data: redis: - host: 192.168.124.202 - port: 6379 - password: zghz@123 - database: 12 + host: 10.100.51.227 + port: 30379 + password: unis@123 + database: 10 + # 命令与建连各 5s 超时,Redis 抖动时快速失败而不是挂住线程 + timeout: 5s + connect-timeout: 5s + lettuce: + shutdown-timeout: 500ms mybatis-plus: mapper-locations: classpath*:/mapper/**/*.xml @@ -84,7 +104,7 @@ unisbase: access-token-safety-seconds: 120 oms: enabled: ${OMS_ENABLED:true} - base-url: ${OMS_BASE_URL:http://192.168.124.202:28081} + base-url: ${OMS_BASE_URL:https://oms.unisspace.com/prod-api} api-key: ${OMS_API_KEY:c7f858d0-30b8-4b7f-9ea1-0ccf5ceb1c54} api-key-header: ${OMS_API_KEY_HEADER:apiKey} user-info-path: ${OMS_USER_INFO_PATH:/api/v1/user/info} diff --git a/deploy/README.md b/deploy/README.md new file mode 100644 index 00000000..70612250 --- /dev/null +++ b/deploy/README.md @@ -0,0 +1,113 @@ +# unis_scc 镜像构建与 k3s 部署 + +## 两种打包形态 + +| 形态 | 镜像 | 清单文件 | 说明 | +| --- | --- | --- | --- | +| **all-in-one(默认)** | `public/unis_scc` | `k3s-unis-scc-allinone.yaml` | 一个镜像一个 Pod:nginx 托管两个前端 + 同容器跑 Spring Boot | +| 分体 | `public/unis_scc-backend` / `-frontend` / `-system` | `k3s-unis-scc.yaml` | 三个镜像三个 Deployment,可独立升级回滚 | + +all-in-one 容器内端口:`80` CRM 前端、`8090` 系统管理前端、`8080` 后端 API。任一子进程退出,容器整体退出,由 k8s 重启。 + +## 构建并推送 + +```bash +docker login docker.oa.unissense.tech -u admin + +./deploy/build-and-push.sh # all-in-one,镜像名 unis_scc +./deploy/build-and-push.sh app +./deploy/build-and-push.sh backend frontend system # 分体三镜像 +TAG=202610291500 ./deploy/build-and-push.sh # 指定 tag(默认取当前时间 + latest) +PUSH=0 ./deploy/build-and-push.sh # 本地验证,不推送 +``` + +脚本会先用 JDK 17 执行 `mvn -DskipTests package`。后端依赖 `com.unisbase`,是只装在本地 `~/.m2` 的内部包,所以 jar 必须在宿主机打好,再由镜像 COPY。Apple Silicon 上走 amd64 模拟,前端构建约需几分钟。 + +all-in-one 的构建上下文是仓库根目录(`-f deploy/all-in-one/Dockerfile .`),根 `.dockerignore` 控制上下文体积。 + +## k3s 部署 + +```bash +# 节点可直连 Harbor:public 项目允许匿名拉取 +kubectl apply -f deploy/k3s-unis-scc-allinone.yaml +kubectl -n unis-scc rollout status deploy/unis-scc + +# 节点访问不了 Harbor:离线导入 +docker save -o /tmp/unis_scc.tar docker.oa.unissense.tech/public/unis_scc:latest +scp /tmp/unis_scc.tar node:/tmp/ && ssh node 'k3s ctr images import /tmp/unis_scc.tar' +``` + +访问入口(NodePort):CRM 前端 `:30082`、系统管理前端 `:30083`、后端 API `:30081`。 + +## 配置说明 + +数据库与 Redis 走镜像内置的 `application-prod.yml`(PG `10.100.51.227:30532/unis_scc`、Redis `10.100.51.227:30379` db10)。需要覆盖时在 Deployment 的 `env` 里加 `SPRING_DATASOURCE_URL`、`SPRING_DATA_REDIS_HOST`、`WECOM_*` 等变量,`JAVA_OPTS` 可改 JVM 参数。 + +分体形态下两个前端容器靠 `BACKEND_HOST` / `BACKEND_PORT` 渲染 nginx 反代配置;all-in-one 反代目标是 `127.0.0.1:8080`,不需要这俩变量。 + +项目没有引入 `spring-boot-actuator`,探活不能用 `/actuator/health`。all-in-one 用镜像内的 `/usr/local/bin/healthcheck.sh` 同时检查三个端口,分体形态的后端用 TCP 探活。 + +## 可选配置 + +### Harbor 私有项目的拉取凭证 + +`public` 项目允许匿名拉取,不需要下面这个。若把镜像挪到私有项目,再取消注释并打开 Deployment 里的 `imagePullSecrets`: + +```yaml +apiVersion: v1 +kind: Secret +metadata: + name: harbor-regcred + namespace: unis-scc +type: kubernetes.io/dockerconfigjson +stringData: + .dockerconfigjson: '{"auths":{"docker.oa.unissense.tech":{"username":"admin","password":"unix@1234"}}}' +``` + +### Ingress(k3s 自带 Traefik) + +两个前端是各自独立的 SPA、都按 `/` 为根构建,所以按域名拆比按路径拆省事: + +```yaml +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: unis-scc + namespace: unis-scc +spec: + rules: + - host: crm.unissense.top + http: + paths: + - { path: /, pathType: Prefix, backend: { service: { name: unis-scc, port: { name: crm } } } } + - host: scc-admin.unissense.top + http: + paths: + - { path: /, pathType: Prefix, backend: { service: { name: unis-scc, port: { name: sys } } } } +``` + +## 导入报错排查 + +**`TypeError: Cannot read properties of null (reading 'metadata')`** + +原因:清单里存在"空文档"——`---` 之后整段都是注释,YAML 解析出一个 `null`,按数组逐个取 `metadata` 就炸。kubectl 会忽略空文档,所以命令行正常、GUI 导入失败。现已把所有示例注释移出 YAML(改放本 README),并加了校验: + +```bash +./deploy/check-manifests.py # 检查同目录全部清单,报出空文档/缺字段的文档序号 +``` + +仍失败就用无注释等价版 `k3s-unis-scc-allinone.min.yaml`(与主文件解析结果逐字段一致,绕开注释相关的解析差异)。 + +**离线导入后 ErrImagePull**:`imagePullPolicy` 必须是 `IfNotPresent`,否则 kubelet 仍会去 Harbor 拉取。 + +**改完 YAML 记得重新生成 min 版**: + +```bash +python3 - <<'PY' +import yaml +src, dst = 'k3s-unis-scc-allinone.yaml', 'k3s-unis-scc-allinone.min.yaml' +docs = [d for d in yaml.safe_load_all(open(src, encoding='utf-8')) if d is not None] +open(dst, 'w', encoding='utf-8').write( + "---\n".join(yaml.dump(d, sort_keys=False, default_flow_style=False, allow_unicode=True) for d in docs)) +PY +``` diff --git a/deploy/all-in-one/Dockerfile b/deploy/all-in-one/Dockerfile new file mode 100644 index 00000000..b848c28d --- /dev/null +++ b/deploy/all-in-one/Dockerfile @@ -0,0 +1,59 @@ +# ============================================================================= +# unis_scc all-in-one 镜像(linux/amd64) +# 构建上下文 = 仓库根目录: +# docker buildx build --platform linux/amd64 \ +# -f deploy/all-in-one/Dockerfile -t /public/unis_scc: . +# 后端 jar 需先在宿主机执行 mvn -DskipTests package(com.unisbase 是本地 Maven 依赖) +# ============================================================================= + +# ---- 构建 CRM 前端 ---- +FROM node:20-alpine AS crm-web +WORKDIR /build +COPY frontend/package.json frontend/package-lock.json* ./ +RUN npm ci --registry=https://registry.npmmirror.com +COPY frontend/ ./ +RUN npm run build + +# ---- 构建系统管理前端 ---- +FROM node:20-alpine AS sys-web +WORKDIR /build +COPY frontend1/package.json frontend1/package-lock.json* ./ +RUN npm ci --registry=https://registry.npmmirror.com +COPY frontend1/ ./ +RUN npm run build + +# ---- 运行阶段:JRE 17 + nginx ---- +FROM eclipse-temurin:17-jre + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update \ + && apt-get install -y --no-install-recommends nginx \ + && rm -rf /var/lib/apt/lists/* \ + # Debian 默认站点会抢占 80 端口的 default_server + && rm -f /etc/nginx/sites-enabled/default \ + && mkdir -p /usr/share/nginx/html/crm /usr/share/nginx/html/sys /app/logs + +COPY deploy/all-in-one/nginx/unis-scc.conf /etc/nginx/conf.d/unis-scc.conf +COPY deploy/all-in-one/nginx/proxy-backend.conf /etc/nginx/snippets/proxy-backend.conf +COPY deploy/all-in-one/start.sh /usr/local/bin/start.sh +COPY deploy/all-in-one/healthcheck.sh /usr/local/bin/healthcheck.sh + +COPY --from=crm-web /build/dist /usr/share/nginx/html/crm +COPY --from=sys-web /build/dist /usr/share/nginx/html/sys +COPY backend/target/unis-crm-backend-1.0.0-SNAPSHOT.jar /app/app.jar + +RUN chmod +x /usr/local/bin/start.sh /usr/local/bin/healthcheck.sh \ + && nginx -t + +WORKDIR /app +ENV TZ=Asia/Shanghai +ENV SPRING_PROFILES_ACTIVE=prod + +# 80 CRM 前端 / 8090 系统管理前端 / 8080 后端 API +EXPOSE 80 8090 8080 + +HEALTHCHECK --interval=30s --timeout=5s --start-period=90s --retries=5 \ + CMD ["/usr/local/bin/healthcheck.sh"] + +ENTRYPOINT ["/usr/local/bin/start.sh"] diff --git a/deploy/all-in-one/healthcheck.sh b/deploy/all-in-one/healthcheck.sh new file mode 100755 index 00000000..7ed58033 --- /dev/null +++ b/deploy/all-in-one/healthcheck.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# 同时确认 nginx(:80) 与 Spring Boot(:8080) 都在监听 +set -uo pipefail +bash -c ': /dev/null + [ -n "$APP_PID" ] && kill -TERM "$APP_PID" 2>/dev/null + return 0 +} +trap 'cleanup; exit 0' TERM INT + +echo "[start] java $JAVA_OPTS -jar /app/app.jar (profile=$SPRING_PROFILES_ACTIVE)" +# shellcheck disable=SC2086 +java $JAVA_OPTS -jar /app/app.jar & +APP_PID=$! + +echo "[start] nginx (crm :80 / sys :8090)" +nginx -g 'daemon off;' & +NGX_PID=$! + +code=0 +while true; do + if ! kill -0 "$APP_PID" 2>/dev/null; then + wait "$APP_PID"; code=$? + echo "[start] 后端进程退出 code=$code,容器随之退出" + break + fi + if ! kill -0 "$NGX_PID" 2>/dev/null; then + wait "$NGX_PID"; code=$? + echo "[start] nginx 进程退出 code=$code,容器随之退出" + break + fi + sleep 2 +done + +cleanup +exit "$code" diff --git a/deploy/build-and-push.sh b/deploy/build-and-push.sh new file mode 100755 index 00000000..009f83d3 --- /dev/null +++ b/deploy/build-and-push.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# 构建 unis_scc 镜像(linux/amd64)并推送到 Harbor。 +# +# ./deploy/build-and-push.sh # 默认:all-in-one 单镜像 unis_scc +# ./deploy/build-and-push.sh app # 同上,显式指定 +# ./deploy/build-and-push.sh backend frontend system # 分别打三个镜像 +# TAG=202610291500 ./deploy/build-and-push.sh +# PUSH=0 ./deploy/build-and-push.sh # 只构建不推送 +set -euo pipefail + +REGISTRY="${REGISTRY:-docker.oa.unissense.tech}" +PROJECT="${PROJECT:-public}" +BASE_NAME="${BASE_NAME:-unis_scc}" +TAG="${TAG:-$(date +%Y%m%d%H%M)}" +PLATFORM="${PLATFORM:-linux/amd64}" +PUSH="${PUSH:-1}" + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +BACKEND_JAR="$ROOT_DIR/backend/target/unis-crm-backend-1.0.0-SNAPSHOT.jar" + +# 组件 -> "构建上下文|Dockerfile|镜像后缀" +spec() { + case "$1" in + app) echo "$ROOT_DIR|$ROOT_DIR/deploy/all-in-one/Dockerfile|" ;; + backend) echo "$ROOT_DIR/backend|$ROOT_DIR/backend/Dockerfile|-backend" ;; + frontend) echo "$ROOT_DIR/frontend|$ROOT_DIR/frontend/Dockerfile|-frontend" ;; + system) echo "$ROOT_DIR/frontend1|$ROOT_DIR/frontend1/Dockerfile|-system" ;; + *) return 1 ;; + esac +} + +targets=("$@") +if [ ${#targets[@]} -eq 0 ]; then + targets=(app) +fi + +need_jar=0 +for t in "${targets[@]}"; do + case "$t" in app|backend) need_jar=1 ;; esac +done + +if [ "$need_jar" = 1 ]; then + echo "==> 重新打包后端 jar(JDK 17)" + JAVA_HOME="${JAVA_HOME_17:-$(/usr/libexec/java_home -v 17)}" \ + mvn -B -q -DskipTests -f "$ROOT_DIR/backend/pom.xml" clean package + [ -f "$BACKEND_JAR" ] || { echo "缺少 $BACKEND_JAR"; exit 1; } +fi + +for comp in "${targets[@]}"; do + IFS='|' read -r ctx dockerfile suffix < <(spec "$comp") || { echo "未知组件: $comp"; exit 1; } + image="$REGISTRY/$PROJECT/$BASE_NAME$suffix" + echo "==> 构建 $image:$TAG ($PLATFORM)" + docker buildx build --platform "$PLATFORM" \ + -f "$dockerfile" \ + -t "$image:$TAG" -t "$image:latest" \ + --load "$ctx" +done + +if [ "$PUSH" != 1 ]; then + echo "==> PUSH=0,跳过推送"; exit 0 +fi + +for comp in "${targets[@]}"; do + IFS='|' read -r _ _ suffix < <(spec "$comp") + image="$REGISTRY/$PROJECT/$BASE_NAME$suffix" + echo "==> 推送 $image" + docker push "$image:$TAG" + docker push "$image:latest" +done + +echo "==> 完成,TAG=$TAG" diff --git a/deploy/check-manifests.py b/deploy/check-manifests.py new file mode 100755 index 00000000..86a77101 --- /dev/null +++ b/deploy/check-manifests.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""校验 k8s 清单,专防"注释成段被注释掉后留下空文档"这类导入器炸点。 + +用法: ./deploy/check-manifests.py [文件...] # 不传参则检查同目录下所有 yaml +""" +import sys +from pathlib import Path + +import yaml + +REQUIRED = {"apiVersion", "kind", "metadata"} + + +def main(argv: list[str]) -> int: + here = Path(__file__).resolve().parent + files = [Path(a) for a in argv] if argv else sorted(here.glob("*.yaml")) + failed = False + for f in files: + docs = list(yaml.safe_load_all(f.read_text(encoding="utf-8"))) + problems = [] + for i, d in enumerate(docs): + if d is None: + problems.append(f"doc#{i} 是空文档(--- 之后只有注释),导入器读 metadata 会得到 null") + elif not isinstance(d, dict): + problems.append(f"doc#{i} 不是映射对象: {type(d).__name__}") + elif missing := REQUIRED - set(d): + problems.append(f"doc#{i} 缺少字段 {sorted(missing)}") + elif not d["metadata"].get("name"): + problems.append(f"doc#{i} metadata.name 为空") + if problems: + failed = True + print(f"FAIL {f.name}") + for p in problems: + print(f" - {p}") + else: + kinds = ", ".join(f"{d['kind']}/{d['metadata']['name']}" for d in docs) + print(f"OK {f.name} ({len(docs)} 个资源) {kinds}") + return 1 if failed else 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv[1:])) diff --git a/deploy/k3s-unis-scc-allinone.min.yaml b/deploy/k3s-unis-scc-allinone.min.yaml new file mode 100644 index 00000000..42f75354 --- /dev/null +++ b/deploy/k3s-unis-scc-allinone.min.yaml @@ -0,0 +1,121 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: unis-scc + labels: + app.kubernetes.io/part-of: unis-scc +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: unis-scc + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc +spec: + replicas: 1 + revisionHistoryLimit: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + selector: + matchLabels: + app.kubernetes.io/name: unis-scc + template: + metadata: + labels: + app.kubernetes.io/name: unis-scc + spec: + terminationGracePeriodSeconds: 45 + containers: + - name: unis-scc + image: docker.oa.unissense.tech/public/unis_scc:latest + imagePullPolicy: IfNotPresent + ports: + - name: crm + containerPort: 80 + - name: sys + containerPort: 8090 + - name: api + containerPort: 8080 + env: + - name: TZ + value: Asia/Shanghai + - name: SPRING_PROFILES_ACTIVE + value: prod + resources: + requests: + cpu: 300m + memory: 1Gi + limits: + cpu: '2' + memory: 2Gi + startupProbe: + exec: + command: + - /usr/local/bin/healthcheck.sh + periodSeconds: 5 + failureThreshold: 60 + readinessProbe: + exec: + command: + - /usr/local/bin/healthcheck.sh + periodSeconds: 10 + timeoutSeconds: 5 + livenessProbe: + exec: + command: + - /usr/local/bin/healthcheck.sh + initialDelaySeconds: 30 + periodSeconds: 20 + timeoutSeconds: 5 + failureThreshold: 5 + volumeMounts: + - name: app-logs + mountPath: /app/logs + volumes: + - name: app-logs + emptyDir: {} +--- +apiVersion: v1 +kind: Service +metadata: + name: unis-scc + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc +spec: + type: NodePort + selector: + app.kubernetes.io/name: unis-scc + ports: + - name: crm + port: 80 + targetPort: crm + nodePort: 30082 + - name: sys + port: 8090 + targetPort: sys + nodePort: 30083 + - name: api + port: 8080 + targetPort: api + nodePort: 30081 +--- +apiVersion: v1 +kind: Service +metadata: + name: unis-scc-api + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc +spec: + type: ClusterIP + selector: + app.kubernetes.io/name: unis-scc + ports: + - name: api + port: 8080 + targetPort: api diff --git a/deploy/k3s-unis-scc-allinone.yaml b/deploy/k3s-unis-scc-allinone.yaml new file mode 100644 index 00000000..b13050ec --- /dev/null +++ b/deploy/k3s-unis-scc-allinone.yaml @@ -0,0 +1,171 @@ +# ============================================================================ +# unis_scc(UNIS CRM)—— k3s 单镜像 all-in-one 部署 +# +# 镜像:docker.oa.unissense.tech/public/unis_scc:latest (linux/amd64) +# 一个容器里同时跑: +# nginx :80 -> CRM 前端静态站点 + /api、/sys 反代 +# nginx :8090 -> 系统管理前端静态站点 + /api、/sys 反代 +# java :8080 -> Spring Boot 后端(profile=prod) +# 任一子进程退出,容器整体退出,由 k8s 重启,不会出现"半死不活"的 Pod。 +# +# 在线部署(public 项目允许匿名拉取): +# kubectl apply -f k3s-unis-scc-allinone.yaml +# kubectl -n unis-scc rollout status deploy/unis-scc +# +# 离线导入(节点访问不了 Harbor): +# 离线包已生成:deploy/unis_scc-202610291459.tar(163MB) +# scp deploy/unis_scc-202610291459.tar node:/tmp/ && ssh node 'k3s ctr images import /tmp/unis_scc-202610291459.tar' +# kubectl apply -f k3s-unis-scc-allinone.yaml +# +# 访问入口(NodePort): +# CRM 前端 http://<节点IP>:30082 +# 系统管理前端 http://<节点IP>:30083 +# 后端 API http://<节点IP>:30081 (调试用;正常流量走容器内 nginx) +# +# 数据库/Redis 用镜像内置的 application-prod.yml: +# PG 10.100.51.227:30532/unis_scc Redis 10.100.51.227:30379 db10 +# 可选的 Harbor 拉取凭证、Ingress 示例见 deploy/README.md +# ============================================================================ + +apiVersion: v1 +kind: Namespace +metadata: + name: unis-scc + labels: + app.kubernetes.io/part-of: unis-scc + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: unis-scc + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc +spec: + replicas: 1 + revisionHistoryLimit: 5 + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + selector: + matchLabels: + app.kubernetes.io/name: unis-scc + template: + metadata: + labels: + app.kubernetes.io/name: unis-scc + spec: + # imagePullSecrets: + # - name: harbor-regcred + # 后端是 Spring Boot,优雅停机需要点时间 + terminationGracePeriodSeconds: 45 + containers: + - name: unis-scc + image: docker.oa.unissense.tech/public/unis_scc:latest + # 离线导入场景必须 IfNotPresent,否则仍会去 Harbor 拉取并 ErrImagePull; + # 想每次强制拉最新,改成 Always 或把 image 换成时间戳 tag + imagePullPolicy: IfNotPresent + ports: + - name: crm + containerPort: 80 + - name: sys + containerPort: 8090 + - name: api + containerPort: 8080 + env: + - name: TZ + value: Asia/Shanghai + - name: SPRING_PROFILES_ACTIVE + value: prod + # 容器入口脚本会原样拼到 java 命令上,默认 -XX:MaxRAMPercentage=70.0 + # - name: JAVA_OPTS + # value: "-Xms512m -Xmx1536m" + # 需要覆盖镜像内配置时打开: + # - name: SPRING_DATASOURCE_URL + # value: "jdbc:postgresql://10.100.51.227:30532/unis_scc?connectTimeout=10&socketTimeout=120&tcpKeepAlive=true" + # - name: WECOM_ENABLED + # value: "true" + # 企业微信 SSO 回调域名等 + # - name: WECOM_REDIRECT_URI + # value: "https://crm.unissense.top/api/wecom/sso/callback" + resources: + requests: + cpu: 300m + memory: 1Gi + limits: + cpu: "2" + memory: 2Gi + # 镜像里的 healthcheck.sh 会同时确认 :80 / :8090 / :8080 都在监听 + startupProbe: + exec: + command: ["/usr/local/bin/healthcheck.sh"] + periodSeconds: 5 + failureThreshold: 60 + readinessProbe: + exec: + command: ["/usr/local/bin/healthcheck.sh"] + periodSeconds: 10 + timeoutSeconds: 5 + livenessProbe: + exec: + command: ["/usr/local/bin/healthcheck.sh"] + initialDelaySeconds: 30 + periodSeconds: 20 + timeoutSeconds: 5 + failureThreshold: 5 + volumeMounts: + - name: app-logs + mountPath: /app/logs + volumes: + # 日志主要走 stdout(kubectl logs deploy/unis-scc),这里只是给应用留可写目录 + - name: app-logs + emptyDir: {} + +--- +# 统一入口:一个 Service 暴露三个端口 +apiVersion: v1 +kind: Service +metadata: + name: unis-scc + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc +spec: + type: NodePort + selector: + app.kubernetes.io/name: unis-scc + ports: + - name: crm + port: 80 + targetPort: crm + nodePort: 30082 + - name: sys + port: 8090 + targetPort: sys + nodePort: 30083 + - name: api + port: 8080 + targetPort: api + nodePort: 30081 + +--- +# 集群内网调用后端用的 ClusterIP,省得依赖 NodePort +apiVersion: v1 +kind: Service +metadata: + name: unis-scc-api + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc +spec: + type: ClusterIP + selector: + app.kubernetes.io/name: unis-scc + ports: + - name: api + port: 8080 + targetPort: api + diff --git a/deploy/k3s-unis-scc.yaml b/deploy/k3s-unis-scc.yaml new file mode 100644 index 00000000..68d05e60 --- /dev/null +++ b/deploy/k3s-unis-scc.yaml @@ -0,0 +1,342 @@ +# ============================================================================ +# unis_scc (UNIS CRM) —— k3s 部署清单 +# +# 镜像(linux/amd64,Harbor: docker.oa.unissense.tech,project = public 公共项目) +# docker.oa.unissense.tech/public/unis_scc-backend:202610291447 Spring Boot 后端 :8080 +# docker.oa.unissense.tech/public/unis_scc-frontend:202610291447 CRM 前端 nginx :80 +# docker.oa.unissense.tech/public/unis_scc-system:202610291447 系统管理前端 nginx :80 +# +# 在线部署(k3s 节点能直连 Harbor,公共项目可匿名拉取): +# kubectl apply -f k3s-unis-scc.yaml +# kubectl -n unis-scc rollout status deployment/unis-scc-backend +# kubectl -n unis-scc rollout status deployment/unis-scc-frontend +# kubectl -n unis-scc rollout status deployment/unis-scc-system +# +# 离线导入(节点访问不了 Harbor 时): +# k3s ctr images import unis_scc-images-202610291447.tar # 离线包已在 deploy/ 下生成 +# kubectl apply -f k3s-unis-scc.yaml +# +# 访问入口(NodePort): +# CRM 前端 http://<节点IP>:30082 +# 系统管理前端 http://<节点IP>:30083 (/sys/api/* 走同一后端) +# 后端 API http://<节点IP>:30081 (仅调试用,正常流量走前端 nginx 反代) +# +# 数据库/Redis 不再走 ConfigMap,已烘进镜像里的 application-prod.yml: +# PG 10.100.51.227:30532/unis_scc +# Redis 10.100.51.227:30379 db10 +# 如需临时覆盖,见下方 backend Deployment 里注释掉的 env 段。 +# 可选的 Harbor 拉取凭证、Ingress 示例见 deploy/README.md +# ============================================================================ + +apiVersion: v1 +kind: Namespace +metadata: + name: unis-scc + labels: + app.kubernetes.io/part-of: unis-scc + +# --- 私有项目才需要;public 项目可匿名拉取,留作备用 --- +# apiVersion: v1 +# kind: Secret +# metadata: +# name: harbor-regcred +# namespace: unis-scc +# type: kubernetes.io/dockerconfigjson +# stringData: +# .dockerconfigjson: '{"auths":{"docker.oa.unissense.tech":{"username":"admin","password":"unix@1234"}}}' + +--- +# =========================================================================== +# 后端:Spring Boot(profile=prod),监听 8080 +# =========================================================================== +apiVersion: apps/v1 +kind: Deployment +metadata: + name: unis-scc-backend + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend +spec: + replicas: 1 + revisionHistoryLimit: 3 + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + selector: + matchLabels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend + template: + metadata: + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend + spec: + # imagePullSecrets: + # - name: harbor-regcred + terminationGracePeriodSeconds: 45 + containers: + - name: backend + image: docker.oa.unissense.tech/public/unis_scc-backend:202610291447 + imagePullPolicy: IfNotPresent + # 镜像 ENTRYPOINT 是 `java -jar app.jar`,这里只补 JVM 参数,不改启动方式 + command: ["java"] + args: + - "-XX:MaxRAMPercentage=70.0" + - "-XX:+UseG1GC" + - "-Duser.timezone=Asia/Shanghai" + - "-jar" + - "/app/app.jar" + ports: + - name: http + containerPort: 8080 + env: + - name: TZ + value: Asia/Shanghai + - name: SPRING_PROFILES_ACTIVE + value: prod + # - name: SPRING_DATASOURCE_URL + # value: "jdbc:postgresql://10.100.51.227:30532/unis_scc?connectTimeout=10&socketTimeout=120&tcpKeepAlive=true" + # - name: SPRING_DATA_REDIS_HOST + # value: "10.100.51.227" + # - name: WECOM_ENABLED + # value: "true" + resources: + requests: + cpu: 250m + memory: 768Mi + limits: + cpu: "1500m" + memory: 1800Mi + # 项目未引入 spring-boot-actuator,用 TCP 探活 + startupProbe: + tcpSocket: + port: http + periodSeconds: 5 + failureThreshold: 60 + readinessProbe: + tcpSocket: + port: http + periodSeconds: 10 + timeoutSeconds: 3 + livenessProbe: + tcpSocket: + port: http + initialDelaySeconds: 15 + periodSeconds: 20 + timeoutSeconds: 3 + failureThreshold: 5 + volumeMounts: + - name: app-logs + mountPath: /app/logs + volumes: + # 日志主要走 stdout(kubectl logs 即可查看),这里只是给应用留出可写目录 + - name: app-logs + emptyDir: {} + +--- +apiVersion: v1 +kind: Service +metadata: + name: unis-scc-backend + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend +spec: + type: ClusterIP + selector: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend + ports: + - name: http + port: 8080 + targetPort: http + +--- +# 后端 NodePort,便于从集群外直接调 API / 健康检查 +apiVersion: v1 +kind: Service +metadata: + name: unis-scc-backend-external + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend +spec: + type: NodePort + selector: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: backend + ports: + - name: http + port: 8080 + targetPort: http + nodePort: 30081 + +--- +# =========================================================================== +# CRM 前端:nginx 静态站点 + /api、/sys 反向代理到后端 +# =========================================================================== +apiVersion: apps/v1 +kind: Deployment +metadata: + name: unis-scc-frontend + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: frontend +spec: + replicas: 1 + revisionHistoryLimit: 3 + selector: + matchLabels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: frontend + template: + metadata: + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: frontend + spec: + # imagePullSecrets: + # - name: harbor-regcred + containers: + - name: frontend + image: docker.oa.unissense.tech/public/unis_scc-frontend:202610291447 + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 80 + env: + - name: TZ + value: Asia/Shanghai + # 容器启动脚本会把这两个值渲染进 nginx 配置 + - name: BACKEND_HOST + value: unis-scc-backend.unis-scc.svc.cluster.local + - name: BACKEND_PORT + value: "8080" + resources: + requests: + cpu: 20m + memory: 32Mi + limits: + cpu: 300m + memory: 128Mi + readinessProbe: + httpGet: + path: / + port: http + periodSeconds: 10 + livenessProbe: + httpGet: + path: / + port: http + initialDelaySeconds: 10 + periodSeconds: 20 + failureThreshold: 5 + +--- +apiVersion: v1 +kind: Service +metadata: + name: unis-scc-frontend + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: frontend +spec: + type: NodePort + selector: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: frontend + ports: + - name: http + port: 80 + targetPort: http + nodePort: 30082 + +--- +# =========================================================================== +# 系统管理前端:nginx 静态站点,/sys/api/* 转发到后端 /sys/* +# =========================================================================== +apiVersion: apps/v1 +kind: Deployment +metadata: + name: unis-scc-system + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: system +spec: + replicas: 1 + revisionHistoryLimit: 3 + selector: + matchLabels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: system + template: + metadata: + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: system + spec: + # imagePullSecrets: + # - name: harbor-regcred + containers: + - name: system + image: docker.oa.unissense.tech/public/unis_scc-system:202610291447 + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 80 + env: + - name: TZ + value: Asia/Shanghai + - name: BACKEND_HOST + value: unis-scc-backend.unis-scc.svc.cluster.local + - name: BACKEND_PORT + value: "8080" + resources: + requests: + cpu: 20m + memory: 32Mi + limits: + cpu: 300m + memory: 128Mi + readinessProbe: + httpGet: + path: / + port: http + periodSeconds: 10 + livenessProbe: + httpGet: + path: / + port: http + initialDelaySeconds: 10 + periodSeconds: 20 + failureThreshold: 5 + +--- +apiVersion: v1 +kind: Service +metadata: + name: unis-scc-system + namespace: unis-scc + labels: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: system +spec: + type: NodePort + selector: + app.kubernetes.io/name: unis-scc + app.kubernetes.io/component: system + ports: + - name: http + port: 80 + targetPort: http + nodePort: 30083 + diff --git a/sql/20261029_prod_sequence_sync_pg16.sql b/sql/20261029_prod_sequence_sync_pg16.sql new file mode 100644 index 00000000..1d0b6055 --- /dev/null +++ b/sql/20261029_prod_sequence_sync_pg16.sql @@ -0,0 +1,146 @@ +-- ============================================================================ +-- 生产库迁移后同步序列:unis_crm(PG17@192.168.124.202) -> unis_scc(PG16@10.100.51.227) +-- +-- 成因(已实测复现):Navicat 的“结构导出”先 CREATE SEQUENCE "x_id_seq"(START 1,不带 +-- 源库当前值),再 CREATE TABLE ... GENERATED BY DEFAULT AS IDENTITY。规范名被占用, +-- identity 只能另建一个 *_id_seq1 并从 1 开始。新库现状:34 个 identity 列绑在 +-- *_id_seq1 上,94 个序列里有 37 个已无任何列引用(其中一部分还残留着旧库数值)。 +-- 所以不要按 "<表>_<列>_seq" 猜序列名,必须用 pg_get_serial_sequence() 解析; +-- 否则连 pg_dump 自带的 setval 都会打到那些孤儿序列上,不报错但完全不生效。 +-- +-- 首选做法:用原生 pg_dump 把结构+数据一次性恢复进空库,序列名与数值都会自动对齐, +-- 本脚本只是兜底自检(正常输出“上调 0 个,剩余落后 0 个”)。 +-- +-- 用法:灌完数据之后、放量之前执行。只在序列落后时上调,从不下调,可重复执行。 +-- psql "host=10.100.51.227 port=30532 dbname=unis_scc user=root" -f 本文件 +-- +-- 验证:已在 PostgreSQL 16.15 上按上述成因搭建夹具,复现 identity / serial / 业务编码 +-- 三种 duplicate key 失败,执行本脚本后全部恢复,重复执行幂等。 +-- ============================================================================ + +begin; + +-- 序列被别的会话占用时跳过并告警,不要卡住发布 +set local lock_timeout = '5s'; +set local statement_timeout = '10min'; + +-- 待检查清单:identity / serial 列由目录解析,业务编码序列按名字显式列出 +create temp table if not exists seq_targets (seq text, tbl text, col text, prefix text) on commit preserve rows; +truncate seq_targets; + +insert into seq_targets (seq, tbl, col, prefix) +select pg_get_serial_sequence(format('%I.%I', n.nspname, c.relname), a.attname), + format('%I.%I', n.nspname, c.relname), + a.attname, + null +from pg_attribute a +join pg_class c on c.oid = a.attrelid +join pg_namespace n on n.oid = c.relnamespace +where n.nspname = 'public' + and c.relkind = 'r' + and a.attnum > 0 + and (a.attidentity <> '' or exists ( + select 1 from pg_attrdef d + where d.adrelid = a.attrelid + and d.adnum = a.attnum + and pg_get_expr(d.adbin, d.adrelid) like 'nextval%')); + +-- 编码形如 QD-20261029-000822,mapper 里用 nextval() 直接取号 +insert into seq_targets (seq, tbl, col, prefix) values + ('public.crm_channel_expansion_code_seq', 'crm_channel_expansion', 'channel_code', 'QD'), + ('public.crm_customer_code_seq', 'crm_customer', 'customer_code', 'CUS'), + ('public.crm_opportunity_code_seq', 'crm_opportunity', 'opportunity_code', 'OPP'); + +do $$ +declare + r record; + v_next bigint; + v_max bigint; + v_fixed int := 0; + v_late int := 0; +begin + ---------------------------------------------------------------------------- + -- 第一轮:落后的上调到 max + 1 + ---------------------------------------------------------------------------- + for r in select seq, tbl, col, prefix from seq_targets where seq is not null order by tbl, col + loop + begin + if r.prefix is null then + execute format('select coalesce(max(%I), 0) from %s', r.col, r.tbl) into v_max; + else + execute format( + $f$select coalesce(max((substring(%I from '([0-9]+)$'))::bigint), 0) + from %I where %I ~ %L$f$, + r.col, r.tbl, r.col, '^' || r.prefix || '-[0-9]{8}-[0-9]+$') into v_max; + end if; + + continue when v_max = 0; -- 空表不动序列 + + execute format( + 'select case when is_called then last_value + 1 else last_value end from %s', + r.seq) into v_next; + + if v_next <= v_max then + -- is_called = true:下一次 nextval() 返回 v_max + 1 + execute format('select setval(%L, %s, true)', r.seq, v_max); + v_fixed := v_fixed + 1; + raise notice '[sync] % : next % -> %', r.seq, v_next, v_max + 1; + end if; + exception when others then + raise warning '[sync] skip % : %', r.seq, sqlerrm; + end; + end loop; + + ---------------------------------------------------------------------------- + -- 第二轮:自检,正常情况下不应再有落后项 + ---------------------------------------------------------------------------- + for r in select seq, tbl, col, prefix from seq_targets where seq is not null order by tbl, col + loop + begin + if r.prefix is null then + execute format('select coalesce(max(%I), 0) from %s', r.col, r.tbl) into v_max; + else + execute format( + $f$select coalesce(max((substring(%I from '([0-9]+)$'))::bigint), 0) + from %I where %I ~ %L$f$, + r.col, r.tbl, r.col, '^' || r.prefix || '-[0-9]{8}-[0-9]+$') into v_max; + end if; + + continue when v_max = 0; + + execute format( + 'select case when is_called then last_value + 1 else last_value end from %s', + r.seq) into v_next; + + if v_next <= v_max then + v_late := v_late + 1; + raise warning '[check] % 仍然落后:next = %,期望 >= %', r.seq, v_next, v_max + 1; + end if; + exception when others then + raise warning '[check] skip % : %', r.seq, sqlerrm; + end; + end loop; + + raise notice '序列同步完成:上调 % 个,剩余落后 % 个', v_fixed, v_late; +end +$$; + +commit; + +-- 参考:库里还有这些序列没有任何列或默认值引用(迁移残留,确认后可清理) +select s.relname as unused_sequence, + (select last_value from pg_sequences ps + where ps.schemaname = 'public' and ps.sequencename = s.relname) as last_value +from pg_class s +join pg_namespace n on n.oid = s.relnamespace +where s.relkind = 'S' + and n.nspname = 'public' + and not exists ( + select 1 from pg_attrdef d + where pg_get_expr(d.adbin, d.adrelid) like '%' || s.relname || '%') + and not exists ( + select 1 from seq_targets t + where t.seq = 'public.' || s.relname or t.seq = s.relname) + and s.relname not in ( + select (string_to_array(seq, '.'))[2] from seq_targets where seq is not null) +order by 1;