跳到主要内容
版本:3.2.14.3

转发四层流量

API7 网关除了处理应用层(L7)流量外,还可以处理传输层(L4)TCP 和 UDP 流量。

本教程将演示如何配置 四层路由 在客户端和 MySQL 服务器之间代理 L4 流量。

前提条件

  1. 安装 API7 企业版
  2. 网关组中至少有一个 网关实例
  3. 安装 MySQL 客户端 以验证四层路由。

启动 MySQL 服务器

如果你在 Docker 中安装了网关实例并使用 Dashboard 或 ADC 进行配置,请在默认的 API7 Enterprise 网络 api7-ee_api7 中启动 MySQL 服务器:

docker run -d \
--name mysql \
--network=api7-ee_api7 \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=password \
mysql:8.4 \
mysqld --mysql-native-password=ON

添加具有四层路由的服务

  1. 从侧导航栏中选择网关组的 已发布服务,然后点击 新增服务
  2. 选择 手动新增
  3. 在表单中执行以下操作:
  • 名称 填写 MySQL
  • 服务类型 选择 Stream(四层代理)
  • 上游 Scheme 选择TCP
  • 如何找到上游 选择使用节点
  • 点击新增节点
  • 在表单中执行以下操作:
    • 主机 填写 127.0.0.1
    • 端口 填写 3306
    • 权重 使用默认值 100
    • 点击 新增。这将新建一个 “无版本” 状态的新服务。
  1. 在服务内,点击 新增四层路由
  2. 在表单中执行以下操作:
  • 名称 填写 stream-route-mysql
  • 服务器地址 填写 127.0.0.1
  • 服务器端口 填写 2000
  • 点击 新增

验证四层路由

如果你在 Docker 中安装了网关实例并使用控制台或 ADC 进行配置,在继续验证步骤之前,请确保将服务器端口 2000 暴露给宿主机(-p2000:2000)。

使用 MySQL 客户端通过 API7 Gateway 与 MySQL 服务器建立连接。以 root 身份连接,并使用之前配置的密码。

mysql --host=127.0.0.1 --port=2000 -u root -p

你应该会看到如下的 MySQL 提示符:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.4.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

相关阅读