2 * Copyright 2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.holmes.common.utils;
\r
19 import com.fasterxml.jackson.databind.ObjectMapper;
\r
20 import java.io.IOException;
\r
21 import lombok.extern.slf4j.Slf4j;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.apache.http.HttpResponse;
\r
24 import org.apache.http.client.methods.HttpPost;
\r
25 import org.apache.http.client.methods.HttpRequestBase;
\r
26 import org.apache.http.entity.ByteArrayEntity;
\r
27 import org.apache.http.impl.client.CloseableHttpClient;
\r
28 import org.apache.http.impl.client.HttpClients;
\r
29 import org.apache.http.util.EntityUtils;
\r
30 import org.jvnet.hk2.annotations.Service;
\r
31 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;
\r
32 import org.openo.holmes.common.config.MicroServiceConfig;
\r
33 import org.openo.holmes.common.constant.AlarmConst;
\r
37 public class MSBRegisterUtil {
\r
39 public void register(ServiceRegisterEntity entity) throws IOException {
\r
40 log.info("start inventory micro service register");
\r
41 boolean flag = false;
\r
43 while (!flag && retry < 20) {
\r
44 log.info("inventory micro service register.retry:" + retry);
\r
46 flag = inner_register(entity);
\r
48 log.warn("micro service register failed, sleep 30S and try again.");
\r
51 log.info("micro service register success!");
\r
55 log.info("holmes micro service register end.");
\r
58 private void setHeader(HttpRequestBase httpRequestBase) {
\r
59 httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");
\r
60 httpRequestBase.setHeader("Accept", "application/json");
\r
61 httpRequestBase.setHeader("Content-Type", "application/json");
\r
64 private boolean inner_register(ServiceRegisterEntity entity) {
\r
65 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
67 ObjectMapper mapper = new ObjectMapper();
\r
68 String content = mapper.writeValueAsString(entity);
\r
69 HttpPost httpPost = new HttpPost(
\r
70 MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=false");
\r
71 if (StringUtils.isNotEmpty(content)) {
\r
72 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
\r
74 this.setHeader(httpPost);
\r
75 HttpResponse response;
\r
77 response = httpClient.execute(httpPost);
\r
78 } catch (Exception e) {
\r
79 log.warn("Registering the service to the bus failure", e);
\r
82 if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) {
\r
83 log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity()));
\r
87 "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +
\r
88 response.getStatusLine().getReasonPhrase());
\r
91 } catch (IOException e) {
\r
92 log.warn("ServiceRegisterEntity:" + entity + " parse failed",e);
\r
96 } catch (IOException e) {
\r
97 log.warn("At the time of registering service httpclient close failure",e);
\r
103 private void threadSleep(int second) {
\r
104 log.info("start sleep ....");
\r
106 Thread.sleep(second);
\r
107 } catch (InterruptedException error) {
\r
108 log.error("thread sleep error.errorMsg:" + error.getMessage());
\r
110 log.info("sleep end .");
\r