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.entity.ByteArrayEntity;
\r
26 import org.apache.http.impl.client.CloseableHttpClient;
\r
27 import org.apache.http.impl.client.HttpClients;
\r
28 import org.jvnet.hk2.annotations.Service;
\r
29 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;
\r
30 import org.openo.holmes.common.config.MicroServiceConfig;
\r
31 import org.openo.holmes.common.constant.AlarmConst;
\r
35 public class MSBRegisterUtil {
\r
37 public boolean register(ServiceRegisterEntity entity) throws IOException {
\r
38 CloseableHttpClient httpClient = HttpClients.createDefault();
\r
40 ObjectMapper mapper = new ObjectMapper();
\r
41 String content = mapper.writeValueAsString(entity);
\r
42 HttpPost httpPost = new HttpPost("http://" + MicroServiceConfig.getMsbServerAddr()
\r
43 + ":8086/api/microservices/v1/services?createOrUpdate=false");
\r
44 if (StringUtils.isNotEmpty(content)) {
\r
45 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
\r
47 HttpResponse response;
\r
49 response = httpClient.execute(httpPost);
\r
50 } catch (Exception e) {
\r
51 log.warn("Registering the service to the bus failure", e);
\r
54 if (response.getStatusLine().getStatusCode() == AlarmConst.RESPONSE_STATUS_OK) {
\r
55 log.info("Registration successful service to the bus :" + response.getEntity());
\r
58 log.warn("Registering the service to the bus failure:"+response.getStatusLine().getStatusCode()+" "+
\r
59 response.getStatusLine().getReasonPhrase()+response.getStatusLine().getProtocolVersion());
\r