diff options
author | Guangrong Fu <fu.guangrong@zte.com.cn> | 2017-03-10 02:26:51 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@open-o.org> | 2017-03-10 02:26:51 +0000 |
commit | 76709abd1cb52cb187f574d26744b4504ea6cbe2 (patch) | |
tree | 19c47881894f46134e744cb522519f2a97d444ee /holmes-actions | |
parent | 292ecc8781b642f494638acc3595e33079303ced (diff) | |
parent | 62359f952127eace5e9715b581f27d8443fc90a9 (diff) |
Merge "Add start registering service with thread"
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 |