Algotrader API密钥配置:Robinhood与Alpha Vantage认证全攻略
2026/8/14 19:24:43
✅核心架构:1 台 Director (LVS)→2 台物理机(机 A / 机 B)→每机 2 个 RS→每 RS1 个 Nginx(共 4 个独立 Nginx),DR 模式,全程步骤落地可直接复制✅统一环境:CentOS 7.x,所有节点同网段(示例网段 192.168.1.x),VIP=192.168.1.100,机 A=192.168.1.10,机 B=192.168.1.11
所有节点(Director + 机 A + 机 B)统一执行,避免依赖缺失
bash
# 临时关闭 systemctl stop firewalld && setenforce 0 # 永久禁用,重启不失效 systemctl disable firewalld sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/configbash
yum install -y ipvsadm keepalived gcc gcc-c++ make pcre-devel zlib-devel wgetbash
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf sysctl -pbash
# 创建VIP网卡配置文件 cat > /etc/sysconfig/network-scripts/ifcfg-eth0:0 <<EOF TYPE=Ethernet BOOTPROTO=static NAME=eth0:0 DEVICE=eth0:0 IPADDR=192.168.1.100 NETMASK=255.255.255.255 ONBOOT=yes USERCTL=no EOF # 启动VIP网卡 ifup eth0:0 # 验证:ip addr 能看到eth0:0绑定192.168.1.100bash
# 1. 清空旧规则(避免冲突) ipvsadm -C # 2. 添加VIP服务:对外暴露VIP:80,加权轮询(性能均衡) ipvsadm -A -t 192.168.1.100:80 -s wrr # 3. 绑定机A 2个RS(8080/8081端口=2个Nginx) ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10:8080 -g ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10:8081 -g # 4. 绑定机B 2个RS(8080/8081端口=2个Nginx) ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.11:8080 -g ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.11:8081 -g # 5. 保存规则(开机自启) ipvsadm-save > /etc/sysconfig/ipvsadm # 6. 启动ipvsadm并设置开机自启 systemctl enable --now ipvsadm # 验证:ipvsadm -Ln 能看到4个RS,状态都是UPbash
# 覆盖默认配置 cat > /etc/keepalived/keepalived.conf <<EOF global_defs { router_id LVS_DIRECTOR # 唯一标识,备机可改LVS_DIRECTOR_BACKUP } vrrp_instance VI_1 { state MASTER # 备机改BACKUP interface eth0 # 绑定网卡 virtual_router_id 51 # 主备一致 priority 100 # 备机改90,优先级低 advert_int 1 # 心跳间隔1秒 authentication { auth_type PASS auth_pass 1111 # 主备一致 } virtual_ipaddress { 192.168.1.100/32 dev eth0 label eth0:0 # VIP配置 } } # 4个RS健康检查(关键:对应端口) virtual_server 192.168.1.100 80 { delay_loop 6 # 检查间隔6秒 lb_algo wrr # 加权轮询 lb_kind DR # DR模式 persistence_timeout 0 # 关闭会话保持 protocol TCP # 机A RS1 8080 real_server 192.168.1.10 8080 { weight 1 # 权重,越高分发越多 TCP_CHECK { connect_port 8080 connect_timeout 3 # 超时3秒 retry 3 # 重试3次 } } # 机A RS2 8081 real_server 192.168.1.10 8081 { weight 1 TCP_CHECK { connect_port 8081 connect_timeout 3 retry 3 } } # 机B RS3 8080 real_server 192.168.1.11 8080 { weight 1 TCP_CHECK { connect_port 8080 connect_timeout 3 retry 3 } } # 机B RS4 8081 real_server 192.168.1.11 8081 { weight 1 TCP_CHECK { connect_port 8081 connect_timeout 3 retry 3 } } } EOF # 启动Keepalived并开机自启 systemctl enable --now keepalived # 验证:ip addr 能看到VIP,systemctl status keepalived 状态running机 A(192.168.1.10)和机 B(192.168.1.11)完全执行相同命令
bash
# 创建回环VIP配置 cat > /etc/sysconfig/network-scripts/ifcfg-lo:0 <<EOF TYPE=Ethernet BOOTPROTO=static NAME=lo:0 DEVICE=lo:0 IPADDR=192.168.1.100 NETMASK=255.255.255.255 ONBOOT=yes USERCTL=no EOF # 启动回环VIP ifup lo:0 # 验证:ip addr 能看到lo:0绑定192.168.1.100bash
# 添加ARP参数 cat >> /etc/sysctl.conf <<EOF net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 EOF # 生效配置 sysctl -p机 A、机 B 均执行,全程分步教安装,零踩坑,用源码安装保证独立,避免 yum 安装冲突
bash
# 统一目录,方便管理 mkdir -p /usr/local/nginx8080 /usr/local/nginx8081 # 2个Nginx安装目录 mkdir -p /data/nginx/html8080 /data/nginx/html8081 # 2个Nginx网站目录 mkdir -p /var/log/nginx8080 /var/log/nginx8081 # 2个Nginx日志目录bash
cd /usr/local/src wget https://nginx.org/download/nginx-1.24.0.tar.gz # 稳定版 tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0bash
# 配置编译参数:指定安装目录、端口8080 ./configure \ --prefix=/usr/local/nginx8080 \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_sub_module \ --with-stream \ --http-port=8080 \ --pid-path=/var/run/nginx8080.pid \ --error-log-path=/var/log/nginx8080/error.log \ --http-log-path=/var/log/nginx8080/access.log # 编译+安装 make && make install # 验证安装:/usr/local/nginx8080/sbin/nginx -v 显示版本即成功同一源码包直接编译,无需重新下载,仅改目录和端口
bash
# 配置编译参数:指定安装目录、端口8081 ./configure \ --prefix=/usr/local/nginx8081 \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-http_sub_module \ --with-stream \ --http-port=8081 \ --pid-path=/var/run/nginx8081.pid \ --error-log-path=/var/log/nginx8081/error.log \ --http-log-path=/var/log/nginx8081/access.log # 编译+安装 make && make install # 验证安装:/usr/local/nginx8081/sbin/nginx -v 显示版本即成功bash
# 编辑配置文件 cat > /usr/local/nginx8080/conf/nginx.conf <<EOF worker_processes auto; # 自动匹配CPU核数,不抢资源 worker_rlimit_nofile 65535; # 调大文件句柄,扛高并发 events { use epoll; # 高效IO模型 worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; # 日志格式 log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' '\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" "\$http_x_forwarded_for"'; access_log /var/log/nginx8080/access.log main; error_log /var/log/nginx8080/error.log warn; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; server { listen 8080; # 固定端口,和LVS规则对应 server_name _; # 匹配所有请求(对应VIP) location / { root /data/nginx/html8080; # 网站根目录 index index.html index.htm; } # 健康检查接口(方便排查) location /health { stub_status on; access_log off; } # 错误页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOFbash
# 编辑配置文件,仅改端口和目录,其余一致 cat > /usr/local/nginx8081/conf/nginx.conf <<EOF worker_processes auto; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; charset utf-8; log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' '\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" "\$http_x_forwarded_for"'; access_log /var/log/nginx8081/access.log main; error_log /var/log/nginx8081/error.log warn; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; gzip on; server { listen 8081; # 固定端口,和LVS规则对应 server_name _; location / { root /data/nginx/html8081; # 网站根目录 index index.html index.htm; } location /health { stub_status on; access_log off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOFbash
# 机A RS1(8080) echo "【物理机A - RS1 - Nginx8080】" > /data/nginx/html8080/index.html # 机A RS2(8081) echo "【物理机A - RS2 - Nginx8081】" > /data/nginx/html8081/index.htmlbash
# 机B RS3(8080) echo "【物理机B - RS3 - Nginx8080】" > /data/nginx/html8080/index.html # 机B RS4(8081) echo "【物理机B - RS4 - Nginx8081】" > /data/nginx/html8081/index.htmlbash
# 启动8080端口Nginx /usr/local/nginx8080/sbin/nginx # 启动8081端口Nginx /usr/local/nginx8081/sbin/nginx # 验证启动:ss -tlnp | grep nginx 能看到8080、8081端口监听 # 验证访问:本机curl http://127.0.0.1:8080 和 http://127.0.0.1:8081 能看到对应页面创建 systemd 服务文件,支持 systemctl 管理
bash
# 8080端口Nginx服务 cat > /usr/lib/systemd/system/nginx8080.service <<EOF [Unit] Description=Nginx 8080 Service After=network.target [Service] Type=forking PIDFile=/var/run/nginx8080.pid ExecStart=/usr/local/nginx8080/sbin/nginx ExecReload=/usr/local/nginx8080/sbin/nginx -s reload ExecStop=/usr/local/nginx8080/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF # 8081端口Nginx服务 cat > /usr/lib/systemd/system/nginx8081.service <<EOF [Unit] Description=Nginx 8081 Service After=network.target [Service] Type=forking PIDFile=/var/run/nginx8081.pid ExecStart=/usr/local/nginx8081/sbin/nginx ExecReload=/usr/local/nginx8081/sbin/nginx -s reload ExecStop=/usr/local/nginx8081/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF # 重新加载服务,设置开机自启 systemctl daemon-reload systemctl enable --now nginx8080 nginx8081 # 验证自启:systemctl status nginx8080 nginx8081 均为runningbash
# 查看LVS规则+RS状态(核心) ipvsadm -Ln # 查看LVS流量统计(看分发是否均匀) ipvsadm -Ln --stats # 新增RS(比如机A加8082端口) ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.10:8082 -g # 删除RS(比如机A8081端口) ipvsadm -d -t 192.168.1.100:80 -r 192.168.1.10:8081 # 重启LVS systemctl restart ipvsadmbash
# 启停单个Nginx(互不影响) systemctl start/stop/restart/status nginx8080 systemctl start/stop/restart/status nginx8081 # 重载Nginx配置(不重启) /usr/local/nginx8080/sbin/nginx -s reload /usr/local/nginx8081/sbin/nginx -s reload # 查看Nginx日志(排查问题) tail -f /var/log/nginx8080/access.log tail -f /var/log/nginx8081/error.log客户端(同网段任意机器)访问http://192.168.1.100(VIP:80),多次刷新,会轮询显示 4 个页面:✅ 【物理机 A - RS1 - Nginx8080】✅ 【物理机 A - RS2 - Nginx8081】✅ 【物理机 B - RS3 - Nginx8080】✅ 【物理机 B - RS4 - Nginx8081】→ 说明 LVS 分发成功
systemctl stop nginx8080systemctl start nginx8080,页面恢复轮询→ 说明 LVS 健康检查生效worker_processes = CPU核数/2,避免抢核;worker_connections设 65535 即可