'client'에 해당되는 글 7건

  1. 2021.12.07 [Elasticsearch] LLRC + Springboot 성능 튜닝 팁
  2. 2020.04.21 [Spring] Spring Cloud Config Client 구성 시 주의점
  3. 2018.06.27 [Elasticsearch] Move TransportClient to High Level REST Client
  4. 2018.04.03 [Elasticsearch] JSON 스트링으로 질의요청
  5. 2017.03.09 [Elasticsearch] TransportClient on 5.x
  6. 2017.02.06 [HttpClient] javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name
  7. 2013.04.05 [elasticsearch] Java API : Client

[Elasticsearch] LLRC + Springboot 성능 튜닝 팁

Elastic/Elasticsearch 2021. 12. 7. 09:14

대부분의 성능 이슈는 서버 엔진 보다는 클라이언트 단에서 사용을 잘 못 하는 경우가 많이 있습니다.

 

Low Level Rest Client 와 Springboot 조합으로 API 개발 시 튜닝 요소를 조금 정리 합니다.

나중에 또 기억 못할 것 같으니...

 

RestClientBuilder 에 보면 아래와 같이 기본 설정이 되어 있습니다.

public static final int DEFAULT_MAX_CONN_PER_ROUTE = 10;
public static final int DEFAULT_MAX_CONN_TOTAL = 30;

이 기본 값으로 그냥 사용하게 되면 너무 리소스를 제한적으로 사용하기 때문에 성능이 제대로 나오지 않게 됩니다.

해당 값을 적절하게 튜닝을 하셔야 하는데 모든 케이스에 다 적용 가능한 부분은 아니지만 그래도 가늠 할 수 있는 기준 정도로는 사용이 가능 할 것 같아 공유 합니다.

분석 결과는 Core 1 개당 setMaxConnPerRoute 설정 시 25 개씩이 최적 값으로 보입니다.
4 core 짜리면 4 x 25 = setMaxConnPerRoute(100)

 

아래는 실제 코드 내부에 작성 되어 있는 코멘트를 보여 드리기 위해 캡쳐 했습니다.

HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build())
  //default settings for connection pooling may be too constraining
  .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE).setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL)

 

코드에서도 동일하게 기본 설정은 너무 제한적일 수 있다고 되어 있습니다.

 

Embedded Tomcat 에서의 기본 Max Connection 은 8192 개 입니다.

Tomcat 과 HttpClient 그리고 Elasitcsearch 에 대한 각각의 Connection, Thread Count 를 잘 조정 하셔야 성능을 최적화 할 수 있습니다.

 

추가적으로 Connection 과 Route 의 비율은 10:1 정도가 적절해 보입니다.

 

아래는 Tomcat 기본 설정 내용입니다.

- Embedded Tocmat 의 설정 중
    acceptCount 는 기본 100 개 이며 이 설정은 maxConnection 에 다다랐을 때 OS 레벨에서 큐잉 하게 되는 값 입니다.
    maxConnections 는 기본 8192 개 이며 NIO/NIO2 를 사용 하며, -1 로 설정 시 카운팅 하지 않습니다. (unlimited)
    maxThreads 는 기본 200 개 이며 connection 당 생성 가능한 최대 thread 수 입니다.
    BIO 일 경우 maxConnections 와 maxThreads 값은 같아야 합니다.


정답은 없으나 시스템 리소스 상황에 맞춰서 최적 값을 찾아 내는게 제일 중요 합니다.
위에 설정 방식이나 값이 최적 값이 아니며 상황에 맞춘 최적 값이고 다른 환경에서는 튜닝 포인트가 된다고 보는게 좋을 것 같습니다.

시스템의 ulimit 설정을 꼭 확인 하고 사용하는 stack 의 default 값도 꼭 확인 하고 사용 합시다.

성능 최적화를 위해 함께 살펴 봐야 하는 소스 코드는 

Java NIO, Executor
Tomcat Connector
Http Client (Components)
Elasticsearch RestClient

 

:

[Spring] Spring Cloud Config Client 구성 시 주의점

ITWeb/개발일반 2020. 4. 21. 16:35

주의점 이라기 보다 그냥 설정을 잘 못 하는 경우가 있어서 기록 합니다.

 

Client 쪽 Dependency 등록)

implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

 

Client 쪽 bootstrap.yml 등록)

