diff options
author | 2017-03-10 08:59:27 +0800 | |
---|---|---|
committer | 2017-03-10 10:20:02 +0800 | |
commit | 62359f952127eace5e9715b581f27d8443fc90a9 (patch) | |
tree | 69c417780d82684548a71bf8b2d6e99650e7caa6 /holmes-actions | |
parent | 1c5dbb19bae635260fdd81615f57242c3ca85ec6 (diff) |
Add start registering service with thread
Issue-ID: HOLMES-50
Change-Id: I614d5337884da9e87ee1c2b63fe1a097fe430579
Signed-off-by: youbowu <wu.youbo@zte.com.cn>
Diffstat (limited to 'holmes-actions')
-rw-r--r-- | holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java | 2 | ||||
-rw-r--r-- | holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java | 57 |
2 files changed, 49 insertions, 10 deletions
diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java index c17ad4a..44c52d3 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java @@ -28,7 +28,7 @@ public class MicroServiceConfig { }
public static String getMsbServerAddr() {
- return AlarmConst.HTTP+getProperty("MSB_ADDR") + ":" + AlarmConst.MICRO_SERVICE_PORT;
+ return AlarmConst.HTTP + getProperty("MSB_ADDR") + ":" + AlarmConst.MICRO_SERVICE_PORT;
}
public static String getServiceIp() {
diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java index 9f66178..1de431e 100644 --- a/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java +++ b/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java @@ -26,6 +26,7 @@ import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.api.entity.ServiceRegisterEntity;
import org.openo.holmes.common.config.MicroServiceConfig;
@@ -35,13 +36,40 @@ import org.openo.holmes.common.constant.AlarmConst; @Service
public class MSBRegisterUtil {
- public boolean register(ServiceRegisterEntity entity) throws IOException {
+ public void register(ServiceRegisterEntity entity) throws IOException {
+ ((Runnable) () -> {
+ log.info("start inventory micro service register");
+ boolean flag = false;
+ int retry = 0;
+ while (!flag && retry < 20) {
+ log.info("inventory micro service register.retry:" + retry);
+ retry++;
+ flag = inner_register(entity);
+ if (!flag) {
+ log.warn("micro service register failed, sleep 30S and try again.");
+ threadSleep(30000);
+ } else {
+ log.info("micro service register success!");
+ break;
+ }
+ }
+ log.info("holmes micro service register end.");
+ }).run();
+ }
+
+ private void setHeader(HttpRequestBase httpRequestBase) {
+ httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");
+ httpRequestBase.setHeader("Accept", "application/json");
+ httpRequestBase.setHeader("Content-Type", "application/json");
+ }
+
+ private boolean inner_register(ServiceRegisterEntity entity) {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
ObjectMapper mapper = new ObjectMapper();
String content = mapper.writeValueAsString(entity);
- HttpPost httpPost = new HttpPost(MicroServiceConfig.getMsbServerAddr()
- + "/api/microservices/v1/services?createOrUpdate=false");
+ HttpPost httpPost = new HttpPost(
+ MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=false");
if (StringUtils.isNotEmpty(content)) {
httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
}
@@ -54,7 +82,7 @@ public class MSBRegisterUtil { return false;
}
if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) {
- log.info("Registration successful service to the bus :" + response.getEntity());
+ log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity()));
return true;
} else {
log.warn(
@@ -62,14 +90,25 @@ public class MSBRegisterUtil { response.getStatusLine().getReasonPhrase());
return false;
}
+ } catch (IOException e) {
+ log.warn("ServiceRegisterEntity:" + entity + " parse failed",e);
} finally {
- httpClient.close();
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ log.warn("At the time of registering service httpclient close failure",e);
+ }
}
+ return false;
}
- private void setHeader(HttpRequestBase httpRequestBase) {
- httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");
- httpRequestBase.setHeader("Accept", "application/json");
- httpRequestBase.setHeader("Content-Type", "application/json");
+ private void threadSleep(int second) {
+ log.info("start sleep ....");
+ try {
+ Thread.sleep(second);
+ } catch (InterruptedException error) {
+ log.error("thread sleep error.errorMsg:" + error.getMessage());
+ }
+ log.info("sleep end .");
}
}
\ No newline at end of file |