用户工具

站点工具


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();
  • direct
singletons:
- com.networknt.registry.URL:
  - com.networknt.registry.URLImpl:
      protocol: light
      host: localhost
      port: 8080
      path: direct
      parameters:
        com.xlongwei.light4j: http://localhost:8080
- com.networknt.registry.Registry:
  - com.networknt.registry.support.DirectRegistry
  • Http2Client
        String serviceToUrl = cluster.serviceToUrl("http", "com.xlongwei.light4j", null, null);
        ClientConnection connection = client
                .connect(new URI(serviceToUrl), Http2Client.WORKER, null, Http2Client.BUFFER_POOL, OptionMap.EMPTY)
                .get();
        ClientRequest clientRequest = new ClientRequest().setMethod(Methods.POST).setPath("/service/apijson/get");
        clientRequest.getRequestHeaders().put(Headers.HOST, "localhost");
        clientRequest.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        AtomicReference<ClientResponse> responseReference = new AtomicReference<>();
        CountDownLatch latch = new CountDownLatch(1);
        final String body = "{\"Province[]\":{\"Province\":{},\"count\":10,\"page\":2}}";
        connection.sendRequest(clientRequest, client.createClientCallback(responseReference, latch, body));
        latch.await(10000, TimeUnit.MILLISECONDS);
        ClientResponse clientResponse = responseReference.get();
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
        if (clientResponse != null && clientResponse.getResponseCode() == 200) {
            String json = clientResponse.getAttachment(Http2Client.RESPONSE_BODY);
            exchange.getResponseSender().send(json);
        }
light4j/consumer.txt · 最后更改: 2021/05/22 19:19 由 admin