import os
import boto3
from mimetypes import guess_type
# MinIO 配置信息
MINIO_ENDPOINT = "http://xx:9000" # MinIO 地址
MINIO_ACCESS_KEY = "AT03z5LqjDlaJ4fjn" # MinIO Access Key
MINIO_SECRET_KEY = "jOjWgkDhOc3zC8RgRYLzm4wKP3q4rnR" # MinIO Secret Key
BUCKET_NAME = "lanyuancc" # MinIO 中的桶名
# 本地文件夹路径
LOCAL_FOLDER = r"D:\Backup\Documents\Downloads\蓝源产城图片" # 替换为本地文件夹路径
# 初始化 MinIO 客户端
s3_client = boto3.client(
's3',
endpoint_url=MINIO_ENDPOINT,
aws_access_key_id=MINIO_ACCESS_KEY,
aws_secret_access_key=MINIO_SECRET_KEY
)
def upload_to_minio(local_folder, bucket_name, target_directory=""):
"""
将本地文件夹中的文件上传到 MinIO,支持指定桶内的文件夹路径
:param local_folder: 本地文件夹路径
:param bucket_name: MinIO 中的桶名称
:param target_directory: MinIO 中目标文件夹路径(子目录,例如 "specific-directory")
"""
for root, dirs, files in os.walk(local_folder):
for file_name in files:
local_path = os.path.join(root, file_name) # 本地文件路径
relative_path = os.path.relpath(local_path, local_folder) # 相对路径
# 在 MinIO 中的目标路径,加入指定目录前缀
minio_path = os.path.join(target_directory, relative_path).replace("\\", "/")
mime_type, _ = guess_type(local_path) # 推测文件的 MIME 类型
try:
# 上传文件到 MinIO
s3_client.upload_file(
local_path,
bucket_name,
minio_path,
ExtraArgs={"ContentType": mime_type or "application/octet-stream"}
)
print(f"上传成功: {minio_path}")
except Exception as e:
print(f"上传失败: {minio_path},错误: {e}")
# 调用函数进行文件上传
if __name__ == "__main__":
# 修改 target_directory 为桶中希望的子目录路径
target_directory = "" # 示例子目录:上传到 BUCKET_NAME/specific-directory/
upload_to_minio(LOCAL_FOLDER, BUCKET_NAME, target_directory)
使用步骤
安装依赖模块
确保安装 mysql-connector-python:
pip install mysql-connector-python
确保 mysqldump 和 mysql 命令可用
此脚本依赖于系统中安装的 mysqldump 和 mysql 命令。请确保它们已安装并配置在系统路径中。
配置参数
替换脚本中的 host、user、password、original_db 和 new_db 变量,使用你自己的数据库连接信息和数据库名称。
运行脚本:
python clone_database.py
检查结果
脚本运行后,原始数据库的数据将被复制到一个新数据库中。
import mysql.connector
import subprocess
import os
def export_and_clone_databases(host, user, password, databases, suffix):
try:
# Step 1: 创建 MySQL 连接
connection = mysql.connector.connect(
host=host,
user=user,
password=password
)
cursor = connection.cursor()
for original_db in databases:
# Step 2: 检查原始数据库是否存在(使用反引号转义数据库名)
cursor.execute(f"SHOW DATABASES LIKE '{original_db}';")
if not cursor.fetchone():
print(f"Error: Database '{original_db}' does not exist.")
continue
# Step 3: 创建新数据库(用反引号转义数据库名)
new_db = f"{original_db}_{suffix}"
cursor.execute(f"CREATE DATABASE IF NOT EXISTS `{new_db}`;")
print(f"New database '{new_db}' created successfully.")
# Step 4: 使用 mysqldump 导出原数据库
dump_file = f"{original_db}.sql"
# dump_command = f"mysqldump -h {host} -u {user} -p{password} `{original_db}` > {dump_file}"
dump_command = f'"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe" -h {host} -u {user} -p{password} {original_db} > {dump_file}'
print(f"Executing: {dump_command}")
subprocess.run(dump_command, shell=True, check=True)
print(f"Database '{original_db}' exported to '{dump_file}'.")
# Step 5: 导入 SQL 文件到新数据库
# import_command = f"mysql -h {host} -u {user} -p{password} `{new_db}` < {dump_file}"
import_command = f'"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" -h {host} -u {user} -p{password} {new_db} < {dump_file}'
print(f"Executing: {import_command}")
subprocess.run(import_command, shell=True, check=True)
print(f"Data imported into new database '{new_db}' successfully.")
# Step 6: 删除临时文件(可选)
if os.path.exists(dump_file):
os.remove(dump_file)
print(f"Temporary file '{dump_file}' deleted.")
except mysql.connector.Error as err:
print(f"Error: {err}")
except subprocess.CalledProcessError as err:
print(f"Command failed: {err}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection closed.")
# 使用示例
if __name__ == "__main__":
host = "localhost" # 数据库地址
user = "root" # 数据库用户名
password = "xxx" # 数据库密码
# 定义需要复制的数据库列表
databases = [
"industry-xxx"
]
# 新数据库的后缀
suffix = "xxx" # 可更改为需要的后缀
export_and_clone_databases(host, user, password, databases, suffix)
先给脚本设置权限
chmod +x manage-services.sh
#!/bin/bash
# 基础配置
SRC_DIR=/data/HH-Cloud # 源码路径
WORK_DIR=/data/jar # 工作目录
LOG_DIR=/data/scripts/logs # 日志目录
# 服务脚本列表
SERVICE_SCRIPTS=(
"xx-gateway"
)
# Nacos、RocketMQ 和 Seata 启动和停止脚本
NACOS_START_CMD="bash /data/nacos/bin/startup.sh -m standalone"
NACOS_STOP_CMD="bash /data/nacos/bin/shutdown.sh"
ROCKETMQ_START_CMD="nohup sh /data/rocketmq-5.2.0/bin/mqnamesrv > /data/logs/rocketmq_namesrv.log 2>&1 &"
ROCKETMQ_BROKER_CMD="nohup sh /data/rocketmq-5.2.0/bin/mqbroker -n 127.0.0.1:9876 > /data/logs/rocketmq_broker.log 2>&1 &"
ROCKETMQ_STOP_CMD="sh /data/rocketmq-5.2.0/bin/mqshutdown namesrv && sh /data/rocketmq-5.2.0/bin/mqshutdown broker"
SEATA_START_CMD="bash /data/seata-server-1.6.0/bin/seata-server.sh"
SEATA_STOP_CMD="bash /data/seata-server-1.6.0/bin/seata-server.sh stop"
OPTIONS="-Xms512m -Xmx512m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
# 确保目录存在
mkdir -p $WORK_DIR
mkdir -p $LOG_DIR
# 启动单个服务
start_service() {
local SERVICE=$1
local JAR_PATH=""
local LOG_FILE=$LOG_DIR/$SERVICE.log
local PID_FILE=/var/run/$SERVICE.pid
echo "Starting $SERVICE..."
# 查找 JAR 文件
for DIR in "$SRC_DIR/hh-gateway/target" "$SRC_DIR/hh-modules/$SERVICE/target" "$SRC_DIR/hh-visual/$SERVICE/target" "$SRC_DIR/hh-auth/target"; do
if [ -f "$DIR/$SERVICE.jar" ]; then
JAR_PATH="$DIR/$SERVICE.jar"
break
fi
done
if [ -z "$JAR_PATH" ]; then
echo "Error: JAR file for $SERVICE not found!"
return
fi
# 检查是否已在运行
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
if kill -0 $PID 2>/dev/null; then
echo "$SERVICE is already running (PID: $PID)"
return
else
echo "Stale PID file found for $SERVICE. Removing it."
rm -f $PID_FILE
fi
fi
# 停止旧进程(如果未正确停止)
OLD_PID=$(ps -ef | grep "$SERVICE".jar | grep -v grep | awk '{print $2}')
if [ -n "$OLD_PID" ]; then
echo "Stopping old instance of $SERVICE (PID: $OLD_PID)..."
kill -9 $OLD_PID
echo "Old instance stopped."
fi
# 启动新服务
nohup java $OPTIONS -jar $JAR_PATH --spring.profiles.active=stage > $LOG_FILE 2>&1 &
NEW_PID=$!
echo $NEW_PID > $PID_FILE
echo "$SERVICE started (PID: $NEW_PID)"
}
# 停止单个服务
stop_service() {
local SERVICE=$1
local PID_FILE=/var/run/$SERVICE.pid
echo "Stopping $SERVICE..."
if [ -f $PID_FILE ]; then
PID=$(cat $PID_FILE)
if kill -0 $PID 2>/dev/null; then
kill -9 $PID
rm -f $PID_FILE
echo "$SERVICE stopped."
else
echo "PID $PID not valid. Removing stale PID file."
rm -f $PID_FILE
fi
else
echo "$SERVICE is not running."
fi
}
# 查询单个服务状态
status_service() {
local SERVICE=$1
local PID_FILE=/var/run/$SERVICE.pid
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2>/dev/null; then
echo "$SERVICE is running (PID: $(cat $PID_FILE))"
else
echo "$SERVICE is not running."
fi
}
# 启动 Nacos、RocketMQ、Seata 服务
start_nacos() {
echo "Starting Nacos..."
$NACOS_START_CMD &
echo "Nacos started."
}
start_rocketmq() {
echo "Starting RocketMQ..."
# 启动 NameServer
eval $ROCKETMQ_START_CMD
sleep 2 # 稍等一下,确保 NameServer 启动
# 启动 Broker
eval $ROCKETMQ_BROKER_CMD
sleep 2 # 稍等一下,确保 Broker 启动
# 使用 disown 完全将进程与终端分离
disown
echo "RocketMQ started. Logs are available in /data/logs/rocketmq_namesrv.log and /data/logs/rocketmq_broker.log"
}
start_seata() {
echo "Starting Seata..."
$SEATA_START_CMD
sleep 3 # 给 Seata 一些时间启动
echo "Seata started."
}
# 停止 Nacos、RocketMQ、Seata 服务
stop_nacos() {
echo "Stopping Nacos..."
$NACOS_STOP_CMD
echo "Nacos stopped."
}
stop_rocketmq() {
echo "Stopping RocketMQ..."
$ROCKETMQ_STOP_CMD
echo "RocketMQ stopped."
}
stop_seata() {
echo "Stopping Seata..."
$SEATA_STOP_CMD
echo "Seata stopped."
}
# 检查 Nacos 服务状态
status_nacos() {
if pgrep -f "nacos-server.jar" > /dev/null; then
echo "Nacos is running."
else
echo "Nacos is not running."
fi
}
# 检查 RocketMQ 服务状态
status_rocketmq() {
if pgrep -f "mqnamesrv" > /dev/null && pgrep -f "mqbroker" > /dev/null; then
echo "RocketMQ is running."
else
echo "RocketMQ is not running."
fi
}
# 检查 Seata 服务状态
status_seata() {
if pgrep -f "seata-server" > /dev/null; then
echo "Seata is running."
else
echo "Seata is not running."
fi
}
# 检查参数
if [ $# -lt 1 ]; then
echo "Usage: $0 <start|stop|status> [service_name]"
exit 1
fi
ACTION=$1
SERVICE=$2
# 根据参数执行操作
case $ACTION in
start)
if [ -z "$SERVICE" ]; then
# 启动所有服务
start_nacos
start_rocketmq
start_seata
for SERVICE in "${SERVICE_SCRIPTS[@]}"; do
start_service $SERVICE
sleep 2 # 确保每个服务有足够时间启动
done
echo "All services started."
else
# 启动指定服务
if [[ "$SERVICE" == "nacos" ]]; then
start_nacos
elif [[ "$SERVICE" == "rocketmq" ]]; then
start_rocketmq
elif [[ "$SERVICE" == "seata" ]]; then
start_seata
elif [[ " ${SERVICE_SCRIPTS[@]} " =~ " ${SERVICE} " ]]; then
start_service $SERVICE
else
echo "Invalid service name: $SERVICE"
exit 1
fi
fi
;;
stop)
if [ -z "$SERVICE" ]; then
# 停止所有服务
stop_nacos
stop_rocketmq
stop_seata
for SERVICE in "${SERVICE_SCRIPTS[@]}"; do
stop_service $SERVICE
done
echo "All services stopped."
else
# 停止指定服务
if [[ "$SERVICE" == "nacos" ]]; then
stop_nacos
elif [[ "$SERVICE" == "rocketmq" ]]; then
stop_rocketmq
elif [[ "$SERVICE" == "seata" ]]; then
stop_seata
elif [[ " ${SERVICE_SCRIPTS[@]} " =~ " ${SERVICE} " ]]; then
stop_service $SERVICE
else
echo "Invalid service name: $SERVICE"
exit 1
fi
fi
;;
status)
if [ -z "$SERVICE" ]; then
# 查询所有服务状态
echo "Checking Nacos status..."
status_nacos
echo "Checking RocketMQ status..."
status_rocketmq
echo "Checking Seata status..."
status_seata
for SERVICE in "${SERVICE_SCRIPTS[@]}"; do
status_service $SERVICE
done
else
# 查询指定服务状态
if [[ "$SERVICE" == "nacos" ]]; then
status_nacos
elif [[ "$SERVICE" == "rocketmq" ]]; then
status_rocketmq
elif [[ "$SERVICE" == "seata" ]]; then
status_seata
elif [[ " ${SERVICE_SCRIPTS[@]} " =~ " ${SERVICE} " ]]; then
status_service $SERVICE
else
echo "Invalid service name: $SERVICE"
exit 1
fi
fi
;;
*)
echo "Invalid action: $ACTION. Use 'start', 'stop', or 'status'."
exit 1
;;
esac
微信小程序和H5端对接dify ai接口
用的uniapp开发的
H5端是基于SSE协议开发的
要安装
import {
fetchEventSource
} from '@microsoft/fetch-event-source';
/*#ifdef H5*/
const eventSource = new fetchEventSource('/v1/chat-messages', {
method: 'POST',
headers: {
'Authorization': 'Bearer app-xxx',
'Content-Type': 'application/json',
},
body: JSON.stringify({
inputs: {},
query: message, // 替换为实际的查询内容
response_mode: 'streaming', // 启用流式响应
conversation_id: that.conversationId,
user: "abc-123"
}),
onopen(response) {
// 建立连接
console.log(response, "open");
if (response.ok) {
console.log("成功建立连接");
} else {
throw new Error(JSON.stringify(response));
}
},
onmessage(event) {
try {
const jsonData = JSON.parse(event.data);
if (jsonData.event === "message") {
//that.loading = false; // 隐藏加载图标
that.conversationId = jsonData.conversation_id;
// 如果返回的答案中有新的文字,逐字显示
if (jsonData.answer) {
// 追加接收到的新文字
that.displayedText += jsonData.answer;
// 继续显示新的文字
//that.startTyping(jsonData.message_id);
}
}
console.log("that.displayedText", that.displayedText);
if (jsonData.event === "message_end") {
that.loading = false; // 隐藏加载图标
that.arr.push({
answer: that.displayedText,
message_id: jsonData.message_id,
});
that.displayedText = "";
console.log("that.arr", that.arr);
}
} catch (error) {
console.error("数据解析错误:", error);
}
},
onclose() {
console.log("关闭链接");
},
onerror(err) {
console.error("发生错误", err);
throw err;
},
openWhenHidden: true, // 实时通知
});
/*#endif*/
/*#ifdef MP-WEIXIN*/
// 微信小程序端执行的逻辑
const requestTask = uni.request({
url: '/v1/chat-messages',
timeout: 15000,
responseType: 'text',
method: 'POST',
header: {
'Authorization': 'Bearer app-xxx',
'Content-Type': 'application/json',
},
enableChunked: true, //配置这里
data: JSON.stringify({
inputs: {},
query: message, // 替换为实际的查询内容
response_mode: 'streaming', // 启用流式响应
conversation_id: that.conversationId,
user: "abc-123"
}),
success: response => {
console.log(response)
},
fail: error => {}
})
requestTask.onHeadersReceived(function(res) {
console.log(res.header);
});
// 这里监听消息
requestTask.onChunkReceived(function(res) {
let decoder = new TextDecoder('utf-8')
let text = decoder.decode(new Uint8Array(res.data))
console.log(text)
try {
// 使用正则表达式去除前缀 "data: "
const jsonString = text.replace(/^data:\s*/, '');
const jsonData = JSON.parse(jsonString);
if (jsonData.event === "message") {
//that.loading = false; // 隐藏加载图标
that.conversationId = jsonData.conversation_id;
// 如果返回的答案中有新的文字,逐字显示
if (jsonData.answer) {
// 追加接收到的新文字
that.displayedText += jsonData.answer;
// 继续显示新的文字
//that.startTyping(jsonData.message_id);
}
}
console.log("that.displayedText", that.displayedText);
if (jsonData.event === "message_end") {
that.loading = false; // 隐藏加载图标
that.arr.push({
answer: that.displayedText,
message_id: jsonData.message_id,
});
that.displayedText = "";
console.log("that.arr", that.arr);
}
} catch (error) {
console.error("数据解析错误:", error);
}
})
/*#endif*/
目录
- 拉取镜像
- 创建挂载点目录
- 创建minio容器
- 启动minio容器
测试minio是否安装成功
minio官网:MinIO | High Performance, Kubernetes Native Object Storage
拉取镜像
代码:
docker pull minio/minio
实例:
[root@localhost howlong]# docker pull minio/minio Using default tag: latest latest: Pulling from minio/minio 5329d7039f25: Pull complete 2b3707fd3b24: Pull complete 5206d1e7147f: Pull complete 33feab5fa1c5: Pull complete 5cd9e0eb62e5: Pull complete 34f92c74bdaf: Pull complete Digest: sha256:15d762671436cf383f9cc6667260e6c1298c25c8d7009576f709c3823e4a494d Status: Downloaded newer image for minio/minio:latest docker.io/minio/minio:latest [root@localhost howlong]#
创建挂载点目录
代码:
mkdir -p /usr/local/minio/config mkdir -p /usr/local/minio/data
创建minio容器
注意:这里设置了minio客户端的账号和密码,其密码不可少于8位 账号:admin 密码:admin123
代码:
docker run -d \ --name minio \ -p 9000:9000 \ -p 9001:9001 \ --privileged=true \ -e "MINIO_ROOT_USER=admin" \ -e "MINIO_ROOT_PASSWORD=admin123" \ -v /usr/local/minion/data:/data \ -v /usr/local/minio/config:/root/.minio \ minio/minio server \ --console-address ":9000" \ --address ":9001" /data
实例:
[root@localhost howlong]# docker run -d \ --name minio \ -p 9000:9000 \ -p 9001:9001 \ --privileged=true \ -e "MINIO_ROOT_USER=admin" \ -e "MINIO_ROOT_PASSWORD=admin123" \ -v /usr/local/minion/data:/data \ -v /usr/local/minio/config:/root/.minio \ minio/minio server \ --console-address ":9000" \ --address ":9001" /data 93c98998d5cbc51c6f208cfe676adf495d4931469aad1d9756e19dca3fd4b66a
启动minio容器
代码:
docker start minio
实例:
[root@localhost howlong]# docker start minio minio [root@localhost howlong]#
测试minio是否安装成功
访问虚拟机地址+端口号,前面配置minio 的端口号为:9000
例如:
账号:admin 密码:admin123