mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
路由管理优化
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.SopConstants;
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.retry.ExponentialBackoffRetry;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public class CuratorTest extends TestCase {
|
||||
|
||||
|
||||
private String zookeeperServerAddr = "127.0.0.1:2181";
|
||||
|
||||
/**
|
||||
* 递归删除节点,只能在测试环境用。
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testDel() throws Exception {
|
||||
CuratorFramework client = CuratorFrameworkFactory.builder()
|
||||
.connectString(zookeeperServerAddr)
|
||||
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
|
||||
.build();
|
||||
|
||||
client.start();
|
||||
|
||||
client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH);
|
||||
}
|
||||
}
|
@@ -1,127 +0,0 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class PostTest extends TestBase {
|
||||
|
||||
String url = "http://localhost:8081/zuul";
|
||||
String appKey = "test";
|
||||
String secret = "123456";
|
||||
|
||||
@Test
|
||||
public void testPost() throws Exception {
|
||||
// 业务参数
|
||||
Map<String, String> jsonMap = new HashMap<String, String>();
|
||||
jsonMap.put("id", "1");
|
||||
jsonMap.put("id", "葫芦娃");
|
||||
|
||||
String json = JSON.toJSONString(jsonMap);
|
||||
json = URLEncoder.encode(json, "utf-8");
|
||||
|
||||
// 系统参数
|
||||
Map<String, Object> param = new HashMap<String, Object>();
|
||||
param.put("name", "story.get");
|
||||
param.put("app_key", appKey);
|
||||
param.put("data", json);
|
||||
param.put("timestamp", getTime());
|
||||
param.put("version", "2.0");
|
||||
|
||||
String sign = buildSign(param, secret);
|
||||
|
||||
param.put("sign", sign);
|
||||
|
||||
System.out.println("=====请求数据=====");
|
||||
String postJson = JSON.toJSONString(param);
|
||||
System.out.println(postJson);
|
||||
|
||||
post(url, param); // 发送请求
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 构建签名
|
||||
*
|
||||
* @param paramsMap
|
||||
* 参数
|
||||
* @param secret
|
||||
* 密钥
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String buildSign(Map<String, ?> paramsMap, String secret) throws IOException {
|
||||
Set<String> keySet = paramsMap.keySet();
|
||||
List<String> paramNames = new ArrayList<String>(keySet);
|
||||
|
||||
Collections.sort(paramNames);
|
||||
|
||||
StringBuilder paramNameValue = new StringBuilder();
|
||||
|
||||
for (String paramName : paramNames) {
|
||||
paramNameValue.append(paramName).append(paramsMap.get(paramName));
|
||||
}
|
||||
|
||||
String source = secret + paramNameValue.toString() + secret;
|
||||
|
||||
return md5(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成md5,全部大写
|
||||
*
|
||||
* @param message
|
||||
* @return
|
||||
*/
|
||||
public static String md5(String message) {
|
||||
try {
|
||||
// 1 创建一个提供信息摘要算法的对象,初始化为md5算法对象
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
|
||||
// 2 将消息变成byte数组
|
||||
byte[] input = message.getBytes();
|
||||
|
||||
// 3 计算后获得字节数组,这就是那128位了
|
||||
byte[] buff = md.digest(input);
|
||||
|
||||
// 4 把数组每一字节(一个字节占八位)换成16进制连成md5字符串
|
||||
return byte2hex(buff);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 二进制转十六进制字符串
|
||||
*
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
private static String byte2hex(byte[] bytes) {
|
||||
StringBuilder sign = new StringBuilder();
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
String hex = Integer.toHexString(bytes[i] & 0xFF);
|
||||
if (hex.length() == 1) {
|
||||
sign.append("0");
|
||||
}
|
||||
sign.append(hex.toUpperCase());
|
||||
}
|
||||
return sign.toString();
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
}
|
||||
}
|
@@ -1,121 +0,0 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class SopPostTest extends TestBase {
|
||||
|
||||
|
||||
String url = "http://localhost:8081/zuul";
|
||||
String appKey = "test";
|
||||
String secret = "123456";
|
||||
|
||||
@Test
|
||||
public void testPost() throws Exception {
|
||||
|
||||
// 系统参数
|
||||
Map<String, Object> param = new HashMap<String, Object>();
|
||||
param.put("method", "story.get");
|
||||
param.put("app_key", appKey);
|
||||
param.put("timestamp", getTime());
|
||||
param.put("version", "2.0");
|
||||
|
||||
// 业务参数
|
||||
param.put("id", "1");
|
||||
param.put("name", "葫芦娃");
|
||||
|
||||
String sign = buildSign(param, secret);
|
||||
|
||||
param.put("sign", sign);
|
||||
|
||||
System.out.println("=====请求数据=====");
|
||||
String postJson = JSON.toJSONString(param);
|
||||
System.out.println(postJson);
|
||||
|
||||
post(url, param); // 发送请求
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建签名
|
||||
*
|
||||
* @param paramsMap 参数
|
||||
* @param secret 密钥
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String buildSign(Map<String, ?> paramsMap, String secret) throws IOException {
|
||||
Set<String> keySet = paramsMap.keySet();
|
||||
List<String> paramNames = new ArrayList<String>(keySet);
|
||||
|
||||
Collections.sort(paramNames);
|
||||
|
||||
StringBuilder paramNameValue = new StringBuilder();
|
||||
|
||||
for (String paramName : paramNames) {
|
||||
paramNameValue.append(paramName).append(paramsMap.get(paramName));
|
||||
}
|
||||
|
||||
String source = secret + paramNameValue.toString() + secret;
|
||||
|
||||
return md5(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成md5,全部大写
|
||||
*
|
||||
* @param message
|
||||
* @return
|
||||
*/
|
||||
public static String md5(String message) {
|
||||
try {
|
||||
// 1 创建一个提供信息摘要算法的对象,初始化为md5算法对象
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
|
||||
// 2 将消息变成byte数组
|
||||
byte[] input = message.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
// 3 计算后获得字节数组,这就是那128位了
|
||||
byte[] buff = md.digest(input);
|
||||
|
||||
// 4 把数组每一字节(一个字节占八位)换成16进制连成md5字符串
|
||||
return byte2hex(buff);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 二进制转十六进制字符串
|
||||
*
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
private static String byte2hex(byte[] bytes) {
|
||||
StringBuilder sign = new StringBuilder();
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
String hex = Integer.toHexString(bytes[i] & 0xFF);
|
||||
if (hex.length() == 1) {
|
||||
sign.append("0");
|
||||
}
|
||||
sign.append(hex.toUpperCase());
|
||||
}
|
||||
return sign.toString();
|
||||
}
|
||||
|
||||
public String getTime() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
}
|
||||
|
||||
}
|
@@ -1,102 +0,0 @@
|
||||
package com.gitee.sop.gateway;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public class TestBase extends TestCase {
|
||||
public void post(String url, String postJson) throws IOException {
|
||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
||||
HttpPost post = new HttpPost(url);
|
||||
// 构造消息头
|
||||
|
||||
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
|
||||
// 构建消息实体
|
||||
StringEntity entity = new StringEntity(postJson, Charset.forName("UTF-8"));
|
||||
entity.setContentEncoding("UTF-8");
|
||||
// 发送Json格式的数据请求
|
||||
entity.setContentType("application/json");
|
||||
post.setEntity(entity);
|
||||
|
||||
HttpResponse response = httpClient.execute(post);
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
String content = IOUtils.toString(responseEntity.getContent(), "UTF-8");
|
||||
System.out.println(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送POST请求
|
||||
* @param url
|
||||
* @return JSON或者字符串
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Object post(String url, Map<String, Object> params) throws Exception{
|
||||
CloseableHttpClient client = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try{
|
||||
/**
|
||||
* 创建一个httpclient对象
|
||||
*/
|
||||
client = HttpClients.createDefault();
|
||||
/**
|
||||
* 创建一个post对象
|
||||
*/
|
||||
HttpPost post = new HttpPost(url);
|
||||
List<NameValuePair> nameValuePairs = params.entrySet().stream().map(entry -> {
|
||||
return new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
|
||||
}).collect(Collectors.toList());
|
||||
/**
|
||||
* 包装成一个Entity对象
|
||||
*/
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
|
||||
/**
|
||||
* 设置请求的内容
|
||||
*/
|
||||
post.setEntity(entity);
|
||||
/**
|
||||
* 设置请求的报文头部的编码
|
||||
*/
|
||||
post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
|
||||
/**
|
||||
* 执行post请求
|
||||
*/
|
||||
response = client.execute(post);
|
||||
/**
|
||||
* 通过EntityUitls获取返回内容
|
||||
*/
|
||||
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
|
||||
System.out.println(result);
|
||||
return result;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
IOUtils.closeQuietly(client);
|
||||
IOUtils.closeQuietly(response);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user