如何在 apiman 中本地测试 API?
How to test an API locally in apiman?
期望的行为
我想在本地测试 apiman API 以查看 apiman-quickstarts/echo-service 的响应(或者如果它不再适用于最新版本的 apiman,则任何回显响应都是伟大的)。
我试过的
我使用 the official apiman site 中的以下内容在 Ubuntu 17.10
上安装了 apiman
:
mkdir ~/apiman-1.3.1.Final
cd ~/apiman-1.3.1.Final
curl http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip -o wildfly-10.1.0.Final.zip
curl http://downloads.jboss.org/apiman/1.3.1.Final/apiman-distro-wildfly10-1.3.1.Final-overlay.zip -o apiman-distro-wildfly10-1.3.1.Final-overlay.zip
unzip wildfly-10.1.0.Final.zip
unzip -o apiman-distro-wildfly10-1.3.1.Final-overlay.zip -d wildfly-10.1.0.Final
cd wildfly-10.1.0.Final
./bin/standalone.sh -c standalone-apiman.xml
使用 apiman GUI (localhost:8080/apimanui/api-manager
),我创建了一个 Organisation
,一个没有 API Security
的 Public API
,一个任意定义为 http://localhost:8080/apiman-echo
,还有一个 Client App
。由于 API 被定义为 Public
,因此我的理解是不需要在 Client App
中定义 Contract
。
API 的 Managed Endpoint
在 GUI 中提供为:
https://localhost:8443/apiman-gateway/MyTestOrganisation/MyTestAPI/2.0
实际行为
这些是卷曲结果:
# using the 'managed endpoint' provided in the gui - https
curl -k https://localhost:8443/apiman-gateway/MyTestOrganisation/MyTestAPI/2.0
<html><head><title>Error</title></head><body>404 - Not Found</body></html>
# using the 'managed endpoint' provided in the gui - http
curl -k http://localhost:8443/apiman-gateway/MyTestOrganisation/MyTestAPI/2.0
curl: (52) Empty reply from server
#using the endpoint i defined
curl -k http://localhost:8080/apiman-echo
<html><head><title>Error</title></head><body>404 - Not Found</body></html>
速成课程文档 (apiman.io/latest/crash-course.html) 指出以下是 apiman 文件夹的内容,但是在我电脑上的 wildfly-10.1.0.Final
文件夹中搜索 quickstarts
returns 没有结果:
├── apiman
│ ├── data
│ │ ├── all-policyDefs.json
│ │ └── apiman-realm.json
│ ├── ddls
│ │ ├── apiman_mysql5.ddl
│ │ └── apiman_postgresql9.ddl
│ ├── quickstarts
│ │ ├── echo-service
│ │ ├── LICENSE
│ │ ├── pom.xml
│ │ └── README.md
│ └── sample-configs
│ ├── apiman-ds_mysql.xml
│ └── apiman-ds_postgresql.xml
我觉得我遗漏了一些与 "installing" 和 echo-service
相关的步骤,并使其可供测试。
在 apiman.gitbooks.io/apiman-user-guide and apiman.gitbooks.io/apiman-production-guide produces no results, whilst apiman.io/latest/developer-guide.html 中搜索 echo
状态:
the mock back-end API is a simple "echo" API that responds to all
requests with a JSON payload describing the request it received
但我不确定如何解释和利用这些信息。
我想我已经找到了解决办法。
blog post here 中有一节叫做 "Getting and Building and Deploying the Example API",其内容似乎与早期版本的 apiman 更相关,但大体上是正确的。
我在下面记录了我的过程和结果,以防对其他人有所帮助。
来自博客 post 的以下步骤导致编译错误:
# make a directory on desktop to clone files into
mkdir ~/Desktop/my_container_for_echo_quickstart
# change into directory
cd my_container_for_echo_quickstart
# git clone
git clone https://github.com/apiman/apiman-quickstarts.git
# change into directory
cd apiman-quickstarts/echo-service/
# install maven so i can run `mvn package`
sudo apt install maven
# run `mvn package`
mvn package
记录大量下载信息后,出现如下错误:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:37 min
[INFO] Finished at: 2017-11-26T19:39:29+10:00
[INFO] Final Memory: 17M/242M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project apiman-quickstarts-echo-service: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
然后我查找 java 版本并得到以下内容:
java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.17.10.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
所以我这样做了:
sudo apt-get update
sudo apt-get install openjdk-8-jdk
mvn package
得到BUILD SUCCESS
。博客 post 继续说:
If you look closely, near the end of the output, you’ll see the
location of the .war file
它实际上输出了.jar
文件的位置:
Building jar: /home/me/Desktop/my_container_for_echo_quickstart/apiman-quickstarts/echo-service/target/apiman-quickstarts-echo-service-1.3.1.Final-javadoc.jar
但是在同一目录下有一个 .war
文件:
apiman-quickstarts-echo-service-1.3.1.Final.war
所以我将其复制到 /home/me/apiman-1.3.1.Final/wildfly-10.1.0.Final/standalone/deployments
并在另一个终端中看到服务器输出发生变化:
20:10:54,463 INFO [org.jboss.as.repository] (DeploymentScanner-threads - 1) WFLYDR0001: Content added at location /home/me/apiman-1.3.1.Final/wildfly-10.1.0.Final/standalone/data/content/c6/*****/content
20:10:54,482 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) WFLYSRV0027: Starting deployment of "apiman-quickstarts-echo-service-1.3.1.Final.war" (runtime-name: "apiman-quickstarts-echo-service-1.3.1.Final.war")
20:10:54,667 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 76) WFLYUT0021: Registered web context: /apiman-echo
20:10:54,772 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "apiman-quickstarts-echo-service-1.3.1.Final.war" (runtime-name : "apiman-quickstarts-echo-service-1.3.1.Final.war")
这是生成的卷曲结果:
curl -k http://localhost:8080/apiman-echo
{
"method" : "GET",
"resource" : "/apiman-echo",
"uri" : "/apiman-echo",
"headers" : {
"Accept" : "*/*",
"User-Agent" : "curl/7.55.1",
"Host" : "localhost:8080"
},
"bodyLength" : null,
"bodySha1" : null
}
虽然这令人鼓舞,但我担心因为我首先在 apiman GUI 中使用 /apiman-echo
端点设置 API,它与我创建的端点之间会发生冲突通过 maven 进程,但我们会看到。
期望的行为
我想在本地测试 apiman API 以查看 apiman-quickstarts/echo-service 的响应(或者如果它不再适用于最新版本的 apiman,则任何回显响应都是伟大的)。
我试过的
我使用 the official apiman site 中的以下内容在 Ubuntu 17.10
上安装了 apiman
:
mkdir ~/apiman-1.3.1.Final
cd ~/apiman-1.3.1.Final
curl http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip -o wildfly-10.1.0.Final.zip
curl http://downloads.jboss.org/apiman/1.3.1.Final/apiman-distro-wildfly10-1.3.1.Final-overlay.zip -o apiman-distro-wildfly10-1.3.1.Final-overlay.zip
unzip wildfly-10.1.0.Final.zip
unzip -o apiman-distro-wildfly10-1.3.1.Final-overlay.zip -d wildfly-10.1.0.Final
cd wildfly-10.1.0.Final
./bin/standalone.sh -c standalone-apiman.xml
使用 apiman GUI (localhost:8080/apimanui/api-manager
),我创建了一个 Organisation
,一个没有 API Security
的 Public API
,一个任意定义为 http://localhost:8080/apiman-echo
,还有一个 Client App
。由于 API 被定义为 Public
,因此我的理解是不需要在 Client App
中定义 Contract
。
API 的 Managed Endpoint
在 GUI 中提供为:
https://localhost:8443/apiman-gateway/MyTestOrganisation/MyTestAPI/2.0
实际行为
这些是卷曲结果:
# using the 'managed endpoint' provided in the gui - https
curl -k https://localhost:8443/apiman-gateway/MyTestOrganisation/MyTestAPI/2.0
<html><head><title>Error</title></head><body>404 - Not Found</body></html>
# using the 'managed endpoint' provided in the gui - http
curl -k http://localhost:8443/apiman-gateway/MyTestOrganisation/MyTestAPI/2.0
curl: (52) Empty reply from server
#using the endpoint i defined
curl -k http://localhost:8080/apiman-echo
<html><head><title>Error</title></head><body>404 - Not Found</body></html>
速成课程文档 (apiman.io/latest/crash-course.html) 指出以下是 apiman 文件夹的内容,但是在我电脑上的 wildfly-10.1.0.Final
文件夹中搜索 quickstarts
returns 没有结果:
├── apiman
│ ├── data
│ │ ├── all-policyDefs.json
│ │ └── apiman-realm.json
│ ├── ddls
│ │ ├── apiman_mysql5.ddl
│ │ └── apiman_postgresql9.ddl
│ ├── quickstarts
│ │ ├── echo-service
│ │ ├── LICENSE
│ │ ├── pom.xml
│ │ └── README.md
│ └── sample-configs
│ ├── apiman-ds_mysql.xml
│ └── apiman-ds_postgresql.xml
我觉得我遗漏了一些与 "installing" 和 echo-service
相关的步骤,并使其可供测试。
在 apiman.gitbooks.io/apiman-user-guide and apiman.gitbooks.io/apiman-production-guide produces no results, whilst apiman.io/latest/developer-guide.html 中搜索 echo
状态:
the mock back-end API is a simple "echo" API that responds to all requests with a JSON payload describing the request it received
但我不确定如何解释和利用这些信息。
我想我已经找到了解决办法。
blog post here 中有一节叫做 "Getting and Building and Deploying the Example API",其内容似乎与早期版本的 apiman 更相关,但大体上是正确的。
我在下面记录了我的过程和结果,以防对其他人有所帮助。
来自博客 post 的以下步骤导致编译错误:
# make a directory on desktop to clone files into
mkdir ~/Desktop/my_container_for_echo_quickstart
# change into directory
cd my_container_for_echo_quickstart
# git clone
git clone https://github.com/apiman/apiman-quickstarts.git
# change into directory
cd apiman-quickstarts/echo-service/
# install maven so i can run `mvn package`
sudo apt install maven
# run `mvn package`
mvn package
记录大量下载信息后,出现如下错误:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:37 min
[INFO] Finished at: 2017-11-26T19:39:29+10:00
[INFO] Final Memory: 17M/242M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project apiman-quickstarts-echo-service: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
然后我查找 java 版本并得到以下内容:
java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.17.10.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
所以我这样做了:
sudo apt-get update
sudo apt-get install openjdk-8-jdk
mvn package
得到BUILD SUCCESS
。博客 post 继续说:
If you look closely, near the end of the output, you’ll see the location of the .war file
它实际上输出了.jar
文件的位置:
Building jar: /home/me/Desktop/my_container_for_echo_quickstart/apiman-quickstarts/echo-service/target/apiman-quickstarts-echo-service-1.3.1.Final-javadoc.jar
但是在同一目录下有一个 .war
文件:
apiman-quickstarts-echo-service-1.3.1.Final.war
所以我将其复制到 /home/me/apiman-1.3.1.Final/wildfly-10.1.0.Final/standalone/deployments
并在另一个终端中看到服务器输出发生变化:
20:10:54,463 INFO [org.jboss.as.repository] (DeploymentScanner-threads - 1) WFLYDR0001: Content added at location /home/me/apiman-1.3.1.Final/wildfly-10.1.0.Final/standalone/data/content/c6/*****/content
20:10:54,482 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) WFLYSRV0027: Starting deployment of "apiman-quickstarts-echo-service-1.3.1.Final.war" (runtime-name: "apiman-quickstarts-echo-service-1.3.1.Final.war")
20:10:54,667 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 76) WFLYUT0021: Registered web context: /apiman-echo
20:10:54,772 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "apiman-quickstarts-echo-service-1.3.1.Final.war" (runtime-name : "apiman-quickstarts-echo-service-1.3.1.Final.war")
这是生成的卷曲结果:
curl -k http://localhost:8080/apiman-echo
{
"method" : "GET",
"resource" : "/apiman-echo",
"uri" : "/apiman-echo",
"headers" : {
"Accept" : "*/*",
"User-Agent" : "curl/7.55.1",
"Host" : "localhost:8080"
},
"bodyLength" : null,
"bodySha1" : null
}
虽然这令人鼓舞,但我担心因为我首先在 apiman GUI 中使用 /apiman-echo
端点设置 API,它与我创建的端点之间会发生冲突通过 maven 进程,但我们会看到。