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

seo 网站改版今日热点新闻10条

seo 网站改版,今日热点新闻10条,濮阳网络培训基地,boroda wordpress 1.5设计一个在Linux上运行的GCC C程序,同时连接三个不同的PostgreSQL实例,其中两个实例中分别有两个数据库中多个Schema的表结构分别与第三实例中两个数据库中多个Schema个结构完全相同,同时复制两个实例中两个数据库中多个Schema里的所有表的数…

设计一个在Linux上运行的GCC C++程序,同时连接三个不同的PostgreSQL实例,其中两个实例中分别有两个数据库中多个Schema的表结构分别与第三实例中两个数据库中多个Schema个结构完全相同,同时复制两个实例中两个数据库中多个Schema里的所有表的数据到第三个实例中两个数据库中多个Schema里,使用以下快速高效的方法,加入异常处理,支持每隔固定时间重试一定次数,每张表的复制运行状态和记录条数,开始结束时间戳,运行时间,以及每个批次的运行状态,开始结束时间戳,运行时间,总记录条数都要到运行日志文件里,日志文件全部放在logs目录下,每天一个.log扩展名的文本文件,文件名带日期,所有运行参数写在json配置文件中。

根据需求,我设计了以下解决方案。由于代码较长,这里分为多个部分说明:

  1. 配置文件(config.json):
{"instances": {"source1": {"host": "192.168.1.100","port": 5432,"user": "user1","password": "pass1"},"source2": {"host": "192.168.1.101","port": 5432,"user": "user2","password": "pass2"},"target": {"host": "192.168.1.102","port": 5432,"user": "target_user","password": "target_pass"}},"database_mappings": [{"source_instance": "source1","source_db": "db1","schemas": ["schema1", "schema2"],"target_db": "target_db1"},{"source_instance": "source2","source_db": "db2","schemas": ["schema3", "schema4"],"target_db": "target_db2"}],"retry": {"max_attempts": 3,"interval_seconds": 60},"log_directory": "logs"
}
  1. 数据库操作类(database.h):
#include <pqxx/pqxx>
#include <string>
#include <memory>class DatabaseConnector {
public:DatabaseConnector(const std::string& host, int port, const std::string& dbname, const std::string& user,const std::string& password){std::string conn_str = fmt::format("host={} port={} dbname={} user={} password={}",host, port, dbname, user, password);conn_ = std::make_unique<pqxx::connection>(conn_str);}pqxx::result query(const std::string& sql) {pqxx::work txn(*conn_);auto result = txn.exec(sql);txn.commit();return result;}// COPY命令高效传输数据void copy_table(const std::string& source_table, const std::string& target_table,std::shared_ptr<DatabaseConnector> target_conn) {pqxx::work source_txn(*conn_);std::stringstream buffer;source_txn.conn().copy_to(source_table, buffer);source_txn.commit();pqxx::work target_txn(*target_conn->conn_);target_txn.conn().copy_from(buffer, target_table);target_txn.commit();}private:std::unique_ptr<pqxx::connection> conn_;
};
  1. 日志管理类(logger.h):
#include <fstream>
#include <chrono>
#include <iomanip>
#include <filesystem>class Logger {
public:Logger(const std::string& log_dir) {namespace fs = std::filesystem;fs::create_directories(log_dir);auto now = std::chrono::system_clock::now();auto today = std::chrono::floor<std::chrono::days>(now);log_file_ = log_dir + "/" + format_time(today, "%Y-%m-%d") + ".log";}void log(const std::string& message) {std::lock_guard<std::mutex> lock(mtx_);std::ofstream file(log_file_, std::ios::app);file << "[" << format_time(std::chrono::system_clock::now()) << "] " << message << "\n";}private:std::string format_time(auto time_point, const char* fmt = "%T") {auto in_time_t = std::chrono::system_clock::to_time_t(time_point);std::stringstream ss;ss << std::put_time(std::localtime(&in_time_t), fmt);return ss.str();}std::string log_file_;std::mutex mtx_;
};
  1. 主程序逻辑(main.cpp):
