当您在京东云上通过微服务平台创建了自有微服务应用后,可以通过京东云API网关将其发布到公网供客户调用,在此过程中就需要微服务网关来发挥作用。
以下过程将指导您完成在API网关控制台中使用微服务网关作为后端服务,进行微服务API分组调用的过程。
(1)通过Java SDK调用,Java SDK示例如下:
复制成功
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.util.Maps;
import com.jdcloud.sdk.http.Protocol;
import com.jdcloud.sdk.utils.BinaryUtils;
import net.jdcloud.Abs.client.JdcloudSDKClient;
/**
* Demo
*/
public class Main {
public static void main(String[ ] args) throws UnsupportedEncodingException, HttpResponseException {
String assessKey = "ak";//免鉴权时传空
String secretKey = "sk";//免鉴权时传空
String host = "w22bzgeav4by.cn-north-1.jdcloud-api.net";// 域名:不要添加http://或https://
// String host = "w22bzgeav4by-test.cn-north-1.jdcloud-api.net"; // 测试环境地址
// String host = "w22bzgeav4by-preview.cn-north-1.jdcloud-api.net"; // 预发环境地址
String path = "/v1/test";// 请求路径:不要包含query参数,例如 /test?queryParam1=value1&queryParam2=value2,只传/test
String method = "POST";// 请求方式,大写
Map<String, String> headers = Maps.newHashMap();// header
headers.put("header1", "headerValue1");
Map<String, Object> queryMap = Maps.newHashMap();// query参数,可以为空
queryMap.put("key1", "value1");
String body = "{\"title\":\"qq\",\"description\":\"222\"}";// 请求体,可以为空
try {
HttpResponse httpResponse = JdcloudSDKClient.execute(assessKey, secretKey, Protocol.HTTP, host, path, method, headers,
queryMap, body);
System.out.println(BinaryUtils.toByteArray(httpResponse.getContent()));
} catch (HttpResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)通过Python SDK调用,Python SDK示例如下:
复制成功
# coding: utf-8
from jdcloud_apim_sdk.core.credential import Credential
from jdcloud_apim_sdk.core.config import Config
from jdcloud_apim_sdk.core.logger import Logger
from jdcloud_apim_sdk.core.const import SCHEME_HTTPS
from jdcloud_apim_sdk.simpleclient import SimpleClient
from jdcloud_apim_sdk.simplerequest import SimpleRequest
if __name__=="__main__":
access_key = 'ak' # 免鉴权时传空
secret_key = 'sk' # 免鉴权时传空
credential = Credential(access_key, secret_key)
# config = Config('w5f70cfq9ili-test.cn-north-1.jdcloud-api.net', scheme=SCHEME_HTTPS, timeout=60) # 测试环境地址
# config = Config('w5f70cfq9ili-preview.cn-north-1.jdcloud-api.net', scheme=SCHEME_HTTPS, timeout=60) # 预发环境地址
config = Config('w5f07gqcm7ri.cn-north-1.jdcloud-api.net', scheme=SCHEME_HTTPS, timeout=60) # 线上环境地址,不要添加http://或https://
logger = Logger(3)
client = SimpleClient(config, credential, logger)
url = '/todo/api/v1/tasks/createTask/json' # 请求路径:不要包含query参数,例如 /test?queryParam1=value1&queryParam2=value2,只传/test
method = 'POST' # 请求方式,大写
header = dict()
header['header1'] = "headerValue1" # header
parameter = dict()
parameter['queryParam'] = 'this is query param' # query参数,可以为空
body = {"description": "1234567890", "title": "title"} # 请求体,可以为空
request = SimpleRequest(url, method, parameter, body, header)
resp = client.send(request)
print(resp.status_code)
我们的产品专家为您找到最合适的产品/解决⽅案
1v1线上咨询获取售前专业咨询
专业产品顾问,随时随地沟通