1、下载及安装:
运行环境:java
- JAVA环境
- linux
下载地址:https://github.com/dreamhead/mocolinux
下载下来的是一个jar包,如:moco-runner-0.12.0-standalone.jargit
把这个jar包上传到linux服务器上,而后在与moco-runner-0.12.0-standalone.jar同级的目录下建立 foo.json 文件:github
[ { "response" : { "text" : "Hello, Moco" } } ]
Moco的运行很是简单,只须要一行命令便可: java -jar <path-to-moco-runner> http -p <monitor-port> -c < configuration -file>正则表达式
<path-to-moco-runner>
:moco-runner-0.11.0-standalone.jar包的路径<monitor-port>
:http服务监听的端口<configuration -file>
:配置文件路径
java -jar moco-runner-0.12.0-standalone.jar http -p 9999 -c foo.json
经过浏览器访问服务器的9999端口:shell
返回正确就说明Moco服务搭建成功了。json
虽说服务运行没问题了,可是总以为启动和中止很麻烦,如今用shell来写一个启动和中止的脚本api
启动start.sh:浏览器
#!/bin/bash dirWiremock=`pwd` getCount=`ps -ef | grep "moco-runner" | grep -v "grep" |wc -l` wiremock_jar=${dirWiremock}/moco-runner-0.12.0-standalone.jar port=9999 exe_name=moco-runner if [ $getCount -ne 0 ]; then echo $exe_name is already running with $ssu_pid_count processes echo $exe_name started failed exit 1; else nohup java -jar ${wiremock_jar} http -p ${port} -c foo.json & echo "Start success!......" fi
中止stop.sh:bash
#!/bin/sh exe_name=moco-runner # 进入可执行目录 base_path=$(cd `dirname $0`; pwd) cd $base_path # 中止进程 exe_pid_path=`pwd` exe_pid_list=`ps -ef | grep $exe_name|grep -v 'grep'|awk '{print $2}'` our_pid_list="" for exe_pid in $exe_pid_list do pid_path=`pwdx $exe_pid | awk '{print $2}'` if [ "$pid_path"x == "$exe_pid_path"x ]; then our_pid_list=$our_pid_list" "$exe_pid fi done if [ "$our_pid_list"x != "x" ]; then kill -9 $our_pid_list echo $exe_name process"$our_pid_list" killed else echo $exe_name is not running fi
运行shell脚本,启动和中止就方便多了。
2、Moco配置
启动服务以后,必然会根据需求stub出各类各样接口反馈,咱们会把这个配置放在一个json文件中,启动Moco的时候,须要指定使用的配置文件路径,这样配置就能够生效了。Moco服务能够检测到配置文件的变动,假如你修改了配置文件,不须要从新启动Moco,服务照样能够生效。更详细的配置介绍请查看:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md
配置文件的工做原理大体以下:
如何在配置文件添加注释
json不支持注释,想要添加注释的话,能够在description字段中加入描述
约定请求Body
约定接口的uri
约定请求参数
约定请求方法
约定HTTP版本
约定请求头部
约定cookie
约定请求form
表单能够添加多项,多项的时候,必须所有匹配,接口才算匹配成功