fuweng11 commented on code in PR #9057:
URL: https://github.com/apache/inlong/pull/9057#discussion_r1524524352


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/es/ElasticsearchConfig.java:
##########
@@ -56,54 +58,84 @@ public class ElasticsearchConfig {
     private String password;
 
     /**
-     * highLevelClient
+     *  Get http rest client.
      *
-     * @return RestHighLevelClient
+     * @return springframework RestTemplate
      */
-    public RestHighLevelClient highLevelClient() {
-        if (highLevelClient != null) {
-            return highLevelClient;
+    public RestTemplate getRestClient() {
+        if (restTemplate != null) {
+            return restTemplate;
         }
         try {
-            synchronized (RestHighLevelClient.class) {
-                if (highLevelClient == null) {
-                    List<HttpHost> hosts = new ArrayList<>();
-                    String[] hostArrays = 
this.hosts.split(InlongConstants.SEMICOLON);
-                    for (String host : hostArrays) {
-                        if (StringUtils.isNotBlank(host)) {
-                            host = host.trim();
-                            hosts.add(HttpHost.create(host));
-                        }
-                    }
-                    RestClientBuilder clientBuilder = 
RestClient.builder(hosts.toArray(new HttpHost[0]));
-                    this.setEsAuth(clientBuilder);
-                    highLevelClient = new RestHighLevelClient(clientBuilder);
+            synchronized (RestTemplate.class) {
+                if (restTemplate == null) {
+                    restTemplate = new RestTemplate();
                 }
             }
         } catch (Exception e) {
             logger.error("get es high level client error", e);
         }
-        return highLevelClient;
+        return restTemplate;
     }
 
     /**
-     * Elasticsearch authentication
+     * Get http hosts.
      *
-     * @param builder The builder
+     * @return list of http host info
      */
-    private void setEsAuth(RestClientBuilder builder) {
+    public List<HttpHost> getHttpHosts() {
+        if (httpHosts != null) {
+            return httpHosts;
+        }
         try {
-            logger.info("set es auth of enable={}", authEnable);
-            if (authEnable) {
-                final CredentialsProvider credentialsProvider = new 
BasicCredentialsProvider();
-                credentialsProvider.setCredentials(AuthScope.ANY, new 
UsernamePasswordCredentials(username, password));
-                builder.setHttpClientConfigCallback(
-                        httpClientBuilder -> 
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
-
+            synchronized (HttpHost.class) {
+                httpHosts = new ArrayList<>();
+                String[] hostArrays = 
this.hosts.split(InlongConstants.SEMICOLON);
+                for (String host : hostArrays) {
+                    if (StringUtils.isNotBlank(host)) {
+                        host = host.trim();
+                        httpHosts.add(HttpHost.create(host));
+                    }
+                }
             }
         } catch (Exception e) {
-            logger.error("set es auth error ", e);
+            logger.error("get es http hosts error", e);
+        }
+        return httpHosts;
+    }
+
+    /**
+     * Get one http url.
+     *
+     * @return a http url
+     * @throws Exception any exception if occurred
+     */
+    public String getOneHttpUrl() throws Exception {
+        getHttpHosts();
+        if (!httpHosts.isEmpty() && httpHosts.size() > 0) {
+            return httpHosts.get(rand.nextInt(httpHosts.size())).toString();
+        } else {
+            throw new Exception("http hosts is empty! please check hosts!");
         }

Review Comment:
   ```suggestion
           getHttpHosts();
           if (!httpHosts.isEmpty() && httpHosts.size() > 0) {
               return httpHosts.get(rand.nextInt(httpHosts.size())).toString();
           }
           throw new Exception("http hosts is empty! please check hosts!");
           
   ```



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/es/ElasticsearchConfig.java:
##########
@@ -56,54 +58,84 @@ public class ElasticsearchConfig {
     private String password;
 
     /**
-     * highLevelClient
+     *  Get http rest client.
      *
-     * @return RestHighLevelClient
+     * @return springframework RestTemplate
      */
-    public RestHighLevelClient highLevelClient() {
-        if (highLevelClient != null) {
-            return highLevelClient;
+    public RestTemplate getRestClient() {
+        if (restTemplate != null) {
+            return restTemplate;
         }
         try {
-            synchronized (RestHighLevelClient.class) {
-                if (highLevelClient == null) {
-                    List<HttpHost> hosts = new ArrayList<>();
-                    String[] hostArrays = 
this.hosts.split(InlongConstants.SEMICOLON);
-                    for (String host : hostArrays) {
-                        if (StringUtils.isNotBlank(host)) {
-                            host = host.trim();
-                            hosts.add(HttpHost.create(host));
-                        }
-                    }
-                    RestClientBuilder clientBuilder = 
RestClient.builder(hosts.toArray(new HttpHost[0]));
-                    this.setEsAuth(clientBuilder);
-                    highLevelClient = new RestHighLevelClient(clientBuilder);
+            synchronized (RestTemplate.class) {
+                if (restTemplate == null) {
+                    restTemplate = new RestTemplate();
                 }
             }
         } catch (Exception e) {
             logger.error("get es high level client error", e);
         }
-        return highLevelClient;
+        return restTemplate;
     }
 
     /**
-     * Elasticsearch authentication
+     * Get http hosts.
      *
-     * @param builder The builder
+     * @return list of http host info
      */
-    private void setEsAuth(RestClientBuilder builder) {
+    public List<HttpHost> getHttpHosts() {
+        if (httpHosts != null) {
+            return httpHosts;
+        }
         try {
-            logger.info("set es auth of enable={}", authEnable);
-            if (authEnable) {
-                final CredentialsProvider credentialsProvider = new 
BasicCredentialsProvider();
-                credentialsProvider.setCredentials(AuthScope.ANY, new 
UsernamePasswordCredentials(username, password));
-                builder.setHttpClientConfigCallback(
-                        httpClientBuilder -> 
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
-
+            synchronized (HttpHost.class) {
+                httpHosts = new ArrayList<>();
+                String[] hostArrays = 
this.hosts.split(InlongConstants.SEMICOLON);
+                for (String host : hostArrays) {
+                    if (StringUtils.isNotBlank(host)) {
+                        host = host.trim();
+                        httpHosts.add(HttpHost.create(host));
+                    }
+                }
             }
         } catch (Exception e) {
-            logger.error("set es auth error ", e);
+            logger.error("get es http hosts error", e);
+        }
+        return httpHosts;
+    }
+
+    /**
+     * Get one http url.
+     *
+     * @return a http url
+     * @throws Exception any exception if occurred
+     */
+    public String getOneHttpUrl() throws Exception {
+        getHttpHosts();
+        if (!httpHosts.isEmpty() && httpHosts.size() > 0) {
+            return httpHosts.get(rand.nextInt(httpHosts.size())).toString();
+        } else {
+            throw new Exception("http hosts is empty! please check hosts!");
         }
     }
 
+    /**
+     * Get all http url.
+     *
+     * @param urlSuffix
+     * @return http url array
+     * @throws Exception
+     */
+    public String[] getHttpUrls(String urlSuffix) throws Exception {
+        getHttpHosts();
+        if (!httpHosts.isEmpty() && httpHosts.size() > 0) {
+            String[] urls = new String[httpHosts.size()];
+            for (int i = 0; i < urls.length; i++) {
+                urls[i] = httpHosts.get(i) + urlSuffix;
+            }
+            return urls;
+        } else {
+            throw new Exception("http hosts is empty! please check hosts!");
+        }

Review Comment:
   ```suggestion
           getHttpHosts();
           if (!httpHosts.isEmpty() && httpHosts.size() > 0) {
               String[] urls = new String[httpHosts.size()];
               for (int i = 0; i < urls.length; i++) {
                   urls[i] = httpHosts.get(i) + urlSuffix;
               }
               return urls;
           }
           throw new Exception("http hosts is empty! please check hosts!");
           
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to