Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
|
eacf72c9d7 | 2 months ago |
|
8a8c117acc | 2 months ago |
@ -0,0 +1,302 @@
|
||||
/*
|
||||
Copyright (c) 2022 Sogou, Inc.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
You may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
Authors: Wang Zhenpeng (wangzhenpeng@sogou-inc.com)
|
||||
*/
|
||||
|
||||
#ifndef _CONSULDATATYPES_H_
|
||||
#define _CONSULDATATYPES_H_
|
||||
|
||||
#include <assert.h>
|
||||
#include <atomic>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace protocol
|
||||
{
|
||||
|
||||
/**
|
||||
* @class ConsulConfig
|
||||
* @brief 配置类,用于管理 Consul 的各种设置。
|
||||
*/
|
||||
class ConsulConfig
|
||||
{
|
||||
public:
|
||||
// === Common Config ===
|
||||
// 设置和获取 token 配置,用于鉴权
|
||||
void set_token(const std::string& token) { this->ptr->token = token; }
|
||||
std::string get_token() const { return this->ptr->token; }
|
||||
|
||||
// === Discover Config ===
|
||||
// 设置和获取数据中心名称
|
||||
void set_datacenter(const std::string& data_center) { this->ptr->dc = data_center; }
|
||||
std::string get_datacenter() const { return this->ptr->dc; }
|
||||
|
||||
// 设置和获取指定的邻近节点
|
||||
void set_near_node(const std::string& near_node) { this->ptr->near = near_node; }
|
||||
std::string get_near_node() const { return this->ptr->near; }
|
||||
|
||||
// 设置和获取过滤表达式
|
||||
void set_filter_expr(const std::string& filter_expr) { this->ptr->filter = filter_expr; }
|
||||
std::string get_filter_expr() const { return this->ptr->filter; }
|
||||
|
||||
// 设置和获取阻塞查询的等待时间(单位:毫秒)
|
||||
void set_wait_ttl(int wait_ttl) { this->ptr->wait_ttl = wait_ttl; }
|
||||
int get_wait_ttl() const { return this->ptr->wait_ttl; }
|
||||
|
||||
// 启用或禁用阻塞查询
|
||||
void set_blocking_query(bool enable_flag) { this->ptr->blocking_query = enable_flag; }
|
||||
bool blocking_query() const { return this->ptr->blocking_query; }
|
||||
|
||||
// 设置和获取仅获取健康通过状态的服务实例
|
||||
void set_passing(bool passing) { this->ptr->passing = passing; }
|
||||
bool get_passing() const { return this->ptr->passing; }
|
||||
|
||||
// === Register Config ===
|
||||
// 设置和获取是否替换现有检查
|
||||
void set_replace_checks(bool replace_checks) { this->ptr->replace_checks = replace_checks; }
|
||||
bool get_replace_checks() const { return this->ptr->replace_checks; }
|
||||
|
||||
// 设置和获取健康检查的名称
|
||||
void set_check_name(const std::string& check_name) { this->ptr->check_cfg.check_name = check_name; }
|
||||
std::string get_check_name() const { return this->ptr->check_cfg.check_name; }
|
||||
|
||||
// 设置和获取 HTTP 检查 URL
|
||||
void set_check_http_url(const std::string& http_url) { this->ptr->check_cfg.http_url = http_url; }
|
||||
std::string get_check_http_url() const { return this->ptr->check_cfg.http_url; }
|
||||
|
||||
// 设置和获取 HTTP 检查方法
|
||||
void set_check_http_method(const std::string& method) { this->ptr->check_cfg.http_method = method; }
|
||||
std::string get_check_http_method() const { return this->ptr->check_cfg.http_method; }
|
||||
|
||||
// 添加 HTTP 请求头
|
||||
void add_http_header(const std::string& key, const std::vector<std::string>& values)
|
||||
{
|
||||
this->ptr->check_cfg.headers.emplace(key, values);
|
||||
}
|
||||
const std::map<std::string, std::vector<std::string>>* get_http_headers() const
|
||||
{
|
||||
return &this->ptr->check_cfg.headers;
|
||||
}
|
||||
|
||||
// 设置和获取 HTTP 检查请求体
|
||||
void set_http_body(const std::string& body) { this->ptr->check_cfg.http_body = body; }
|
||||
std::string get_http_body() const { return this->ptr->check_cfg.http_body; }
|
||||
|
||||
// 设置和获取检查的时间间隔
|
||||
void set_check_interval(int interval) { this->ptr->check_cfg.interval = interval; }
|
||||
int get_check_interval() const { return this->ptr->check_cfg.interval; }
|
||||
|
||||
// 设置和获取检查超时时间
|
||||
void set_check_timeout(int timeout) { this->ptr->check_cfg.timeout = timeout; }
|
||||
int get_check_timeout() const { return this->ptr->check_cfg.timeout; }
|
||||
|
||||
// 设置和获取健康检查的备注
|
||||
void set_check_notes(const std::string& notes) { this->ptr->check_cfg.notes = notes; }
|
||||
std::string get_check_notes() const { return this->ptr->check_cfg.notes; }
|
||||
|
||||
// 设置和获取 TCP 地址
|
||||
void set_check_tcp(const std::string& tcp_address) { this->ptr->check_cfg.tcp_address = tcp_address; }
|
||||
std::string get_check_tcp() const { return this->ptr->check_cfg.tcp_address; }
|
||||
|
||||
// 设置和获取初始状态
|
||||
void set_initial_status(const std::string& initial_status) { this->ptr->check_cfg.initial_status = initial_status; }
|
||||
std::string get_initial_status() const { return this->ptr->check_cfg.initial_status; }
|
||||
|
||||
// 设置和获取自动注销时间
|
||||
void set_auto_deregister_time(int milliseconds)
|
||||
{
|
||||
this->ptr->check_cfg.auto_deregister_time = milliseconds;
|
||||
}
|
||||
int get_auto_deregister_time() const { return this->ptr->check_cfg.auto_deregister_time; }
|
||||
|
||||
// 设置和获取通过前的成功次数
|
||||
void set_success_times(int times) { this->ptr->check_cfg.success_times = times; }
|
||||
int get_success_times() const { return this->ptr->check_cfg.success_times; }
|
||||
|
||||
// 设置和获取进入临界状态前的失败次数
|
||||
void set_failure_times(int times) { this->ptr->check_cfg.failure_times = times; }
|
||||
int get_failure_times() const { return this->ptr->check_cfg.failure_times; }
|
||||
|
||||
// 启用或禁用健康检查
|
||||
void set_health_check(bool enable_flag) { this->ptr->check_cfg.health_check = enable_flag; }
|
||||
bool get_health_check() const { return this->ptr->check_cfg.health_check; }
|
||||
|
||||
public:
|
||||
// 构造函数,初始化默认配置
|
||||
ConsulConfig()
|
||||
{
|
||||
this->ptr = new Config;
|
||||
this->ptr->blocking_query = false;
|
||||
this->ptr->passing = false;
|
||||
this->ptr->replace_checks = false;
|
||||
this->ptr->wait_ttl = 300 * 1000;
|
||||
this->ptr->check_cfg.interval = 5000;
|
||||
this->ptr->check_cfg.timeout = 10000;
|
||||
this->ptr->check_cfg.http_method = "GET";
|
||||
this->ptr->check_cfg.initial_status = "critical";
|
||||
this->ptr->check_cfg.auto_deregister_time = 10 * 60 * 1000;
|
||||
this->ptr->check_cfg.success_times = 0;
|
||||
this->ptr->check_cfg.failure_times = 0;
|
||||
this->ptr->check_cfg.health_check = false;
|
||||
|
||||
this->ref = new std::atomic<int>(1);
|
||||
}
|
||||
|
||||
// 析构函数,释放内存
|
||||
virtual ~ConsulConfig()
|
||||
{
|
||||
if (--*this->ref == 0)
|
||||
{
|
||||
delete this->ptr;
|
||||
delete this->ref;
|
||||
}
|
||||
}
|
||||
|
||||
// 移动构造函数
|
||||
ConsulConfig(ConsulConfig&& move)
|
||||
{
|
||||
this->ptr = move.ptr;
|
||||
this->ref = move.ref;
|
||||
move.ptr = new Config;
|
||||
move.ref = new std::atomic<int>(1);
|
||||
}
|
||||
|
||||
// 拷贝构造函数
|
||||
ConsulConfig(const ConsulConfig& copy)
|
||||
{
|
||||
this->ptr = copy.ptr;
|
||||
this->ref = copy.ref;
|
||||
++(*this->ref);
|
||||
}
|
||||
|
||||
// 移动赋值运算符
|
||||
ConsulConfig& operator= (ConsulConfig&& move)
|
||||
{
|
||||
if (this != &move)
|
||||
{
|
||||
this->~ConsulConfig();
|
||||
this->ptr = move.ptr;
|
||||
this->ref = move.ref;
|
||||
move.ptr = new Config;
|
||||
move.ref = new std::atomic<int>(1);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// 拷贝赋值运算符
|
||||
ConsulConfig& operator= (const ConsulConfig& copy)
|
||||
{
|
||||
if (this != ©)
|
||||
{
|
||||
this->~ConsulConfig();
|
||||
this->ptr = copy.ptr;
|
||||
this->ref = copy.ref;
|
||||
++(*this->ref);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
// 健康检查配置结构体
|
||||
struct HealthCheckConfig
|
||||
{
|
||||
std::string check_name; // 检查名称
|
||||
std::string notes; // 备注
|
||||
std::string http_url; // HTTP URL
|
||||
std::string http_method; // HTTP 方法
|
||||
std::string http_body; // HTTP 请求体
|
||||
std::string tcp_address; // TCP 地址
|
||||
std::string initial_status; // 初始状态
|
||||
std::map<std::string, std::vector<std::string>> headers; // 请求头
|
||||
int auto_deregister_time; // 自动注销时间
|
||||
int interval; // 时间间隔
|
||||
int timeout; // 超时时间
|
||||
int success_times; // 成功次数
|
||||
int failure_times; // 失败次数
|
||||
bool health_check; // 是否启用健康检查
|
||||
};
|
||||
|
||||
// 配置结构体
|
||||
struct Config
|
||||
{
|
||||
// 通用配置
|
||||
std::string token;
|
||||
|
||||
// 服务发现配置
|
||||
std::string dc; // 数据中心
|
||||
std::string near; // 邻近节点
|
||||
std::string filter; // 过滤表达式
|
||||
int wait_ttl; // 等待时间
|
||||
bool blocking_query; // 阻塞查询
|
||||
bool passing; // 健康状态
|
||||
|
||||
// 注册配置
|
||||
bool replace_checks; // 替换现有检查
|
||||
HealthCheckConfig check_cfg; // 健康检查配置
|
||||
};
|
||||
|
||||
private:
|
||||
struct Config *ptr; // 配置指针
|
||||
std::atomic<int> *ref; // 引用计数
|
||||
};
|
||||
|
||||
// 地址类型,包含地址和端口
|
||||
using ConsulAddress = std::pair<std::string, unsigned short>;
|
||||
|
||||
/**
|
||||
* @struct ConsulService
|
||||
* @brief 表示一个服务的基本信息。
|
||||
*/
|
||||
struct ConsulService
|
||||
{
|
||||
std::string service_name; // 服务名称
|
||||
std::string service_namespace; // 服务命名空间
|
||||
std::string service_id; // 服务 ID
|
||||
std::vector<std::string> tags; // 服务标签
|
||||
ConsulAddress service_address; // 服务地址
|
||||
ConsulAddress lan; // 局域网地址
|
||||
ConsulAddress lan_ipv4; // 局域网 IPv4 地址
|
||||
ConsulAddress lan_ipv6; // 局域网 IPv6 地址
|
||||
ConsulAddress virtual_address; // 虚拟地址
|
||||
ConsulAddress wan; // 广域网地址
|
||||
ConsulAddress wan_ipv4; // 广域网 IPv4 地址
|
||||
ConsulAddress wan_ipv6; // 广域网 IPv6 地址
|
||||
std::map<std::string, std::string> meta; // 元信息
|
||||
bool tag_override; // 标签覆盖标志
|
||||
};
|
||||
|
||||
/**
|
||||
* @struct ConsulServiceInstance
|
||||
* @brief 表示服务实例,包括节点和健康检查信息。
|
||||
*/
|
||||
struct ConsulServiceInstance
|
||||
{
|
||||
// 节点信息
|
||||
std::string node_id; // 节点 ID
|
||||
std::string node_name; // 节点名称
|
||||
std::string node_address; // 节点地址
|
||||
std::string dc; // 数据中心
|
||||
std::map<std::string, std::string> node_meta; // 节点元信息
|
||||
long long create_index; // 创建索引
|
||||
long long modify_index; // 修改索引
|
||||
|
||||
// 服务信息
|
||||
struct ConsulService service;
|
||||
|
||||
// 健康检查信息
|
||||
std::string check_name; // 检查名称
|
||||
std
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue