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.HttpGet;
\r
25 import org.apache.http.client.methods.HttpPost;
\r
26 import org.apache.http.client.methods.HttpRequestBase;
\r
27 import org.apache.http.entity.ByteArrayEntity;
\r
28 import org.apache.http.impl.client.CloseableHttpClient;
\r
29 import org.apache.http.impl.client.HttpClients;
\r
30 import org.apache.http.util.EntityUtils;
\r
31 import org.jvnet.hk2.annotations.Service;
\r
32 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;
\r
33 import org.openo.holmes.common.config.MicroServiceConfig;
\r
34 import org.openo.holmes.common.constant.AlarmConst;
\r
38 public class MSBRegisterUtil {
\r
40 public void register(ServiceRegisterEntity entity) throws IOException {
\r
41 log.info("start inventory micro service register");
\r
42 boolean flag = false;
\r
44 while (!flag && retry < 20) {
\r
45 log.info("holmes micro service register.retry:" + retry);
\r
47 flag = inner_register(entity);
\r
49 log.warn("micro service register failed, sleep 30S and try again.");
\r
52 log.info("micro service register success!");
\r
56 log.info("holmes micro service register end.");
\r
59 private void setHeader(HttpRequestBase httpRequestBase) {
\r
60 httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");
\r
61 httpRequestBase.setHeader("Accept", "application/json");
\r
62 httpRequestBase.setHeader("Content-Type", "application/json");
\r
65 private boolean inner_register(ServiceRegisterEntity entity) {
\r
66 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
68 ObjectMapper mapper = new ObjectMapper();
\r
69 String content = mapper.writeValueAsString(entity);
\r
70 HttpPost httpPost = new HttpPost(
\r
71 MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=true");
\r
72 HttpGet httpGet = new HttpGet(
\r
73 MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services/");
\r
74 if (StringUtils.isNotEmpty(content)) {
\r
75 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
\r
77 this.setHeader(httpPost);
\r
78 this.setHeader(httpGet);
\r
79 HttpResponse response;
\r
81 response = httpClient.execute(httpPost);
\r
82 } catch (Exception e) {
\r
83 log.warn("Registering the service to the bus failure", e);
\r
86 HttpResponse responseGet = null;
\r
88 responseGet = httpClient.execute(httpPost);
\r
89 log.info("all service:" + EntityUtils.toString(responseGet.getEntity()));
\r
90 } catch (Exception e) {
\r
91 if (responseGet != null) {
\r
92 log.info(responseGet.getStatusLine().getReasonPhrase());
\r
94 log.warn("query all service failure", e);
\r
96 if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) {
\r
97 log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity()));
\r
101 "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +
\r
102 response.getStatusLine().getReasonPhrase());
\r
105 } catch (IOException e) {
\r
106 log.warn("ServiceRegisterEntity:" + entity + " parse failed", e);
\r
109 httpClient.close();
\r
110 } catch (IOException e) {
\r
111 log.warn("At the time of registering service httpclient close failure", e);
\r
117 private void threadSleep(int second) {
\r
118 log.info("start sleep ....");
\r
120 Thread.sleep(second);
\r
121 } catch (InterruptedException error) {
\r
122 log.error("thread sleep error.errorMsg:" + error.getMessage());
\r
124 log.info("sleep end .");
\r