【云备份|| 日志 day3】服务端配置信息模块

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 【云备份|| 日志 day3】服务端配置信息模块

云备份day3


使用文件配置加载一些程序的运行关键信息可以让程序的运行更加灵活,且当需要修改部分内容时,不需要在代码上修改,只需要修改配置文件,然后重启服务器即可。

配置信息

热点判断时间

文件下载URL前缀路径

压缩包后缀名称

上传文件存放路径

压缩文件存放路径

服务端备份信息存放文件

服务器访问 IP 地址

服务器访问端口

采用json 格式将配置信息存放在Cloud.conf中,当启动服务器时,由服务器从.conf文件中读取关键数据。

Cloud.conf

{

“hot_time” : 30,

“server_port” : 9090,

“server_ip” : “1.1.1.1”,

“url_prefix” : “/download/”,

“arc_suffix” : “.lz”,

“pack_dir” : “./packdir/”,

“back_dir” : “./backdir/”,

“manager_file” : “./back.dat”

}

读取配置信息:

在服务器启动时,需要其自动化去加载数据,采取单例模式,在服务器启动时一起进行初始化。(懒汉模式)

#define CONFIG_FILE "./cloud.conf"
class Config{
private:
time_t _hot_time;
int _server_port;
std::string _server_ip;
std::string _url_prefix;
std::string _arc_suffix;
std::string _pack_dir;
std::string _back_dir;
std::string _manager_file;//备份信息的配置管理
private:
static std::mutex _mutex;
static Config *_instance;
Config();
public:
bool ReadConfig(const std::string &filename);
int GetHotTime();
int GetServerPort();
std::string GetServerIp();
std::string GetURLPrefix();
std::string GetArcSuffix();
std::string GetPackDir();
std::string GetBackDir();
std::string GetManagerFile();
public:
   static Config *GetInstance();
};

代码:

#define CONFIG_FILE "./cloud.conf"
  class Config{
    private:
      Config(){
        ReadConfigFile();
      }
      static Config *_instance;
      static std::mutex _mutex;
    private:
      int _hot_time;
      int _server_port;
      std::string _server_ip;
      std::string _download_prefix;
      std::string _packfile_suffix;
      std::string _pack_dir;
      std::string _back_dir;
      std::string _backup_file;
      bool ReadConfigFile() {
        FileUtil fu(CONFIG_FILE);
        std::string body;
        if(fu.GetContent(&body) == false){
          std::cout << "load config file failed!\n";
          return false;
        }
        Json::Value root;
        if (JsonUtil::UnSerialize(body, &root) == false){
          std::cout << "parse config file failed!\n";
          return false;
        }
        _hot_time = root["hot_time"].asInt();
        _server_port = root["server_port"].asInt();
        _server_ip = root["server_ip"].asString();
        _download_prefix = root["download_prefix"].asString();
        _packfile_suffix = root["packfile_suffix"].asString();
        _pack_dir = root["pack_dir"].asString();
        _back_dir = root["back_dir"].asString();
        _backup_file = root["backup_file"].asString();
        return true;
      }
    public:
      static Config *GetInstance() {
        if (_instance == NULL){
          _mutex.lock();
          if (_instance == NULL) {
            _instance = new Config();
          }
          _mutex.unlock();
        }
        return _instance;
      }
      int GetHotTime() {
        return _hot_time;
      }
      int GetServerPort() {
        return _server_port;
      }
      std::string GetServerIp() {
        return _server_ip;
      }
      std::string GetDownloadPrefix() {
        return _download_prefix;
      }
      std::string GetPackFileSuffix() {
        return _packfile_suffix;
      }
      std::string GetPackDir() {
        return _pack_dir;
      }
      std::string GetBackDir() {
        return _back_dir;
      }
      std::string GetBackupFile() {
        return _backup_file;
      }
  };
  Config *Config::_instance = NULL;
  std::mutex Config::_mutex;



相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
9天前
|
监控 Java API
如何将不同业务模块产生的日志 分多文件记录
如何将不同业务模块产生的日志 分多文件记录
10 0
|
6天前
|
SQL 监控 Java
在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
这篇文章介绍了如何在IDEA和Spring Boot中使用AOP技术实现日志信息的记录到数据库的详细步骤和代码示例。
在IDEA 、springboot中使用切面aop实现日志信息的记录到数据库
|
5天前
|
存储 Ubuntu Apache
如何在 Ubuntu VPS 上配置 Apache 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Apache 的日志记录和日志轮转
16 6
|
5天前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
11 4
|
7天前
|
存储 监控 Java
|
5天前
|
XML Java Maven
logback在springBoot项目中的使用 springboot中使用日志进行持久化保存日志信息
这篇文章详细介绍了如何在Spring Boot项目中使用logback进行日志记录,包括Maven依赖配置、logback配置文件的编写,以及实现的日志持久化和控制台输出效果。
logback在springBoot项目中的使用 springboot中使用日志进行持久化保存日志信息
|
12天前
|
存储 安全 Python
[python]使用标准库logging实现多进程安全的日志模块
[python]使用标准库logging实现多进程安全的日志模块
|
20天前
|
开发框架 NoSQL 前端开发
在Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息
在Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息
|
21天前
|
NoSQL Serverless PHP
遇到报错但没有日志信息的情况,该如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
27天前
|
存储 算法 开发工具
Etcd/Raft 原理问题之Etcd-Raft是什么
Etcd/Raft 原理问题之Etcd-Raft是什么

热门文章

最新文章