wujimin commented on a change in pull request #1766:
URL: 
https://github.com/apache/servicecomb-java-chassis/pull/1766#discussion_r427711788



##########
File path: 
service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/serviceregistry/client/ClientUtil.java
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.servicecomb.serviceregistry.client;
+
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstanceStatus;
+import 
org.apache.servicecomb.serviceregistry.server.ServerMicroserviceInstance;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.servicecomb.serviceregistry.ZeroConfigRegistryConstants.*;
+
+public class ClientUtil {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ClientUtil.class);
+
+    private static ScheduledExecutorService executor = 
Executors.newSingleThreadScheduledExecutor();
+
+    public static Microservice microserviceSelf = new Microservice();
+
+    public static Map<String, String> serviceInstanceMapForHeartbeat = null;
+
+    public static synchronized void init(){
+        Runnable heartbeatRunnable = new Runnable() {
+            @Override
+            public void run() {
+                if (serviceInstanceMapForHeartbeat != null && 
!serviceInstanceMapForHeartbeat.isEmpty()){
+                    // after first registration succeeds
+                    try {
+                        byte[] heartbeatEventDataBytes = 
serviceInstanceMapForHeartbeat.toString().getBytes();
+                        MulticastSocket multicastSocket = new 
MulticastSocket();
+                        multicastSocket.setLoopbackMode(false);
+                        multicastSocket.setTimeToLive(TIME_TO_LIVE);
+
+                        DatagramPacket instanceDataPacket = new 
DatagramPacket(heartbeatEventDataBytes, heartbeatEventDataBytes.length,
+                                InetAddress.getByName(GROUP), PORT);
+
+                        multicastSocket.send(instanceDataPacket);
+                    } catch (Exception e) {
+                        LOGGER.error("Failed to send heartbeat event for 
object: {}", serviceInstanceMapForHeartbeat, e);
+                    }
+                }
+            }
+        };
+        executor.scheduleAtFixedRate(heartbeatRunnable, CLIENT_DELAY, 
HEALTH_CHECK_INTERVAL, TimeUnit.SECONDS);
+    }
+
+    public static Optional<Map<String, String>> 
convertToRegisterDataModel(String serviceId, String microserviceInstanceId,
+                                                                           
MicroserviceInstance microserviceInstance, Microservice microservice) {
+        Map<String, String> serviceInstanceTextAttributesMap = new HashMap<>();
+
+        serviceInstanceTextAttributesMap.put(EVENT, REGISTER_EVENT);
+        serviceInstanceTextAttributesMap.put(VERSION, 
microservice.getVersion());
+        serviceInstanceTextAttributesMap.put(SERVICE_ID, serviceId);
+        serviceInstanceTextAttributesMap.put(INSTANCE_ID, 
microserviceInstanceId);
+        serviceInstanceTextAttributesMap.put(STATUS, 
microserviceInstance.getStatus().toString());
+        serviceInstanceTextAttributesMap.put(APP_ID, microservice.getAppId());
+        serviceInstanceTextAttributesMap.put(SERVICE_NAME, 
microservice.getServiceName());
+
+        String hostName = microserviceInstance.getHostName();
+        serviceInstanceTextAttributesMap.put(HOST_NAME, hostName);
+
+        // schema1$schema2
+        serviceInstanceTextAttributesMap.put(ENDPOINTS, 
convertListToString(microserviceInstance.getEndpoints()));
+        serviceInstanceTextAttributesMap.put(SCHEMA_IDS, 
convertListToString(microservice.getSchemas()));
+
+        return Optional.of(serviceInstanceTextAttributesMap);
+    }
+
+    private static String convertListToString (List<String> list){

Review comment:
       `String.join(",", list)`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to