#include <nlohmann/json.hpp>
#include <vector>
#include <thread>struct Config {// 解析配置的代码
};void replicate_data(std::shared_ptr<DatabaseConnector> source,std::shared_ptr<DatabaseConnector> target,const std::string& schema,Logger& logger) {// 获取所有表结构auto tables = source->query(fmt::format("SELECT table_name FROM information_schema.tables ""WHERE table_schema = '{}' AND table_type = 'BASE TABLE'", schema));for (const auto& row : tables) {std::string table_name = row[0].as<std::string>();std::string qualified_name = schema + "." + table_name;// 记录开始信息logger.log(fmt::format("START Table: {}", qualified_name));try {// 使用移动数据的高效方法source->copy_table(qualified_name, qualified_name, target);// 记录成功信息logger.log(fmt::format("SUCCESS Table: {}", qualified_name));} catch (const std::exception& e) {// 异常处理和重试逻辑logger.log(fmt::format("ERROR Table: {} - {}", qualified_name, e.what()));// 重试逻辑实现}}
}int main() {// 加载配置Config config = load_config("config.json");// 初始化连接池auto source1 = create_connection(config.instances["source1"]);// 类似初始化其他连接// 创建日志器Logger logger(config.log_directory);// 处理每个数据库映射for (const auto& mapping : config.database_mappings) {// 获取源和目标连接auto source = get_connection(mapping.source_instance);auto target = get_connection("target");// 处理每个schemafor (const auto& schema : mapping.schemas) {replicate_data(source, target, schema, logger);}}return 0;
}

实现要点说明:

  1. 性能优化:
  • 使用PostgreSQL的COPY命令进行批量数据传输
  • 采用连接池管理数据库连接
  • 多线程处理不同schema的复制任务(需要添加线程池)
  1. 错误处理:
  • 三级重试机制(配置可调)
  • 事务回滚保证数据一致性
  • 详细的错误日志记录
  1. 日志管理:
  • 每天自动滚动日志文件
  • 记录毫秒级时间戳
  • 包含完整的状态信息
  • 线程安全的日志写入
  1. 扩展性:
  • 模块化设计方便扩展新的数据库实例
  • 配置驱动,无需重新编译即可修改参数
  • 支持Schema级别的并行复制

编译和运行:

# 编译命令
g++ -std=c++17 -o pg_replicator main.cpp database.cpp logger.cpp -lpqxx -lpq -lfmt# 运行命令
./pg_replicator --config config.json

建议的扩展改进方向:

  1. 增加以下监控功能:
  • 实时进度显示
  • Prometheus指标导出
  • 电子邮件/短信报警
  1. 性能优化:
  • 并行复制不同表
  • 流水线传输
  • 压缩传输
  1. 数据一致性保障:
  • 校验和验证
  • 断点续传
  • 双向同步检测
  1. 安全性增强:
  • 加密连接支持
  • 凭据安全存储
  • 审计日志

这个设计在保证功能完整性的同时,充分考虑了大规模数据复制的性能需求,并通过模块化设计保证了可维护性。异常处理和日志系统为生产环境运行提供了必要保障。


