drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.62k stars 1.12k forks source link

drogon如何在一台多网卡的机器上指定网卡运行? #2091

Closed bethebest0622 closed 3 months ago

bethebest0622 commented 4 months ago

我的机器有5个网卡,每个网卡有一个公网ip

在drogon code里面,有办法指定某个网卡运行吗

an-tao commented 4 months ago

Listener可以配置IP

bethebest0622 commented 4 months ago

Listener可以配置IP

可以指导一下吗? 有demo吗

an-tao commented 4 months ago

AddListener接口有个参数就是监听的IP,或者配置文件里有对应的配置项,建议你先看看wiki里的文档

bethebest0622 commented 4 months ago

您好,我写了一些简单的代码,似乎没有达到我想要的结果,可以请您看一下吗?

#include <drogon/drogon.h>
#include <unistd.h>

int main(int argc, char ** argv) {
  if (argc != 2) {
    printf("args count should be 2\n");
    exit(1);
  }
  std::thread t([argv] () {
    drogon::app().addListener(argv[1], 80).addListener(argv[1], 443).run();
  }); 
  t.detach();

  usleep(100000);

  // int cpu_cnt = util::common_util::GetCpuCount();
  // drogon::app().addListener(argv[1], 80).addListener(argv[1], 443).disableSigtermHandling().setThreadNum(MAX(cpu_cnt - 2, 2));
  auto cclient_ = drogon::HttpClient::newHttpClient("https://ipinfo.io");
  auto req = drogon::HttpRequest::newHttpRequest();
  req->setContentTypeString("application/json");
  req->setMethod(drogon::HttpMethod::Get);
  req->setPath("/ip");
  req->setBody("");
  // req->setContentTypeString("application/json");
  const auto & [req_result, reponse_ptr] = cclient_->sendRequest(req);

  printf("body = %s\n", reponse_ptr->getBody().data());
}

我的机器有5个网卡,ifconfig 结果如下:

enp55s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.30.231  netmask 255.255.240.0  broadcast 172.31.31.255
        inet6 fe80::49b:4eff:fe04:644b  prefixlen 64  scopeid 0x20<link>
        ether 06:9b:4e:04:64:4b  txqueuelen 1000  (Ethernet)
        RX packets 103444575  bytes 39748786460 (37.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 96770438  bytes 6613227662 (6.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp56s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.31.32  netmask 255.255.240.0  broadcast 172.31.31.255
        inet6 fe80::4d8:51ff:fed9:a8e9  prefixlen 64  scopeid 0x20<link>
        ether 06:d8:51:d9:a8:e9  txqueuelen 1000  (Ethernet)
        RX packets 1805  bytes 83921 (81.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2674  bytes 211113 (206.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp57s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.24.253  netmask 255.255.240.0  broadcast 172.31.31.255
        inet6 fe80::4b6:d6ff:fe2f:d0e3  prefixlen 64  scopeid 0x20<link>
        ether 06:b6:d6:2f:d0:e3  txqueuelen 1000  (Ethernet)
        RX packets 1772  bytes 80618 (78.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2643  bytes 207397 (202.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp58s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.31.30  netmask 255.255.240.0  broadcast 172.31.31.255
        inet6 fe80::405:b9ff:fe69:8031  prefixlen 64  scopeid 0x20<link>
        ether 06:05:b9:69:80:31  txqueuelen 1000  (Ethernet)
        RX packets 89189  bytes 92266319 (87.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 41518  bytes 9523023 (9.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp59s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.28.158  netmask 255.255.240.0  broadcast 172.31.31.255
        inet6 fe80::437:17ff:fe73:7ea5  prefixlen 64  scopeid 0x20<link>
        ether 06:37:17:73:7e:a5  txqueuelen 1000  (Ethernet)
        RX packets 1767  bytes 79616 (77.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2635  bytes 206809 (201.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

似乎我不管如何指定listener的地址,返回的ip地址都是第一个网卡的公网地址(我机器五个网卡,各自都有独立的ip地址)

an-tao commented 4 months ago

172不是内网地址么,这个问题似乎更取决于你ipinfo.io的DNS怎么配的,以及你的服务器的网络环境的配置,而且你是在本机发起的请求,跟你本机的路由配置也有关系

bethebest0622 commented 4 months ago

172不是内网地址么,这个问题似乎更取决于你ipinfo.io的DNS怎么配的,以及你的服务器的网络环境的配置,而且你是在本机发起的请求,跟你本机的路由配置也有关系

我是想指定一个网卡来执行这些http操作,因为服务端会根据我的ip来进行流量控制,我为我机器的5个网卡都购买了单独的ip,所以我希望我可以指定使用的网卡。

另外ipinfo.io这个网站,只是用来获取我http请求的ip地址

bethebest0622 commented 4 months ago

您好,有空时请您再看一下

an-tao commented 4 months ago

那做不到吧,每个机器只有一个默认路由,也就是说你访问公网地址的时候,只能走其中一块网卡,除非你再另跑4个虚拟机,配成桥模式,各自配自己的公网路由

bethebest0622 commented 4 months ago

可是我curl --interface enp57s0 ipinfo.io可以得到别的ip 所以我想应该有些办法

an-tao commented 4 months ago

那相当于指定了路由,HttpClient目前没有这种参数,你得自己改造了,或者等我有时间看看好不好加上

bethebest0622 commented 4 months ago

好的,谢谢!