ShenYu 閘道器開發:在本地啟用執行

語言: CN / TW / HK

1.先決條件

無論什麼方式安裝,都需要先初始化資料庫,這裡我選擇了在本地通過 Docker 啟用一個 mysql 5.7

docker run -d \
  --name mysql \
  -p 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=xyzj1a2y3 \
  -e TZ=Asia/Shanghai \
  mysql:5.7

啟動以後,可以通過資料庫管理軟體進行連線 localhost:3306 使用者名稱和密碼都是 root。

然後根據 文件 執行 mysql 資料庫初始化,我是直接通過資料庫管理軟體執行的 sql。執行成功以後建立一個 shenyu 資料庫,並且會建立一些表並插入必須資料。

2.本地部署

文件: http://shenyu.apache.org/zh/docs/deployment/deployment-local

Clone 程式碼到本地

git clone http://github.com/apache/incubator-shenyu.git
cd incubator-shenyu
mvn clean install -Dmaven.javadoc.skip=true -B -Drat.skip=true -Djacoco.skip=true -DskipITs -DskipTests

配置 Mysql

cd shenyu-admin/src/main/resources

application-mysql.yml:

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

shenyu:
  database:
    dialect: mysql
    init_enable: true

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull
    username: root
    password: xyzj1a2y3
    driver-class-name: com.mysql.cj.jdbc.Driver

啟動 ShenYu Admin

使用 IDE 啟動 org.apache.shenyu.admin.ShenyuAdminBootstrap 並配置引數 --spring.profiles.active = mysql

啟用 ShenYu Bootstrap

使用 IDE 啟動 org.apache.shenyu.bootstrap.ShenyuBootstrapApplication

訪問 Admin

訪問 http://localhost:9095 ,預設使用者名稱密碼分別為 admin 和 123456

3.執行測試

ShenYu 提供了多方方式接入閘道器,直接在 UI 操作,通過 client 接入,client 又分為 http、nacos、zookeeper 等等。這裡我是用了 client http 的方式接入。

我執行的是 dotnet client 倉庫提供的 example,註冊成功後可以在 Admin 介面查詢到 selector 資訊:

然後採用 postman 呼叫測試,需要注意的是 9095 埠是 admin 埠,9195 才是閘道器的埠:

可以看到成功進行了呼叫