spring:
  cloud:
    config:
      uri: http://${CONFIG_SERVER_HOST}:${PORT}

management:
  endpoints:
    web:
      exposure:
        include: refresh

 

Refresh 호출)

- HTTP Method : POST

- http://${CONFIG_CLIENT_HOST}:${PORT}/actuator/refresh

 

bootstrap.yml 에 등록 하지 않게 되면 client 가 config server 를 localhost:8888 로 찾는 오류가 발생 할 수 있습니다.

또한 refresh 등록을 해줘야 actuator/refresh 사용이 가능 합니다.

 

:

[Elasticsearch] Move TransportClient to High Level REST Client

Elastic/Elasticsearch 2018. 6. 27. 11:46

이번에 새로 Indexer 모듈을 만들다 보니 아래 문서의 내용을 확인하게 되었습니다.


Reference)

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html

https://github.com/elastic/elasticsearch/tree/master/client/rest/src/test/java/org/elasticsearch/client


원문발췌)

We plan on deprecating the TransportClient in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests. The migration guidedescribes all the steps needed to migrate.

The Java High Level REST Client currently has support for the more commonly used APIs, but there are a lot more that still need to be added. You can help us prioritise by telling us which missing APIs you need for your application by adding a comment to this issue: Java high-level REST client completeness.

Any missing APIs can always be implemented today by using the low level Java REST Client with JSON request and response bodies.


사실 저도 TransportClient 를 사용하면 version 때문에 고민을 하긴했었는데요.

이 참에 그냥 REST Client 로 넘어 가야 겠다고 생각해서 새로 작성 하였습니다.


기본적인 예제 코드는 아래 내용 참고 하시면 됩니다.


public static void main(String[] args) {
requestRestClient();
}

public static void requestRestClient() {
int numNodes = 5; //randomIntBetween(1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
hosts[i] = new HttpHost("localhost", 9200);
}
RestClientBuilder builder = RestClient.builder(hosts);
RestClient restClient = builder.build();

builder.setMaxRetryTimeoutMillis(10000);

builder.setFailureListener(new RestClient.FailureListener() {
@Override
public void onFailure(HttpHost host) {
System.out.println("onFailure");
}
});

builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(
RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder.setSocketTimeout(10000);
}
});

Map<String, String> params = new HashMap<>();
params.put("preference", "_shards:0,1,2,3");

HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(
30 * 1024 * 1024);
try {
Response response = restClient
.performRequest("GET", "/hello/_search", params, null, consumerFactory);

System.out.println(response.toString());

// Read Case 1
RequestLine requestLine = response.getRequestLine();
HttpHost host = response.getHost();
int statusCode = response.getStatusLine().getStatusCode();
Header[] headers = response.getHeaders();
String responseBody = EntityUtils.toString(response.getEntity());

System.out.println(requestLine);
System.out.println(host);
System.out.println(statusCode);
System.out.println(headers);
System.out.println(responseBody);

// Read Case 2 - old fashion
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));

String inputLine;
StringBuffer buffer = new StringBuffer();

while ((inputLine = reader.readLine()) != null) {
buffer.append(inputLine);
}

reader.close();

// System.out.println(buffer.toString());
} catch (IOException e) {
e.printStackTrace();
}

try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static int randomIntBetween(int min, int max) {
return RandomNumbers.randomIntBetween(random(), min, max);
}

public static Random random() {
return RandomizedContext.current().getRandom();
}




:

[Elasticsearch] JSON 스트링으로 질의요청

Elastic/Elasticsearch 2018. 4. 3. 13:41

예전에도 JSON 스트링을 가지고 setSource() 함수로 질의 했었는데요.

필요하신 분들이 계신것 같아 쉬운 샘플 코드 올려 봅니다.


[Source Code]

package org.elasticsearch.action.search;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class HelloSearchSourceBuilder {

public static void main (String[] args) {
Client client;
client = new PreBuiltTransportClient(Settings.EMPTY);

SearchRequestBuilder searchRequestBuilder = client.prepareSearch();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("field1","value1");

System.out.println(termQueryBuilder.toString());

searchSourceBuilder.query(termQueryBuilder).from(0).size(10);

System.out.println(searchSourceBuilder.toString());

searchRequestBuilder.setSource(searchSourceBuilder);

System.out.println(searchRequestBuilder.toString());
}
}