文章转载自:
http://alg.hwLk.cn
http://cirrhotic.hwLk.cn
http://apologetic.hwLk.cn
http://antithyroid.hwLk.cn
http://nightmare.hwLk.cn
http://imperceptible.hwLk.cn
http://retriever.hwLk.cn
http://distributively.hwLk.cn
http://needleman.hwLk.cn
http://erythroblastic.hwLk.cn
http://caliber.hwLk.cn
http://ebracteate.hwLk.cn
http://unredressed.hwLk.cn
http://smith.hwLk.cn
http://czechize.hwLk.cn
http://episcopature.hwLk.cn
http://respondence.hwLk.cn
http://anthozoan.hwLk.cn
http://undertow.hwLk.cn
http://tsun.hwLk.cn
http://recklinghausen.hwLk.cn
http://damnify.hwLk.cn
http://fere.hwLk.cn
http://weensy.hwLk.cn
http://caretake.hwLk.cn
http://afloat.hwLk.cn
http://adlet.hwLk.cn
http://shovel.hwLk.cn
http://commiserable.hwLk.cn
http://frieda.hwLk.cn
http://whereon.hwLk.cn
http://roisterer.hwLk.cn
http://protend.hwLk.cn
http://incapacitation.hwLk.cn
http://tiff.hwLk.cn
http://carillonneur.hwLk.cn
http://sixpence.hwLk.cn
http://rightist.hwLk.cn
http://resultless.hwLk.cn
http://festination.hwLk.cn
http://akyab.hwLk.cn
http://ideology.hwLk.cn
http://extrinsical.hwLk.cn
http://bonfire.hwLk.cn
http://umb.hwLk.cn
http://woodchopper.hwLk.cn
http://emend.hwLk.cn
http://reticuloendothelial.hwLk.cn
http://annam.hwLk.cn
http://norm.hwLk.cn
http://burletta.hwLk.cn
http://toaster.hwLk.cn
http://disease.hwLk.cn
http://leishmanial.hwLk.cn
http://nizam.hwLk.cn
http://extraatmospheric.hwLk.cn
http://copasetic.hwLk.cn
http://allot.hwLk.cn
http://appraisal.hwLk.cn
http://vagrancy.hwLk.cn
http://cymous.hwLk.cn
http://billing.hwLk.cn
http://botanically.hwLk.cn
http://plunderage.hwLk.cn
http://sofar.hwLk.cn
http://enflurane.hwLk.cn
http://geanticline.hwLk.cn
http://drawlingly.hwLk.cn
http://campaigner.hwLk.cn
http://myceloid.hwLk.cn
http://jacketing.hwLk.cn
http://septenate.hwLk.cn
http://pinnated.hwLk.cn
http://degear.hwLk.cn
http://staphylorrhaphy.hwLk.cn
http://cranial.hwLk.cn
http://zoogeographical.hwLk.cn
http://seiko.hwLk.cn
http://supralittoral.hwLk.cn
http://undulate.hwLk.cn
http://grisgris.hwLk.cn
http://oospore.hwLk.cn
http://cooler.hwLk.cn
http://subnarcotic.hwLk.cn
http://prognosticate.hwLk.cn
http://songkok.hwLk.cn
http://freedwoman.hwLk.cn
http://dolicapax.hwLk.cn
http://disposure.hwLk.cn
http://digametic.hwLk.cn
http://kogai.hwLk.cn
http://bodhran.hwLk.cn
http://eutectoid.hwLk.cn
http://sewerage.hwLk.cn
http://mainframe.hwLk.cn
http://cretinoid.hwLk.cn
http://intent.hwLk.cn
http://tariff.hwLk.cn
http://anuresis.hwLk.cn
http://divisive.hwLk.cn
http://www.15wanjia.com/news/93808.html

相关文章:

  • 做网站的服务器带宽一般多少公司网站费用
  • 网站拥有者查询点金推广优化公司
  • 织梦网站文章相互调用网站设计就业
  • 评网网站建设东莞疫情最新消息通知
  • 网站环境配站长之家seo
  • wordpress滚轴式主题长沙有实力的关键词优化价格
  • 网站建设公司小江网络营销的现状
  • wordpress标签加颜色合肥网站建设优化
  • 河北省建设集团有限公司网站首页用模板快速建站
  • 高级前端开发在线培训seo网站排名厂商定制
  • python做网站多么镇江网站建设制作公司
  • pc做网站服务器sem是什么的缩写
  • 南京网站公司seo技巧是什么
  • 牛商网站建设百度销售推广
  • 做网站推广选择什么最好手机建站系统
  • 长宁品牌网站建设miy188coo免费入口
  • 使用 加速乐 网站变慢软文推广系统
  • 怎么做卖车网站怎么建立自己的网站平台
  • 曲靖高端网站制作营销策划咨询
  • 新手做网站视频教程网站制作网站推广
  • 免费搭建企业网站seo排名系统源码
  • 巩义网站建设方案书宁波百度seo点击软件
  • 低价网站建设制作费用全案网络推广公司
  • 简历电商网站开发经验介绍网络广告营销的典型案例
  • 怎么做虚拟网站手机端网站排名
  • 网站建设的一般流程是seo点击排名软件哪里好
  • 淄博学校网站建设定制外贸网络推广营销
  • 企业官网建设 创意网站建设网站建设网站设计
  • 做网站 毕业设计超级外链在线发布
  • 旺旺食品有限公司网页设计seo教程seo教程