apisix Admin API配置TCP路由转发

apisix Admin API配置TCP路由转发

 

一、开启和配置stream代理

  1、修改/usr/local/apisix/conf/config.yaml文件

    新增以下配置

 apisix:
  proxy_mode: http&stream
  stream_proxy:
    tcp:
      - 9100
      - "192.168.1.110:9101"
    udp:
      - 9200

  proxy_mode: 配置开启stream,默认是http,这里修改为http&stream

  stream_proxy: 配置tcp和udp信息。

 

  2、重启apisix服务

systemctl restart apisix

 

二、配置上游服务

复制代码
curl http://127.0.0.1:9180/apisix/admin/upstreams  \
-H "X-API-KEY: $admin_key" -i -X PUT -d '
{
  "id": "test-upstream-mysql",
  "name": "mytest-upstream-mysql",
  "desc": "测试upstream-mysql",
  "scheme": "tcp",
  "keepalive_pool": {
    "idle_timeout": 60,
    "requests": 1000,
    "size": 320
  },
  "nodes": [
    {
      "host": "192.168.1.110",
      "priority": 0,
      "port": 3306,
      "weight": 1
    }
  ],
  "timeout": {
    "connect": 6,
    "send": 6,
    "read": 6
  },
  "type": "roundrobin",
  "checks": {
    "active": {
      "http_path": "/",
      "type": "tcp",
      "concurrency": 10,
      "timeout": 3,
      "healthy": {
        "http_statuses": [
          200,
          302
        ],
        "interval": 3,
        "successes": 1
      },
      "unhealthy": {
        "http_statuses": [
          429,
          404,
          500,
          501,
          502,
          503,
          504,
          505
        ],
        "interval": 3,
        "http_failures": 2,
        "tcp_failures": 2,
        "timeouts": 2
      }
    }
  }
}'
复制代码

 

三、配置路由

  1、配置通用路由,则9100、9101端口都可以转发tcp,9200可以转发udp

curl http://127.0.0.1:9180/apisix/admin/stream_routes -H "X-API-KEY: $admin_key" -X PUT -d '
{
    "id": "test-stream",
    "upstream_id": "test-upstream-mysql"
}'

 

  2、配置匹配路由,则只有匹配到IP、端口或者IP+端口的情况才做转发。

curl http://127.0.0.1:9180/apisix/admin/stream_routes -H "X-API-KEY: $admin_key" -X PUT -d '
{
    "id": "test-stream",
    "server_addr": "192.168.1.110",
    "server_port": 9101,
    "upstream_id": "test-upstream-mysql"
}'

 

四、验证匹配路由

 

[root@archive ~]# mysql -h 192.168.1.110 -P 9100 -uroot -p
Enter password: 
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
[root@localhost ~]# mysql -h 192.168.1.110 -P 9101 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
...
mysql>

 

posted @   难止汗  阅读(382)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示