用户工具

站点工具


light4j:consumer

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

后一修订版
前一修订版
light4j:consumer [2020/07/01 14:38]
admin 创建
light4j:consumer [2020/07/01 23:41] (当前版本)
admin
行 22: 行 22:
   - com.networknt.cluster.LightCluster   - com.networknt.cluster.LightCluster
 </​code>​ </​code>​
 +
 +  * HttpClientBuilder:基于Http2Client
 +<​code>​
 +Future<​ClientResponse>​ clientRequest = new HttpClientBuilder()
 +        .setApiHost("​https://​localhost:​8453"​) //​支持直连、服务发现
 +        //​.setServiceDef(new ServiceDef("​https",​ "​training.customers-1.00.00","​training",​ null))
 +        .setClientRequest(new ClientRequest().setPath("/​v1/​customers/​1"​).setMethod(Methods.GET))
 +        .setLatch(new CountDownLatch(1))
 +        .setAuthToken(""​) ​ //​优先于CcToken,都是设置Headers.AUTHORIZATION,用于外部OAuth2授权应用,
 +        .disableHttp2()
 +        .addCCToken() ​ //​Http2Client.addCcToken,Client Credentials token从TokenManager获取,用于应用内部
 +        .setConnectionRequestTimeout(new TimeoutDef(100,​ TimeUnit.SECONDS))
 +        .setRequestTimeout(new TimeoutDef(100,​ TimeUnit.SECONDS))
 +        .setRequestBody(""​)
 +        .setConnectionCacheTTLms(10000)
 +        .setMaxReqCount(5)
 +        .send();
 +ClientResponse clientResponse = clientRequest.get();​
 +</​code>​
 +  * LightRestClient:基于HttpClientBuilder,RestClientOptions配置参数
 +<​code>​
 +LightRestClient lightRestClient = new LightRestClient();​
 +String requestStr = "​{\"​selection\":​{\"​accessCard\":​\"​22222222\",​\"​selectID\":​10009,​\"​crossReference\":​{\"​externalSystemID\":​226,​\"​referenceType\":​2,​\"​ID\":​\"​122222\"​}}}";​
 +Map<​String,​ String> headerMap = new HashMap<>​();​
 +headerMap.put("​Content-Type",​ "​application/​json"​);​
 +headerMap.put("​Transfer-Encoding",​ "​chunked"​);​ //​也支持ServiceDef
 +Map resultMap = lightRestClient.post("​https://​localhost:​8467",​ "/​networknt/​select/", ​ Map.class, headerMap, requestStr);​
 +</​code>​
 +  * Http2ServiceRequest:ActiveRecord模式,多个请求的future结果可以join
 +<​code>​
 +ServiceDef serviceDef = new ServiceDef("​https",​ "​com.networknt.apia.selection-1.00.00",​ null, null);
 +cluster.serviceToUrl(serviceDef.getProtocol(),​ serviceDef.getServiceId(),​ serviceDef.getEnvironment(),​ serviceDef.getRequestKey()) //​serviceDef转换url
 +Http2ServiceRequest request1 = Http2ServiceRequest(serviceDef,​ "/​get",​ HttpVerb.GET);​
 +CompletableFuture<​Map>​ futureResponse1 = request1.callForTypedObject(Map.class);​
 +CompletableFuture.allOf(futureResponse1,​ futureResponse2).join();​
 +</​code>​
 +
light4j/consumer.1593585497.txt.gz · 最后更改: 2020/07/01 14:38 由 admin