From 4b68e64dbeeeaa365eb1698987e7d3cbfaef1e60 Mon Sep 17 00:00:00 2001 From: GuangrongFu Date: Sat, 11 Mar 2017 14:03:05 +0800 Subject: [PATCH] Modify the MSB Registration Logic Change-Id: I2b223b3d5b6b377abd21fe77ed2c2eaa4fdacf71 Issue-ID: HOLMES-50 Signed-off-by: GuangrongFu --- holmes-actions/pom.xml | 5 ++ .../holmes/common/msb/MicroserviceBusRest.java | 35 ++++++++++ .../openo/holmes/common/utils/MSBRegisterUtil.java | 78 ++++------------------ 3 files changed, 53 insertions(+), 65 deletions(-) create mode 100644 holmes-actions/src/main/java/org/openo/holmes/common/msb/MicroserviceBusRest.java diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index 955958c..1ca8a8a 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -117,6 +117,11 @@ org.apache.httpcomponents httpclient + + com.eclipsesource.jaxrs + consumer + ${jaxrs.consumer.version} + diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/msb/MicroserviceBusRest.java b/holmes-actions/src/main/java/org/openo/holmes/common/msb/MicroserviceBusRest.java new file mode 100644 index 0000000..eb9deef --- /dev/null +++ b/holmes-actions/src/main/java/org/openo/holmes/common/msb/MicroserviceBusRest.java @@ -0,0 +1,35 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.holmes.common.msb; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import org.openo.holmes.common.api.entity.ServiceRegisterEntity; + +@Path("/openoapi/microservices/v1/services") +public interface MicroserviceBusRest { + @Path("") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate, + ServiceRegisterEntity entity) throws Exception; +} 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 0b3c81e..176107e 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 @@ -16,33 +16,26 @@ package org.openo.holmes.common.utils; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.eclipsesource.jaxrs.consumer.ConsumerFactory; import java.io.IOException; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; 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.glassfish.jersey.client.ClientConfig; import org.jvnet.hk2.annotations.Service; import org.openo.holmes.common.api.entity.ServiceRegisterEntity; import org.openo.holmes.common.config.MicroServiceConfig; -import org.openo.holmes.common.constant.AlarmConst; +import org.openo.holmes.common.msb.MicroserviceBusRest; @Slf4j @Service public class MSBRegisterUtil { public void register(ServiceRegisterEntity entity) throws IOException { - log.info("start inventory micro service register"); + log.info("start holmes micro service register"); boolean flag = false; int retry = 0; while (!flag && retry < 20) { - log.info("holmes micro service register.retry:" + retry); + log.info("Holmes microservice register. retry:" + retry); retry++; flag = inner_register(entity); if (!flag) { @@ -56,62 +49,17 @@ public class MSBRegisterUtil { log.info("holmes micro service register end."); } - 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(); + ClientConfig config = new ClientConfig(); try { - ObjectMapper mapper = new ObjectMapper(); - String content = mapper.writeValueAsString(entity); - HttpPost httpPost = new HttpPost( - MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=true"); - HttpGet httpGet = new HttpGet( - MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services/"); - if (StringUtils.isNotEmpty(content)) { - httpPost.setEntity(new ByteArrayEntity(content.getBytes())); - } - this.setHeader(httpPost); - this.setHeader(httpGet); - HttpResponse response; - try { - response = httpClient.execute(httpPost); - } catch (Exception e) { - log.warn("Registering the service to the bus failure", e); - return false; - } - HttpResponse responseGet = null; - try { - responseGet = httpClient.execute(httpPost); - log.info("all service:" + EntityUtils.toString(responseGet.getEntity())); - } catch (Exception e) { - if (responseGet != null) { - log.info(responseGet.getStatusLine().getReasonPhrase()); - } - log.warn("query all service failure", e); - } - if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) { - log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity())); - return true; - } else { - log.warn( - "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " + - response.getStatusLine().getReasonPhrase()); - return false; - } - } catch (IOException e) { - log.warn("ServiceRegisterEntity:" + entity + " parse failed", e); - } finally { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("At the time of registering service httpclient close failure", e); - } + MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer( + MicroServiceConfig.getMsbServerAddr(), config, MicroserviceBusRest.class); + resourceserviceproxy.registerServce("false", entity); + } catch (Exception error) { + log.error("microservice register failed!" + error.getMessage()); + return false; } - return false; + return true; } private void threadSleep(int second) { -- 2.16.6