新增一个maven项目
mvn archetype:generate \
-DgroupId=org.darebeat \
-DartifactId=dataworks \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeCatalog=local \
-DinteractiveMode=false
配置pop.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.darebeat</groupId>
<artifactId>dataworks</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>dataworks</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-noop</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.20</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dataworks-public</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-core</artifactId>
<version>0.37.9-public</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sonatype-nexus-staging</id>
<name>Sonatype Nexus Staging</name>
<url>https://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
JAVA代码实现
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.Arrays;
public class SearchNodes {
public static void main(String[] args) throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();
String uri = "http://baseapi.***.com/v1.0/node/prod?exe cuteMethod=SEARCH&searchText=nodeTest";
HttpUriRequest request = createHttpRequest(
"base key",
"base token", uri
);
request.setHeader("tenant_id", "租户 id");
request.setHeader("base_id", "用户 id");
HttpResponse response = httpClient.execute(request);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder stringBuilder = new StringBuilder();
while(bufferedReader.ready()) {
String line = bufferedReader.readLine(); if (line == null) {
break;
}
stringBuilder.append(line);
}
System.out.println(stringBuilder.toString());
}
private static String byteArrayToHexString(byte[] byteArray) {
final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b','c', 'd', 'e', 'f'};
String result = null;
int l = byteArray.length; char[] str = new char[l << 1]; int k = 0;
for (int i = 0; i < l; i++) {
byte byte0 = byteArray[i];
str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
str[k++] = HEX_DIGITS[byte0 & 0xf];
}
result = new String(str); return result;
}
public static String tokenSign(String param, String salt) t hrows Exception {
if (StringUtils.isEmpty(param) || StringUtils.isEmpty(salt)) {
return null;
}
String query = URLDecoder.decode(param, "UTF-8");
String[] pStr = query.trim().split("&");
Arrays.sort(pStr);
StringBuilder buf = new StringBuilder(); for (int i = 0; i < pStr.length; ++i) {
buf.append(pStr[i]);
buf.append("&");
}
String paramStr = buf.substring(0, buf.length() - 1);
String sourceStr = salt + ":" + paramStr + ":" + salt;
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] byteArray = md.digest(sourceStr.getBytes("UTF-8"));
return byteArrayToHexString(byteArray);
}
protected static HttpUriRequest createHttpRequest(String ba seKey, String baseToken, String uri) {
StringBuilder buf;
int index = uri.indexOf('?');
if (index == -1) {
index = uri.length();
buf = new StringBuilder(uri.length() + baseKey.length() + 13);
buf.append(uri).append('?');
} else {
buf = new StringBuilder(uri.length() + baseKey.length() + 13);
buf.append(uri).append('&');
}
buf.append("baseKey=").append(baseKey);
buf.append("×tamp=").append(System.currentTimeMillis());
HttpUriRequest request = new HttpGet(buf.toString()); String signature;
try {
signature = tokenSign(buf.substring(index + 1), baseToken);
} catch (Exception ex) {
throw new RuntimeException("error on sign for uri: " + uri, ex);
}
request.addHeader("signature", signature);
return request;
}
}