From 7097109938a92a43fe1212fac625bc6701856a5b Mon Sep 17 00:00:00 2001 From: xue_meng_en <1836611252@qq.com> Date: Wed, 28 Sep 2022 21:11:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E5=85=A8=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../applicationdemo/ApplicationServer.java | 28 ++++--- pom.xml | 21 +++-- .../opengauss/cmrestapi/CMRestAPIClient.java | 82 +++++++++---------- .../opengauss/cmrestapi/CMRestAPIServer.java | 8 +- .../opengauss/cmrestapi/InfoPushThread.java | 2 +- .../opengauss/cmrestapi/InfoQueryThread.java | 18 +++- 6 files changed, 91 insertions(+), 68 deletions(-) diff --git a/applicationdemo/src/main/java/com/application/applicationdemo/ApplicationServer.java b/applicationdemo/src/main/java/com/application/applicationdemo/ApplicationServer.java index c0a5f27..4075f42 100644 --- a/applicationdemo/src/main/java/com/application/applicationdemo/ApplicationServer.java +++ b/applicationdemo/src/main/java/com/application/applicationdemo/ApplicationServer.java @@ -15,6 +15,7 @@ package com.application.applicationdemo; import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -35,18 +36,25 @@ public class ApplicationServer { * @return * boolean */ - @PutMapping("/CMRestAPI") - public boolean receiveMasterAndStandbyInfo( - @RequestParam(value = "MasterIpPort", required = false, defaultValue = "")String masterIpPort, - @RequestParam(value = "StanbysInfo", required = false, defaultValue = "")String stanbysInfo) { - if (masterIpPort != null && !"".equals(masterIpPort)) { + @PutMapping("/CMRestAPI/MasterInfo") + public String receiveMasterInfo(@RequestBody String masterInfo) { + String result = "Recvieve master info is null."; + if (masterInfo != null && !"".equals(masterInfo)) { // handle master info - System.out.println("Received put master info request, current master info is " + masterIpPort); + System.out.println("Received put master info request, current master info is " + masterInfo); + result = "Recieved master info successfully."; } - if (stanbysInfo != null && !"".equals(stanbysInfo)) { - // handle standbys info - System.out.println("Received put standbys info request, current standbys info is " + stanbysInfo); + return result; + } + + @PutMapping("/CMRestAPI/StandbyInfo") + public String receiveStandbyInfo(@RequestBody String stanbyInfo) { + String result = "Recvieve standby info is null."; + if (stanbyInfo != null && !"".equals(stanbyInfo)) { + // handle standby info + System.out.println("Received put standby info request, current standbys info is " + stanbyInfo); + result = "Recieved standby info successfully."; } - return true; + return result; } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3cce4f7..87e7e27 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.7.3 + 2.6.7 org.opengauss @@ -20,16 +20,18 @@ org.springframework.boot spring-boot-starter-web + + + org.yaml + snakeyaml + + org.springframework.boot spring-boot-starter-test test - - org.springframework.boot - spring-boot-starter-webflux - com.google.code.gson gson @@ -42,6 +44,15 @@ org.springframework.boot spring-boot-maven-plugin + + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + true + + diff --git a/src/main/java/org/opengauss/cmrestapi/CMRestAPIClient.java b/src/main/java/org/opengauss/cmrestapi/CMRestAPIClient.java index c6a8aed..d92d95a 100644 --- a/src/main/java/org/opengauss/cmrestapi/CMRestAPIClient.java +++ b/src/main/java/org/opengauss/cmrestapi/CMRestAPIClient.java @@ -16,12 +16,13 @@ package org.opengauss.cmrestapi; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.MediaType; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.reactive.function.BodyInserters; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.reactive.function.client.WebClientRequestException; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; /** * @Title: CMRestAPIClient @@ -32,14 +33,20 @@ import org.springframework.web.reactive.function.client.WebClientRequestExceptio */ public class CMRestAPIClient { private String url; - private WebClient webClient; + private RestTemplate restTemplate = null; private Logger logger = LoggerFactory.getLogger(CMRestAPIClient.class); - - public CMRestAPIClient(String uri) { - this.url = uri; - this.webClient = WebClient.create(uri); + + public CMRestAPIClient(String url) { + this.url = url; + SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); + // set connect timeout = 1s + clientHttpRequestFactory.setConnectTimeout(1000); + // set sending timeout = 1s + clientHttpRequestFactory.setReadTimeout(1000); + restTemplate = new RestTemplate(); + restTemplate.setRequestFactory(clientHttpRequestFactory); } - + /** * @Title: pushMasterInfo * @Description: @@ -47,48 +54,35 @@ public class CMRestAPIClient { * @param masterIpPort * void */ - public void pushMasterInfo(String masterIpPort) { - logger.info("Sendind newest master info({}) to {}", masterIpPort, url); - MultiValueMap bodyValues = new LinkedMultiValueMap<>(); - bodyValues.add("MasterIpPort", masterIpPort); - + public void pushMasterInfo(String masterInfo) { + logger.info("Sendind newest master info({}) to {}", masterInfo, url); try { - String response = webClient.put() - .uri("") - .accept(MediaType.APPLICATION_JSON) - .body(BodyInserters.fromFormData(bodyValues)) - .retrieve() - .bodyToMono(String.class) - .block(); - logger.info("Receive response {} from server.", response); - } catch (WebClientRequestException e) { - logger.error("The server {} maybe offline.\nDetail:", url, e); + HttpEntity entity = new HttpEntity<>(masterInfo); + ResponseEntity response = restTemplate.exchange(url + "/MasterInfo", HttpMethod.PUT, entity, String.class); + logger.info("StatusCode: {}", response.getStatusCode()); + logger.info("Msg: {}", response.getBody()); + } catch (ResourceAccessException | HttpClientErrorException e) { + logger.error("Failed to send newest master info.\nDetail:{}", url, e.getMessage()); } + logger.info("Send newest master info successfully."); } - + /** * @Title: pushStandbysInfo * @Description: * Push current standbys' info(ip:port) to url. * void */ - public void pushStandbysInfo() { - String standbysInfo = CMRestAPI.peerIpPorts; - logger.info("Sendind newest standbys info({}) to {}", standbysInfo, url); - MultiValueMap bodyValues = new LinkedMultiValueMap<>(); - bodyValues.add("StanbysInfo", standbysInfo); - + public void pushStandbysInfo(String standbyInfo) { + logger.info("Sendind newest standby info({}) to {}", standbyInfo, url); try { - String response = webClient.put() - .uri("") - .accept(MediaType.APPLICATION_JSON) - .body(BodyInserters.fromFormData(bodyValues)) - .retrieve() - .bodyToMono(String.class) - .block(); - logger.info("Receive response {} from server.", response); - } catch (WebClientRequestException e) { - logger.error("The server {} maybe offline.\nDetail:", url, e.getMessage()); + HttpEntity entity = new HttpEntity<>(standbyInfo); + ResponseEntity response = restTemplate.exchange(url + "/StandbyInfo", HttpMethod.PUT, entity, String.class); + logger.info("Response status code: {}", response.getStatusCode()); + logger.info("Response msg: {}", response.getBody()); + } catch (ResourceAccessException | HttpClientErrorException e) { + logger.error("Failed to send newest standby info.\nDetail:{}", url, e.getMessage()); } + logger.info("Send newest standby info successfully."); } } diff --git a/src/main/java/org/opengauss/cmrestapi/CMRestAPIServer.java b/src/main/java/org/opengauss/cmrestapi/CMRestAPIServer.java index 18e541b..5db6c87 100644 --- a/src/main/java/org/opengauss/cmrestapi/CMRestAPIServer.java +++ b/src/main/java/org/opengauss/cmrestapi/CMRestAPIServer.java @@ -14,7 +14,6 @@ */ package org.opengauss.cmrestapi; -import javax.annotation.PreDestroy; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -52,7 +51,7 @@ public class CMRestAPIServer { private final String SEPARATOR = ","; private Logger logger = LoggerFactory.getLogger(CMRestAPIServer.class); private OGCmdExecuter ogCmdExcuter = new OGCmdExecuter(CMRestAPI.envFile); - + private static final String[] IP_HEADER_CANDIDATES = { "X-Forwarded-For", "Proxy-Client-IP", @@ -116,11 +115,6 @@ public class CMRestAPIServer { List nodesStatus; List defResStatus; } - - @PreDestroy - public void preDestroy() { - logger.info("Destroying CMRestAPI."); - } private String getClientIp(HttpServletRequest request) { String ipAddress = null; diff --git a/src/main/java/org/opengauss/cmrestapi/InfoPushThread.java b/src/main/java/org/opengauss/cmrestapi/InfoPushThread.java index 3bc8449..b1fc2b9 100644 --- a/src/main/java/org/opengauss/cmrestapi/InfoPushThread.java +++ b/src/main/java/org/opengauss/cmrestapi/InfoPushThread.java @@ -40,7 +40,7 @@ public class InfoPushThread implements Runnable { public void run() { CMRestAPIClient client = new CMRestAPIClient(recvAddrUrl); client.pushMasterInfo(masterIpPort); - client.pushStandbysInfo(); + client.pushStandbysInfo(CMRestAPI.peerIpPorts); } /** diff --git a/src/main/java/org/opengauss/cmrestapi/InfoQueryThread.java b/src/main/java/org/opengauss/cmrestapi/InfoQueryThread.java index 9f1b981..4e5bbdc 100644 --- a/src/main/java/org/opengauss/cmrestapi/InfoQueryThread.java +++ b/src/main/java/org/opengauss/cmrestapi/InfoQueryThread.java @@ -14,6 +14,8 @@ */ package org.opengauss.cmrestapi; +import javax.annotation.PreDestroy; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; @@ -35,11 +37,20 @@ public class InfoQueryThread implements Runnable { public InfoQueryThread() { THREAD_NAME = "InfoQueryThread"; } - + + class ThreadException implements Thread.UncaughtExceptionHandler { + @Override + public void uncaughtException(Thread t, Throwable e) { + logger.error("Error occured when web server start. \nDetail:{}" + e.getMessage()); + System.exit(ErrorCode.EUNKNOWN.getCode()); + } + } + public void start() { logger.info("Starting thread {}", THREAD_NAME); if (thread == null) { thread = new Thread(this, THREAD_NAME); + thread.setUncaughtExceptionHandler(new ThreadException()); thread.start (); } } @@ -48,4 +59,9 @@ public class InfoQueryThread implements Runnable { public void run() { SpringApplication.run(InfoQueryThread.class); } + + @PreDestroy + public void preDestroy() { + logger.info("Destroying CMRestAPI."); + } } -- Gitee