用户工具

站点工具


light4j:consumer
  • light-consumer-4j
<dependency>
    <groupId>com.networknt</groupId>
    <artifactId>light-consumer-4j</artifactId>
</dependency>
  • service
- com.networknt.registry.URL:
  - com.networknt.registry.URLImpl:
      parameters:
        registryRetryPeriod: '30000'
- com.networknt.consul.client.ConsulClient:
  - com.networknt.consul.client.ConsulClientImpl
- com.networknt.registry.Registry:
  - com.networknt.consul.ConsulRegistry
- com.networknt.balance.LoadBalance:
  - com.networknt.balance.RoundRobinLoadBalance
- com.networknt.cluster.Cluster:
  - com.networknt.cluster.LightCluster
  • HttpClientBuilder:基于Http2Client
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();
  • LightRestClient:基于HttpClientBuilder,RestClientOptions配置参数
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);
  • Http2ServiceRequest:ActiveRecord模式,多个请求的future结果可以join
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();
light4j/consumer.txt · 最后更改: 2020/07/01 23:41 由 admin