[Output]

[2018-04-03T13:36:38,466][INFO ][o.e.p.PluginsService     ] [_client_] no modules loaded

[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService     ] [_client_] loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]

[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService     ] [_client_] loaded plugin [org.elasticsearch.join.ParentJoinPlugin]

[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService     ] [_client_] loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]

[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService     ] [_client_] loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]

[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService     ] [_client_] loaded plugin [org.elasticsearch.transport.Netty4Plugin]

{

  "term" : {

    "field1" : {

      "value" : "value1",

      "boost" : 1.0

    }

  }

}

{"from":0,"size":10,"query":{"term":{"field1":{"value":"value1","boost":1.0}}}}

{"from":0,"size":10,"query":{"term":{"field1":{"value":"value1","boost":1.0}}}}


Process finished with exit code 0



:

[Elasticsearch] TransportClient on 5.x

Elastic/Elasticsearch 2017. 3. 9. 11:51

elasticsearch 2.4 에서 사용하던 java api 중 TransportClinet 사용 방법이 바뀌어서 작성 합니다.

변경된 내용에 대해서는 elasticsearch 공식 홈페이지에 자세히 나와 있습니다.


[참고문서]

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_maven_repository.html

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html


[코드 변경]


2.x)

settings = settingsBuilder()

  .put("cluster.name", cluster)

  .put("client.transport.sniff", true)

  .put("network.tcp.blocking", false)           // tcp non-blocking mode

  .put("client.transport.ping_timeout", "10s")

  .build();


5.x)

settings = builder()

  .put("cluster.name", cluster)

  .put("client.transport.sniff", true)

  .put("network.tcp.blocking", false)           // tcp non-blocking mode

  .put("client.transport.ping_timeout", "10s")

  .build();


2.x)

TransportClient client = TransportClient.builder().settings(settings).build();


5.x)

TransportClient client = new PreBuiltTransportClient(settings);


여기서 주의 하실 점은 참고문서에 있지만  transport 가 분리 되었기 때문에 별도로 dependency 구성을 해주셔야 합니다.


Maven Dependency 추가)

<dependency>

  <groupId>org.elasticsearch.client</groupId>

  <artifactId>transport</artifactId>

  <version>${elasticsearch.version}</version>

</dependency>


별 내용은 아니지만 혹시라도 삽질 하시는 분들이 계실 수 있어 작성해 봤습니다.

:

[HttpClient] javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name

ITWeb/개발일반 2017. 2. 6. 11:33

http 통신 기능 구현 시 아래와 같은 에러를 접하는 경우가 있어서 해결 방법을 기록해 봅니다.


[에러내용]

javax.net.ssl.SSLProtocolException: handshake alert:  unrecognized_name

    at sun.security.ssl.ClientHandshaker.handshakeAlert(ClientHandshaker.java:1441)

    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2016)

    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)

    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)

    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)

    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)

    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)

    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)

    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)



[해결방법]

JVM 실행 시 옵션 추가)

-Djsse.enableSNIExtension=false


Application 내부에서 설정 추가)

System.setProperty(“jsse.enableSNIExtension”, “false”);

:

[elasticsearch] Java API : Client

Elastic/Elasticsearch 2013. 4. 5. 19:01

본 문서는 개인적인 테스트와 elasticsearch.org 그리고 community 등을 참고해서 작성된 것이며,

정보 교환이 목적입니다.


잘못된 부분에 대해서는 지적 부탁 드립니다.

(예시 코드는 성능 및 보안 검증이 되지 않았습니다.)



[elasticsearch java api 리뷰]

원문 링크 : http://www.elasticsearch.org/guide/reference/java-api/client/


Java API 를 이용해서 elasticsearch 로 연결하는 두 가지 방법을 설명 하고 있습니다.

1. Node Client

2. Transport Client


※ 커뮤니티에서 관련 글들을 찾다 보면 kimchy 가 추천 하는 방식은 "TransportClient" 를 이용하는 것입니다.


원문에서 보면 elasticsearch 를 사용하면 주의 해야 하는 부분이 나와 있습니다.

사용하시면서 꼭 숙지 하시면 한번 쯤 실수 할 수 있는 오류를 범하지 않을 수 있습니다.

Important:

Please note that you are encouraged to use the same version on client and cluster sides. You may hit some incompatibilities issues when mixing major versions.



