parent
d1f33d4e19
commit
2138ff6fda
@ -0,0 +1,32 @@ |
||||
package com.gitee.sop.gatewaycommon.bean; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public abstract class AbstractTargetRoute<R extends BaseServiceRouteInfo, E extends BaseRouteDefinition, T> implements TargetRoute<R,E, T> { |
||||
|
||||
private R serviceRouteInfo; |
||||
private E routeDefinition; |
||||
private T targetRoute; |
||||
|
||||
public AbstractTargetRoute(R serviceRouteInfo, E routeDefinition, T targetRoute) { |
||||
this.serviceRouteInfo = serviceRouteInfo; |
||||
this.routeDefinition = routeDefinition; |
||||
this.targetRoute = targetRoute; |
||||
} |
||||
|
||||
@Override |
||||
public R getServiceRouteInfo() { |
||||
return serviceRouteInfo; |
||||
} |
||||
|
||||
@Override |
||||
public E getRouteDefinition() { |
||||
return routeDefinition; |
||||
} |
||||
|
||||
@Override |
||||
public T getTargetRouteDefinition() { |
||||
return targetRoute; |
||||
} |
||||
} |
@ -1,29 +0,0 @@ |
||||
package com.gitee.sop.gatewaycommon.bean; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
@Data |
||||
public class ServiceApiInfo { |
||||
private String md5; |
||||
private String appName; |
||||
private List<ApiMeta> apis; |
||||
|
||||
@Data |
||||
public static class ApiMeta { |
||||
/** 接口名 */ |
||||
private String name; |
||||
/** 请求path */ |
||||
private String path; |
||||
/** 版本号 */ |
||||
private String version; |
||||
|
||||
public String fetchNameVersion() { |
||||
return name + version; |
||||
} |
||||
} |
||||
} |
@ -1,38 +0,0 @@ |
||||
package com.gitee.sop.gatewaycommon.bean; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import java.util.function.Consumer; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public abstract class ServiceRouteRepository<R, E> { |
||||
/** |
||||
* key:serviceId |
||||
*/ |
||||
private Map<String, Set<E>> serviceRouteMap = new ConcurrentHashMap<>(); |
||||
|
||||
public abstract String getServiceId(R r); |
||||
|
||||
public void saveRouteDefinition(R serviceRouteInfo, E definition) { |
||||
String serverId = getServiceId(serviceRouteInfo); |
||||
Set<E> routeDefinitionSet = serviceRouteMap.putIfAbsent(serverId, new HashSet<>(16)); |
||||
if (routeDefinitionSet == null) { |
||||
routeDefinitionSet = serviceRouteMap.get(serverId); |
||||
} |
||||
routeDefinitionSet.add(definition); |
||||
} |
||||
|
||||
public synchronized void deleteAll(R serviceRouteInfo, Consumer<E> consumer) { |
||||
String serverId = getServiceId(serviceRouteInfo); |
||||
Set<E> definitionSet = serviceRouteMap.getOrDefault(serverId, Collections.emptySet()); |
||||
for (E routeDefinition : definitionSet) { |
||||
consumer.accept(routeDefinition); |
||||
} |
||||
definitionSet.clear(); |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
package com.gitee.sop.gatewaycommon.bean; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public interface TargetRoute<R extends BaseServiceRouteInfo, E extends BaseRouteDefinition, T> { |
||||
R getServiceRouteInfo(); |
||||
|
||||
E getRouteDefinition(); |
||||
|
||||
T getTargetRouteDefinition(); |
||||
} |
@ -1,31 +0,0 @@ |
||||
package com.gitee.sop.gatewaycommon.gateway.route; |
||||
|
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
import lombok.ToString; |
||||
import org.springframework.util.LinkedMultiValueMap; |
||||
import org.springframework.util.MultiValueMap; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@ToString |
||||
public class GatewayContext { |
||||
|
||||
public static final String CACHE_GATEWAY_CONTEXT = "cacheGatewayContext"; |
||||
|
||||
/** |
||||
* cache json body |
||||
*/ |
||||
private String cacheBody; |
||||
/** |
||||
* cache formdata |
||||
*/ |
||||
private MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); |
||||
/** |
||||
* cache reqeust path |
||||
*/ |
||||
private String path; |
||||
} |
@ -0,0 +1,13 @@ |
||||
package com.gitee.sop.gatewaycommon.gateway.route; |
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.AbstractTargetRoute; |
||||
import org.springframework.cloud.gateway.route.RouteDefinition; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class GatewayTargetRoute extends AbstractTargetRoute<GatewayServiceRouteInfo, GatewayRouteDefinition, RouteDefinition> { |
||||
public GatewayTargetRoute(GatewayServiceRouteInfo baseServiceRouteInfo, GatewayRouteDefinition baseRouteDefinition, RouteDefinition targetRoute) { |
||||
super(baseServiceRouteInfo, baseRouteDefinition, targetRoute); |
||||
} |
||||
} |
@ -1,27 +0,0 @@ |
||||
package com.gitee.sop.gatewaycommon.manager; |
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.BaseRouteDefinition; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class RouteDefinitionItemContext { |
||||
// key:id
|
||||
private static Map<String, BaseRouteDefinition> routeDefinitionMap = new HashMap<>(64); |
||||
|
||||
public static void add(BaseRouteDefinition routeDefinition) { |
||||
routeDefinitionMap.put(routeDefinition.getId(), routeDefinition); |
||||
} |
||||
|
||||
public static BaseRouteDefinition getRouteDefinition(String id) { |
||||
return routeDefinitionMap.get(id); |
||||
} |
||||
|
||||
public static void delete(BaseRouteDefinition routeDefinition) { |
||||
routeDefinitionMap.remove(routeDefinition.getId()); |
||||
} |
||||
|
||||
} |
@ -1,14 +1,40 @@ |
||||
package com.gitee.sop.gatewaycommon.manager; |
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.TargetRoute; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public interface RouteRepository<R, T> { |
||||
public interface RouteRepository<T extends TargetRoute> { |
||||
/** |
||||
* 获取路由信息 |
||||
* @param id 路由id |
||||
* @return |
||||
*/ |
||||
T get(String id); |
||||
|
||||
String add(R serviceRouteInfo, T route); |
||||
/** |
||||
* 添加路由 |
||||
* @param targetRoute |
||||
* @return |
||||
*/ |
||||
String add(T targetRoute); |
||||
|
||||
/** |
||||
* 更新路由 |
||||
* @param targetRoute |
||||
*/ |
||||
void update(T targetRoute); |
||||
|
||||
/** |
||||
* 删除路由 |
||||
* @param id 路由id |
||||
*/ |
||||
void delete(String id); |
||||
|
||||
void deleteAll(R serviceRouteInfo); |
||||
/** |
||||
* 删除service下的所有路由 |
||||
* @param serviceId |
||||
*/ |
||||
void deleteAll(String serviceId); |
||||
} |
||||
|
@ -0,0 +1,19 @@ |
||||
package com.gitee.sop.gatewaycommon.manager; |
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.TargetRoute; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class RouteRepositoryContext { |
||||
private static RouteRepository<? extends TargetRoute> routeRepository; |
||||
|
||||
public static RouteRepository<? extends TargetRoute> getRouteRepository() { |
||||
return routeRepository; |
||||
} |
||||
|
||||
public static <T extends TargetRoute> void setRouteRepository(RouteRepository<T> routeRepository) { |
||||
RouteRepositoryContext.routeRepository = routeRepository; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,16 @@ |
||||
package com.gitee.sop.gatewaycommon.zuul.route; |
||||
|
||||
import com.gitee.sop.gatewaycommon.bean.AbstractTargetRoute; |
||||
import lombok.Getter; |
||||
import org.springframework.cloud.netflix.zuul.filters.Route; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
@Getter |
||||
public class ZuulTargetRoute extends AbstractTargetRoute<ZuulServiceRouteInfo, ZuulRouteDefinition, Route> { |
||||
|
||||
public ZuulTargetRoute(ZuulServiceRouteInfo baseServiceRouteInfo, ZuulRouteDefinition baseRouteDefinition, Route targetRoute) { |
||||
super(baseServiceRouteInfo, baseRouteDefinition, targetRoute); |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue