最近做一个bytetcc分布式事务的试验看看在高并发下它的机制,和性能如何。所以要模拟http请求,直接上代码
public class HttpSendTest {
static CyclicBarrier cyclicBarrier = new CyclicBarrier(10);//让十个线程同时请求
public static void main(String[] args) throws InterruptedException, BrokenBarrierException,
IOException, TimeoutException {
test();
}
public static void test() throws InterruptedException, BrokenBarrierException, IOException,
TimeoutException {
for (int i = 0; i < 50; i++) {
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e1) {
// logger.error("", e);
}
try {
//这是key=value的写法 post
httpRequest(
"http://192.168.2.49:7080/transfer?Id=1001&amount=1&acctId=2001",
"POST", "");
} catch (IOException e) {
//logger.error("", e);
} catch (InterruptedException e) {
//logger.error("", e);
} catch (BrokenBarrierException e) {
// logger.error("", e);
} catch (TimeoutException e) {
// logger.error("", e);
}
}
}
});
thread.start();
System.out.println(thread.getName());
}
}
public static String httpRequest(String requestUrl, String requestMethod, String data)
throws IOException,
InterruptedException,
BrokenBarrierException,
TimeoutException {
System.out.println("httpRequest==" + Thread.currentThread().getName());
int number = cyclicBarrier.await();
System.out.println("number=" + number);
URL url = new URL(requestUrl);
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置通用的请求属性
httpURLConnection.setRequestProperty("Accept", "*/*");
httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
httpURLConnection.setRequestProperty("connection", "keep-alive");
// httpURLConnection.setRequestProperty("Content-Length", data.getBytes("UTF-8").length + "");
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
// 设置请求方式(GET/POST)
httpURLConnection.setRequestMethod(requestMethod);
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream outputStream = httpURLConnection.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(data.getBytes("UTF-8"));
outputStream.close();
int code = httpURLConnection.getResponseCode();
if (code == 200) {
// 将返回的输入流转换成字符串
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpURLConnection.disconnect();
// JSONObject jsonObject = JSONObject.parseObject(buffer.toString()); //这里可以把服务器返回的数据转成 jsonObject 然后方便取数据
System.out.println(buffer.toString());
}
return buffer.toString();
}
}
key=value模式直接在请求的url后面类似get请求的方式
服务端编写:
@ResponseBody
@RequestMapping(value = "/transfer", method = RequestMethod.POST)
@Transactional
public String transfer(@RequestParam String Id, @RequestParam String acctId,
@RequestParam double amount);
如果要传json传到服务端 用map封装:
Map<String, Object> map =new
HashMap<String, Object>();
map.put("data",
"xxx");
map.put("data1",
"xxx1");
Stringdata=
JSON.toJSONString(map);
httpRequest(
requestUrl, requestMethod, data);
服务端编写:
@RequestMapping(value = "/SyncUserInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody public String getDetail(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response)//当然你自己定义有model也可以不用JSONObject 接收;