※ 요점은 버전을 혼합해서 사용하지 말라는 것입니다. (No Node Available Exception 의 이유가 됩니다.)


원문에 예제 코드가 잘 나와 있기 때문에 제가 테스트 한 코드를 아래 공유 합니다.

ClientConfig clientConfig = ClientConfig.getInstance();


// cluster.name 을 안 줄 경우 NoNodeAvailable 에러 발생.

settings = ImmutableSettings

        .settingsBuilder()

        .put("cluster.name", clientConfig.getProperty("cluster.name"))

        .build();

client = new TransportClient(settings)

        .addTransportAddress(new InetSocketTransportAddress(clientConfig.getProperty("cluster.host1")

                , Integer.parseInt(clientConfig.getProperty("cluster.host1.port"))))

        .addTransportAddress(new InetSocketTransportAddress(clientConfig.getProperty("cluster.host2")

                , Integer.parseInt(clientConfig.getProperty("cluster.host2.port"))));


- ClientConfig : elasticsearch 를 사용하기 위해 필요한 환경설정 값을 구성


elasticsearch client 가 가지는 properties 와 attributes 테스트를 위한 코드

@Test

public void testClientProperties() throws Exception {

    clientManager.openClient();

    Client client = clientManager.getClient();

    AdminClient admin = client.admin();

    ClusterAdminClient cluster = admin.cluster();

    IndicesAdminClient indices = admin.indices();


    ClusterStateRequestBuilder request = cluster.prepareState();

    ClusterStateResponse response = request.execute().actionGet();  

    ClusterState state = response.state();

    AllocationExplanation allocation = state.allocationExplanation();

    ClusterBlocks block = state.blocks();

    MetaData meta = state.metaData();

    RoutingNodes routingNode = state.routingNodes();

    RoutingTable routingTable = state.routingTable();

    ImmutableSettings settings = (ImmutableSettings) meta.persistentSettings();

    

    log.debug("{}", response.clusterName()); // Cluster [test.gruter.es.cluster]

    log.debug("{}", response.getClusterName());

    log.debug("{}", response.getHeaders());

    log.debug("{}", response.getState());

    log.debug("{}", response.state());

    

    log.debug("{}", state.allocationExplanation());

    log.debug("{}", state.blocks());

    log.debug("{}", state.metaData());

    log.debug("{}", state.nodes()); // {[test.gruter.es.cluster2.node][sIj29f-KQziaIbccn_jndg][inet[/127.0.0.1:9301]]{master=true},[test.gruter.es.cluster1.node][kWNDF8SLTxS_axatGmImIg][inet[/127.0.0.1:9300]]{master=true},}

    log.debug("{}", state.routingNodes());

    log.debug("{}", state.routingTable());

    log.debug("{}", state.version());

    

    log.debug("{}", allocation.explanations());

    

    log.debug("{}", block.indices());

    

    log.debug("{}", meta.concreteAllIndices());

    log.debug("{}", meta.concreteAllOpenIndices());

    log.debug("{}", meta.getTotalNumberOfShards());

    log.debug("{}", meta.persistentSettings());

    log.debug("{}", meta.settings());

    

    log.debug("{}", routingNode.getMetaData());

    log.debug("{}", routingNode.getNodesToShards());

    log.debug("{}", routingNode.getRoutingTable());

    log.debug("{}", routingNode.getUnassigned());

    log.debug("{}", routingNode.prettyPrint());

    

    log.debug("{}", routingTable.getIndicesRouting());

    log.debug("{}", routingTable.prettyPrint());

    

    log.debug("{}", settings.getAsMap());

    log.debug("{}", state.nodes().dataNodes());

    log.debug("{}", state.nodes().getSize());

   

    for ( int i=0; i<state.nodes().getSize(); i++ ) {

        log.debug("{}", state.nodes().dataNodes().keySet().toArray()[i]);

    }

    

    Set entries = state.nodes().dataNodes().entrySet();

    Iterator entryIter = entries.iterator();


    while (entryIter.hasNext()) {

        Map.Entry entry = (Map.Entry)entryIter.next();

        Object key = entry.getKey();

        Object value = entry.getValue();

       

        log.debug("key : {}", key);

        log.debug("value : {}", value);

    }

} 

※ 단순히 테스트 및 각 속성들을 보기 위한 코드 입니다.

: