-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
179 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Manifest-Version: 1.4.2 | ||
Manifest-Version: 1.0 | ||
Main-Class: rpc.synapse.test.Test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package rpc.synapse.test; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.rabbitmq.client.AMQP; | ||
import rpc.synapse.chaos.BaseCallback; | ||
|
||
import java.util.Hashtable; | ||
|
||
public class EventTest extends BaseCallback { | ||
@Override | ||
public Hashtable<String, String> regAlias() { | ||
Hashtable<String, String> alias = new Hashtable<>(); | ||
alias.put("dotnet.test", "test"); | ||
alias.put("golang.test", "test"); | ||
alias.put("python.test", "test"); | ||
alias.put("php.test", "test"); | ||
alias.put("ruby.test", "test"); | ||
alias.put("java.test", "test"); | ||
return alias; | ||
} | ||
|
||
public boolean test(JSONObject body, AMQP.BasicProperties props) { | ||
System.out.printf("**收到EVENT: %s@%s %s\n", props.getType(), props.getReplyTo(), body.toJSONString()); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package rpc.synapse.test; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.rabbitmq.client.AMQP; | ||
import rpc.synapse.chaos.BaseCallback; | ||
|
||
import java.util.Hashtable; | ||
|
||
public class RpcTest extends BaseCallback { | ||
@Override | ||
public Hashtable<String, String> regAlias() { | ||
Hashtable<String, String> alias = new Hashtable<>(); | ||
alias.put("test", "test"); | ||
return alias; | ||
} | ||
|
||
public JSONObject test(JSONObject body, AMQP.BasicProperties props) { | ||
System.out.printf("**RPC请求: %s@%s %s\n", props.getType(), props.getReplyTo(), body.toJSONString()); | ||
JSONObject ret = new JSONObject(); | ||
ret.put("from", "java"); | ||
ret.put("m", body.get("msg")); | ||
ret.put("number", 5233); | ||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package rpc.synapse.test; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import org.apache.commons.cli.*; | ||
import rpc.synapse.chaos.Synapse; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
|
||
public class Test { | ||
public static void main(String[] args) throws Exception { | ||
Options options = new Options(); | ||
options.addOption("h", "help", false, "Show Help"); | ||
options.addRequiredOption(null, "host", true, "RabbitMQ Host"); | ||
options.addOption(null, "port", true, "RabbitMQ Port"); | ||
options.addRequiredOption("u", "user", true, "RabbitMQ Username"); | ||
options.addRequiredOption("p", "pass", true, "RabbitMQ Password"); | ||
options.addOption(null, "vhost", true, "RabbitMQ Virtual Host"); | ||
options.addRequiredOption(null, "sys_name", true, "system name"); | ||
options.addOption("d", "debug", false, "Debug Mode"); | ||
CommandLineParser parser = new DefaultParser(); | ||
CommandLine cmd = parser.parse(options, args); | ||
if (cmd.hasOption("h")) { | ||
String formatStr = "must need theses parameters"; | ||
HelpFormatter hf = new HelpFormatter(); | ||
hf.printHelp(formatStr, "", options, ""); | ||
return; | ||
} | ||
Synapse app = new Synapse(); | ||
if (cmd.hasOption("debug")) { | ||
app.debug = true; | ||
} | ||
if (cmd.hasOption("host")) { | ||
app.mqHost = cmd.getOptionValue("host"); | ||
} | ||
if (cmd.hasOption("port")) { | ||
app.mqPort = Integer.parseInt(cmd.getOptionValue("port")); | ||
} | ||
if (cmd.hasOption("user")) { | ||
app.mqUser = cmd.getOptionValue("user"); | ||
} | ||
if (cmd.hasOption("pass")) { | ||
app.mqPass = cmd.getOptionValue("pass"); | ||
} | ||
if (cmd.hasOption("vhost")) { | ||
app.mqVHost = cmd.getOptionValue("vhost"); | ||
} | ||
if (cmd.hasOption("sys_name")) { | ||
app.sysName = cmd.getOptionValue("sys_name"); | ||
} | ||
app.appName = "java"; | ||
app.eventCallback = new EventTest(); | ||
app.rpcCallback = new RpcTest(); | ||
app.serve(); | ||
String[] inputs; | ||
JSONObject body; | ||
JSONObject res; | ||
showHelp(); | ||
while (true) { | ||
body = new JSONObject(); | ||
String str = readDataFromConsole("input >> "); | ||
inputs = str.split(" "); | ||
switch (inputs[0]) { | ||
case "event": | ||
if (inputs.length != 3) { | ||
showHelp(); | ||
continue; | ||
} | ||
body.put("msg", inputs[2]); | ||
app.sendEvent(inputs[1], body); | ||
break; | ||
case "rpc": | ||
if (inputs.length != 4) { | ||
showHelp(); | ||
continue; | ||
} | ||
body.put("msg", inputs[2]); | ||
res = app.sendRpc(inputs[1], inputs[2], body); | ||
System.out.println(res.toJSONString()); | ||
break; | ||
default: | ||
showHelp(); | ||
} | ||
} | ||
} | ||
|
||
private static void showHelp() { | ||
System.out.println("----------------------------------------------"); | ||
System.out.println("| event usage: |"); | ||
System.out.println("| > event [event] [msg] |"); | ||
System.out.println("| rpc usage: |"); | ||
System.out.println("| > rpc [app] [method] [msg] |"); | ||
System.out.println("----------------------------------------------"); | ||
} | ||
|
||
private static String readDataFromConsole(String prompt) { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
String str = null; | ||
try { | ||
System.out.print(prompt); | ||
str = br.readLine(); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return str; | ||
} | ||
} |