0b3c81e88354a129bbeb12381d8655988f2a10ac
[holmes/common.git] / holmes-actions / src / main / java / org / openo / holmes / common / utils / MSBRegisterUtil.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package org.openo.holmes.common.utils;\r
18 \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
35 \r
36 @Slf4j\r
37 @Service\r
38 public class MSBRegisterUtil {\r
39 \r
40     public void register(ServiceRegisterEntity entity) throws IOException {\r
41         log.info("start inventory micro service register");\r
42         boolean flag = false;\r
43         int retry = 0;\r
44         while (!flag && retry < 20) {\r
45             log.info("holmes micro service register.retry:" + retry);\r
46             retry++;\r
47             flag = inner_register(entity);\r
48             if (!flag) {\r
49                 log.warn("micro service register failed, sleep 30S and try again.");\r
50                 threadSleep(30000);\r
51             } else {\r
52                 log.info("micro service register success!");\r
53                 break;\r
54             }\r
55         }\r
56         log.info("holmes micro service register end.");\r
57     }\r
58 \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
63     }\r
64 \r
65     private boolean inner_register(ServiceRegisterEntity entity) {\r
66         CloseableHttpClient httpClient = HttpClients.createDefault();\r
67         try {\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
76             }\r
77             this.setHeader(httpPost);\r
78             this.setHeader(httpGet);\r
79             HttpResponse response;\r
80             try {\r
81                 response = httpClient.execute(httpPost);\r
82             } catch (Exception e) {\r
83                 log.warn("Registering the service to the bus failure", e);\r
84                 return false;\r
85             }\r
86             HttpResponse responseGet = null;\r
87             try {\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
93                 }\r
94                 log.warn("query all service failure", e);\r
95             }\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
98                 return true;\r
99             } else {\r
100                 log.warn(\r
101                         "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +\r
102                                 response.getStatusLine().getReasonPhrase());\r
103                 return false;\r
104             }\r
105         } catch (IOException e) {\r
106             log.warn("ServiceRegisterEntity:" + entity + " parse failed", e);\r
107         } finally {\r
108             try {\r
109                 httpClient.close();\r
110             } catch (IOException e) {\r
111                 log.warn("At the time of registering service httpclient close failure", e);\r
112             }\r
113         }\r
114         return false;\r
115     }\r
116 \r
117     private void threadSleep(int second) {\r
118         log.info("start sleep ....");\r
119         try {\r
120             Thread.sleep(second);\r
121         } catch (InterruptedException error) {\r
122             log.error("thread sleep error.errorMsg:" + error.getMessage());\r
123         }\r
124         log.info("sleep end .");\r
125     }\r
126 }