个人编制软件展示

PSI - Purchase Sale Inventory 进销存软件

PSI进销存系统云原生架构与容器化部署

云原生架构概述

云原生(Cloud Native)是一种构建和运行应用程序的方法,充分利用云计算的优势。PSI 进销存系统采用云原生架构,通过容器化、微服务、DevOps 等技术,实现高可用、易扩展、易维护的系统特性。本文介绍 PSI 系统的云原生架构设计和容器化部署方案。

系统架构设计

层次 组件 技术选型
接入层 API 网关、负载均衡 Nginx Ingress
服务层 业务微服务 Spring Boot / Node.js
数据层 数据库、缓存、消息队列 MySQL / Redis / Kafka
基础设施 容器编排、服务网格 Kubernetes / Istio

Docker 容器化

PSI 前后端服务的 Dockerfile:

# 前端服务 Dockerfile
FROM node:18-alpine AS builder

WORKDIR /app

# 安装依赖
COPY package*.json ./
RUN npm ci --registry=https://registry.npmmirror.com

# 复制源码
COPY . .

# 构建生产版本
RUN npm run build

# 生产镜像
FROM nginx:alpine

# 复制构建产物
COPY --from=builder /app/dist /usr/share/nginx/html

# 复制 Nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 暴露端口
EXPOSE 80

# 启动命令
CMD ["nginx", "-g", "daemon off;"]

# ---------

# 后端服务 Dockerfile
FROM openjdk:17-jdk-slim AS builder

WORKDIR /app

# 复制 Maven 依赖(利用缓存)
COPY pom.xml .
RUN mkdir -p /root/.m2 && cp -r /root/.m2 /root/.m2 || true

# 复制源码并构建
COPY src ./src
RUN ./mvnw clean package -DskipTests

# 生产镜像
FROM openjdk:17-jre-slim

WORKDIR /app

# 创建非 root 用户
RUN groupadd -r psiserver && useradd -r -g psiserver psiserver

# 复制构建产物
COPY --from=builder /app/target/*.jar app.jar

# 设置文件权限
RUN chown -R psiserver:psiserver /app

# 切换到非 root 用户
USER psiserver

# 暴露端口
EXPOSE 8080

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:8080/actuator/health || exit 1

# 启动命令
ENTRYPOINT ["java", "-jar", "app.jar"]

Kubernetes 部署配置

完整的 K8s 部署清单:

# Namespace
apiVersion: v1
kind: Namespace
metadata:
  name: psi-system

---
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: psi-config
  namespace: psi-system
data:
  DATABASE_HOST: "mysql.psi-system.svc.cluster.local"
  DATABASE_PORT: "3306"
  REDIS_HOST: "redis.psi-system.svc.cluster.local"
  REDIS_PORT: "6379"
  KAFKA_BOOTSTRAP_SERVERS: "kafka.psi-system.svc.cluster.local:9092"

---
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: psi-api
  namespace: psi-system
spec:
  replicas: 3
  selector:
    matchLabels:
      app: psi-api
  template:
    metadata:
      labels:
        app: psi-api
    spec:
      containers:
      - name: api
        image: psi/api:v2.5.0
        ports:
        - containerPort: 8080
        envFrom:
        - configMapRef:
            name: psi-config
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "2000m"
            memory: "2Gi"
        livenessProbe:
          httpGet:
            path: /actuator/health/liveness
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /actuator/health/readiness
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 5

---
# Service
apiVersion: v1
kind: Service
metadata:
  name: psi-api
  namespace: psi-system
spec:
  selector:
    app: psi-api
  ports:
  - port: 80
    targetPort: 8080
  type: ClusterIP

---
# HPA (自动扩缩容)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: psi-api-hpa
  namespace: psi-system
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: psi-api
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80

数据库高可用配置

MySQL 主从集群和 Redis 哨兵模式:

# MySQL 主从配置
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: psi-system
data:
  my.cnf: |
    [mysqld]
    # 主从配置
    server-id=1
    log-bin=mysql-bin
    binlog-format=ROW
    gtid-mode=ON
    enforce-gtid-consistency=ON

    # 性能优化
    innodb-buffer-pool-size=2G
    max-connections=500
    query-cache-type=1
    query-cache-size=64M

---
# MySQL StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: psi-system
spec:
  serviceName: mysql
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
        - name: config
          mountPath: /etc/mysql/conf.d
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: root-password
        resources:
          requests:
            cpu: "1000m"
            memory: "2Gi"
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "ssd"
      resources:
        requests:
          storage: 50Gi

---
# Redis 哨兵配置
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: psi-system
data:
  sentinel.conf: |
    port 26379
    sentinel monitor mymaster redis-master 6379 2
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 180000

---
# Redis StatefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: psi-system
spec:
  serviceName: redis
  replicas: 3
  template:
    spec:
      containers:
      - name: redis
        image: redis:7-alpine
        command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
        ports:
        - containerPort: 6379
        volumeMounts:
        - name: data
          mountPath: /data

服务网格与可观测性

使用 Istio 实现服务治理:

# Istio VirtualService - 流量管理
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: psi-api
  namespace: psi-system
spec:
  hosts:
  - psi-api
  http:
  - match:
    - headers:
        x-canary:
          exact: "true"
    route:
    - destination:
        host: psi-api
        subset: canary
      weight: 20
    - destination:
        host: psi-api
        subset: stable
      weight: 80
  - route:
    - destination:
        host: psi-api
        subset: stable

---
# Istio DestinationRule - 熔断配置
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: psi-api
  namespace: psi-system
spec:
  host: psi-api
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 100
      http:
        h2UpgradePolicy: UPGRADE
        http1MaxPendingRequests: 100
        http2MaxRequests: 1000
    outlierDetection:
      consecutive5xxErrors: 5
      interval: 30s
      baseEjectionTime: 30s

---
# Prometheus 监控配置
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: psi-api-monitor
  namespace: psi-system
spec:
  selector:
    matchLabels:
      app: psi-api
  endpoints:
  - port: metrics
    path: /actuator/prometheus

---
# Grafana 仪表盘 - 业务指标
apiVersion: v1
kind: ConfigMap
metadata:
  name: psi-grafana-dashboard
  namespace: monitoring
data:
  dashboard.json: |
    {
      "panels": [
        {
          "title": "订单处理量",
          "type": "graph",
          "datasource": "Prometheus",
          "targets": [
            {
              "expr": "rate(psi_orders_total[5m])"
            }
          ]
        },
        {
          "title": "库存查询响应时间",
          "type": "graph",
          "datasource": "Prometheus",
          "targets": [
            {
              "expr": "histogram_quantile(0.95, rate(psi_inventory_query_duration_seconds_bucket[5m]))"
            }
          ]
        }
      ]
    }

总结

PSI 进销存系统的云原生架构带来了显著优势:

通过云原生技术栈,PSI 系统可以更好地适应业务发展需求。

← 下一篇:PSI进销存系统人工智能与智能推荐