From dc7cb2bdeef1e76de70f54e0bc50b418784e396f Mon Sep 17 00:00:00 2001 From: nkorange Date: Tue, 30 Oct 2018 17:59:05 +0800 Subject: [PATCH] Fix #190 --- .../client/naming/NacosNamingService.java | 2 ++ .../nacos/client/naming/beat/BeatInfo.java | 20 ++++++++++++++++ .../com/alibaba/nacos/client/NamingTest.java | 24 ++++++++++++++++++- .../com/alibaba/nacos/naming/NamingApp.java | 1 - .../nacos/naming/healthcheck/RsInfo.java | 20 ++++++++++++++++ .../alibaba/nacos/naming/web/ApiCommands.java | 3 ++- .../nacos/naming/core/IpAddressTest.java | 19 +++++++++++++++ 7 files changed, 86 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index fa457bf86..63c56a5c6 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -155,6 +155,8 @@ public class NacosNamingService implements NamingService { beatInfo.setIp(instance.getIp()); beatInfo.setPort(instance.getPort()); beatInfo.setCluster(instance.getCluster().getName()); + beatInfo.setWeight(instance.getWeight()); + beatInfo.setMetadata(instance.getMetadata()); beatReactor.addBeatInfo(serviceName, beatInfo); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java index f5a3fa87a..df4471696 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatInfo.java @@ -17,6 +17,8 @@ package com.alibaba.nacos.client.naming.beat; import com.alibaba.fastjson.JSON; +import java.util.Map; + /** * @author dungu.zpf */ @@ -24,8 +26,10 @@ public class BeatInfo { private int port; private String ip; + private double weight; private String dom; private String cluster; + private Map metadata; @Override public String toString() { @@ -63,4 +67,20 @@ public class BeatInfo { public void setPort(int port) { this.port = port; } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } } diff --git a/client/src/test/java/com/alibaba/nacos/client/NamingTest.java b/client/src/test/java/com/alibaba/nacos/client/NamingTest.java index e876bf103..e5bbd9889 100644 --- a/client/src/test/java/com/alibaba/nacos/client/NamingTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/NamingTest.java @@ -15,16 +15,38 @@ */ package com.alibaba.nacos.client; +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; +import org.junit.Ignore; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + /** * @author dungu.zpf */ public class NamingTest { @Test - public void testServiceList() { + @Ignore + public void testServiceList() throws Exception { + NamingService namingService = NacosFactory.createNamingService("127.0.0.1:8848"); + + Instance instance = new Instance(); + instance.setIp("1.1.1.1"); + instance.setPort(80); + instance.setWeight(2); + Map map = new HashMap(); + map.put("env", "prod"); + map.put("version", "2.0"); + instance.setMetadata(map); + + namingService.registerInstance("dungu.test.1", instance); + + Thread.sleep(1000000000L); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/NamingApp.java b/naming/src/main/java/com/alibaba/nacos/naming/NamingApp.java index 11e4f8d8f..2ce5d5f24 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/NamingApp.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/NamingApp.java @@ -24,7 +24,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @author xxc */ @SpringBootApplication - public class NamingApp { public static void main(String[] args) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java index 8043d575b..5a59b82c1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/RsInfo.java @@ -17,6 +17,8 @@ package com.alibaba.nacos.naming.healthcheck; import com.alibaba.fastjson.JSON; +import java.util.Map; + /** * Metrics info of server * @@ -33,6 +35,8 @@ public class RsInfo { private String dom; private String ak; private String cluster; + private double weight; + private Map metadata; public String getDom() { return dom; @@ -114,6 +118,22 @@ public class RsInfo { this.mem = mem; } + public double getWeight() { + return weight; + } + + public void setWeight(double weight) { + this.weight = weight; + } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + public String toString() { return JSON.toJSONString(this); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java index 05bed9657..99e1d7b7c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/ApiCommands.java @@ -308,7 +308,8 @@ public class ApiCommands { IpAddress ipAddress = new IpAddress(); ipAddress.setPort(port); ipAddress.setIp(ip); - ipAddress.setWeight(1); + ipAddress.setWeight(clientBeat.getWeight()); + ipAddress.setMetadata(clientBeat.getMetadata()); ipAddress.setClusterName(clusterName); if (!virtualClusterDomain.allIPs().contains(ipAddress)) { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/IpAddressTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/IpAddressTest.java index 63f76f38c..2fd21a688 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/IpAddressTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/IpAddressTest.java @@ -15,10 +15,15 @@ */ package com.alibaba.nacos.naming.core; +import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.naming.healthcheck.RsInfo; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + /** * @author dungu.zpf */ @@ -50,4 +55,18 @@ public class IpAddressTest { Assert.assertEquals(2, ipAddress.getWeight(), 0.001); Assert.assertEquals("TEST1", ipAddress.getClusterName()); } + + @Test + public void rsInfo() { + + RsInfo info = new RsInfo(); + Map metadata = new HashMap<>(); + metadata.put("version", "2222"); + info.setMetadata(metadata); + System.out.println(JSON.toJSONString(info)); + + String json = JSON.toJSONString(info); + RsInfo info1 = JSON.parseObject(json, RsInfo.class); + System.out.println(info1); + } }