Modify the MSB Registration Logic
authorGuangrongFu <fu.guangrong@zte.com.cn>
Sat, 11 Mar 2017 06:03:05 +0000 (14:03 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Sat, 11 Mar 2017 06:08:48 +0000 (14:08 +0800)
Change-Id: I2b223b3d5b6b377abd21fe77ed2c2eaa4fdacf71
Issue-ID: HOLMES-50
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
holmes-actions/pom.xml
holmes-actions/src/main/java/org/openo/holmes/common/msb/MicroserviceBusRest.java [new file with mode: 0644]
holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java

index 955958c..1ca8a8a 100644 (file)
             <groupId>org.apache.httpcomponents</groupId>\r
             <artifactId>httpclient</artifactId>\r
         </dependency>\r
+        <dependency>\r
+            <groupId>com.eclipsesource.jaxrs</groupId>\r
+            <artifactId>consumer</artifactId>\r
+            <version>${jaxrs.consumer.version}</version>\r
+        </dependency>\r
     </dependencies>\r
     <build>\r
         <resources>\r
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 (file)
index 0000000..eb9deef
--- /dev/null
@@ -0,0 +1,35 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openo.holmes.common.msb;\r
+\r
+import javax.ws.rs.Consumes;\r
+import javax.ws.rs.POST;\r
+import javax.ws.rs.Path;\r
+import javax.ws.rs.Produces;\r
+import javax.ws.rs.QueryParam;\r
+import javax.ws.rs.core.MediaType;\r
+import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
+\r
+@Path("/openoapi/microservices/v1/services")\r
+public interface MicroserviceBusRest {\r
+    @Path("")\r
+    @POST\r
+    @Consumes(MediaType.APPLICATION_JSON)\r
+    @Produces(MediaType.APPLICATION_JSON)\r
+    public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,\r
+            ServiceRegisterEntity entity) throws Exception;\r
+}\r
index 0b3c81e..176107e 100644 (file)
 \r
 package org.openo.holmes.common.utils;\r
 \r
-import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
 import java.io.IOException;\r
 import lombok.extern.slf4j.Slf4j;\r
-import org.apache.commons.lang3.StringUtils;\r
-import org.apache.http.HttpResponse;\r
-import org.apache.http.client.methods.HttpGet;\r
-import org.apache.http.client.methods.HttpPost;\r
 import org.apache.http.client.methods.HttpRequestBase;\r
-import org.apache.http.entity.ByteArrayEntity;\r
-import org.apache.http.impl.client.CloseableHttpClient;\r
-import org.apache.http.impl.client.HttpClients;\r
-import org.apache.http.util.EntityUtils;\r
+import org.glassfish.jersey.client.ClientConfig;\r
 import org.jvnet.hk2.annotations.Service;\r
 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
 import org.openo.holmes.common.config.MicroServiceConfig;\r
-import org.openo.holmes.common.constant.AlarmConst;\r
+import org.openo.holmes.common.msb.MicroserviceBusRest;\r
 \r
 @Slf4j\r
 @Service\r
 public class MSBRegisterUtil {\r
 \r
     public void register(ServiceRegisterEntity entity) throws IOException {\r
-        log.info("start inventory micro service register");\r
+        log.info("start holmes micro service register");\r
         boolean flag = false;\r
         int retry = 0;\r
         while (!flag && retry < 20) {\r
-            log.info("holmes micro service register.retry:" + retry);\r
+            log.info("Holmes microservice register. retry:" + retry);\r
             retry++;\r
             flag = inner_register(entity);\r
             if (!flag) {\r
@@ -56,62 +49,17 @@ public class MSBRegisterUtil {
         log.info("holmes micro service register end.");\r
     }\r
 \r
-    private void setHeader(HttpRequestBase httpRequestBase) {\r
-        httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");\r
-        httpRequestBase.setHeader("Accept", "application/json");\r
-        httpRequestBase.setHeader("Content-Type", "application/json");\r
-    }\r
-\r
     private boolean inner_register(ServiceRegisterEntity entity) {\r
-        CloseableHttpClient httpClient = HttpClients.createDefault();\r
+        ClientConfig config = new ClientConfig();\r
         try {\r
-            ObjectMapper mapper = new ObjectMapper();\r
-            String content = mapper.writeValueAsString(entity);\r
-            HttpPost httpPost = new HttpPost(\r
-                    MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=true");\r
-            HttpGet httpGet = new HttpGet(\r
-                    MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services/");\r
-            if (StringUtils.isNotEmpty(content)) {\r
-                httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
-            }\r
-            this.setHeader(httpPost);\r
-            this.setHeader(httpGet);\r
-            HttpResponse response;\r
-            try {\r
-                response = httpClient.execute(httpPost);\r
-            } catch (Exception e) {\r
-                log.warn("Registering the service to the bus failure", e);\r
-                return false;\r
-            }\r
-            HttpResponse responseGet = null;\r
-            try {\r
-                responseGet = httpClient.execute(httpPost);\r
-                log.info("all service:" + EntityUtils.toString(responseGet.getEntity()));\r
-            } catch (Exception e) {\r
-                if (responseGet != null) {\r
-                    log.info(responseGet.getStatusLine().getReasonPhrase());\r
-                }\r
-                log.warn("query all service failure", e);\r
-            }\r
-            if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) {\r
-                log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity()));\r
-                return true;\r
-            } else {\r
-                log.warn(\r
-                        "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +\r
-                                response.getStatusLine().getReasonPhrase());\r
-                return false;\r
-            }\r
-        } catch (IOException e) {\r
-            log.warn("ServiceRegisterEntity:" + entity + " parse failed", e);\r
-        } finally {\r
-            try {\r
-                httpClient.close();\r
-            } catch (IOException e) {\r
-                log.warn("At the time of registering service httpclient close failure", e);\r
-            }\r
+            MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
+                    MicroServiceConfig.getMsbServerAddr(), config, MicroserviceBusRest.class);\r
+            resourceserviceproxy.registerServce("false", entity);\r
+        } catch (Exception error) {\r
+            log.error("microservice register failed!" + error.getMessage());\r
+            return false;\r
         }\r
-        return false;\r
+        return true;\r
     }\r
 \r
     private void threadSleep(int second) {\r