This is an automated email from the ASF dual-hosted git repository.

hanahmily pushed a commit to branch feature/5.0.0
in repository https://gitbox.apache.org/repos/asf/incubator-skywalking-ui.git


The following commit(s) were added to refs/heads/feature/5.0.0 by this push:
     new 43b67ee  Add graphql proxy
43b67ee is described below

commit 43b67ee20a34fec4b21814e2a7b6c2efcbffeb92
Author: hanahmily <[email protected]>
AuthorDate: Wed Feb 7 15:56:57 2018 +0800

    Add graphql proxy
---
 .../skywalking/apm/ui/ApplicationStartUp.java      | 10 ++-
 .../apache/skywalking/apm/ui/config/UIConfig.java  |  4 +-
 .../skywalking/apm/ui/creator/UrlCreator.java      | 73 ------------------
 .../apm/ui/tools/CollectorServerList.java          | 90 ++++++++++++++++++++++
 .../apm/ui/tools/CollectorUIServerGetterTimer.java | 89 ---------------------
 .../skywalking/apm/ui/tools/RewritePathFilter.java | 63 +++++++++++++++
 .../skywalking/apm/ui/tools/ServerSelector.java    | 56 --------------
 src/main/resources/application.yml                 | 10 ++-
 8 files changed, 170 insertions(+), 225 deletions(-)

diff --git a/src/main/java/org/apache/skywalking/apm/ui/ApplicationStartUp.java 
b/src/main/java/org/apache/skywalking/apm/ui/ApplicationStartUp.java
index 4e3a979..efc325e 100644
--- a/src/main/java/org/apache/skywalking/apm/ui/ApplicationStartUp.java
+++ b/src/main/java/org/apache/skywalking/apm/ui/ApplicationStartUp.java
@@ -18,11 +18,14 @@
 
 package org.apache.skywalking.apm.ui;
 
+import org.apache.skywalking.apm.ui.config.UIConfig;
+import org.apache.skywalking.apm.ui.tools.RewritePathFilter;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.web.support.SpringBootServletInitializer;
 import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
 import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
 
 @SpringBootApplication
 @EnableZuulProxy
@@ -30,7 +33,10 @@ public class ApplicationStartUp extends 
SpringBootServletInitializer {
 
     public static void main(String[] args) throws Exception {
         ApplicationContext applicationContext = 
SpringApplication.run(ApplicationStartUp.class, args);
-//        CollectorUIServerGetterTimer.INSTANCE.start(applicationContext);
     }
-    
+
+    @Bean
+    public RewritePathFilter addWritePathFilter(UIConfig uiConfig) {
+        return new RewritePathFilter(uiConfig.getRewritePath());
+    }
 }
diff --git a/src/main/java/org/apache/skywalking/apm/ui/config/UIConfig.java 
b/src/main/java/org/apache/skywalking/apm/ui/config/UIConfig.java
index 5fe1bdf..3b7bc8e 100644
--- a/src/main/java/org/apache/skywalking/apm/ui/config/UIConfig.java
+++ b/src/main/java/org/apache/skywalking/apm/ui/config/UIConfig.java
@@ -29,6 +29,6 @@ import org.springframework.context.annotation.Configuration;
 @Getter
 public class UIConfig {
     
-    @Value("${collector.servers}")
-    private String[] servers;
+    @Value("${collector.path}")
+    private String rewritePath;
 }
diff --git a/src/main/java/org/apache/skywalking/apm/ui/creator/UrlCreator.java 
b/src/main/java/org/apache/skywalking/apm/ui/creator/UrlCreator.java
deleted file mode 100644
index 693a7bf..0000000
--- a/src/main/java/org/apache/skywalking/apm/ui/creator/UrlCreator.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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.skywalking.apm.ui.creator;
-
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.skywalking.apm.ui.tools.ServerSelector;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-/**
- * @author peng-yongsheng
- */
-@Component
-public class UrlCreator {
-
-    private List<String> servers;
-
-    private volatile boolean inited = false;
-
-    private Object waiter = new Object();
-
-    public UrlCreator() {
-        servers = new ArrayList<>();
-    }
-
-    @Autowired
-    private ServerSelector serverSelector;
-
-    public String compound(String urlSuffix) {
-        if (!inited) {
-            synchronized (waiter) {
-                try {
-                    waiter.wait(100);
-                } catch (InterruptedException e) {
-                }
-            }
-        }
-        String server = serverSelector.select(servers);
-        return "http://"; + server + urlSuffix;
-    }
-
-    public void updateServerList(List<String> servers) {
-        if (null == servers || servers.isEmpty()) {
-            return;
-        }
-        try {
-            this.servers.clear();
-            this.servers.addAll(servers);
-        } finally {
-            inited = true;
-            synchronized (waiter) {
-                waiter.notifyAll();
-            }
-        }
-    }
-}
diff --git 
a/src/main/java/org/apache/skywalking/apm/ui/tools/CollectorServerList.java 
b/src/main/java/org/apache/skywalking/apm/ui/tools/CollectorServerList.java
new file mode 100644
index 0000000..946086b
--- /dev/null
+++ b/src/main/java/org/apache/skywalking/apm/ui/tools/CollectorServerList.java
@@ -0,0 +1,90 @@
+/*
+ * 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.skywalking.apm.ui.tools;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.Lists;
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.netflix.client.config.CommonClientConfigKey;
+import com.netflix.client.config.IClientConfig;
+import com.netflix.loadbalancer.AbstractServerList;
+import com.netflix.loadbalancer.Server;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+public class CollectorServerList extends AbstractServerList<Server> {
+    private static final Logger logger = 
LoggerFactory.getLogger(CollectorServerList.class);
+
+    private final CopyOnWriteArrayList<Server> servers = new 
CopyOnWriteArrayList<>();
+
+    private final Gson gson = new Gson();
+
+    private IClientConfig clientConfig;
+
+    public List<Server> getInitialListOfServers() {
+        return fetchServer();
+    }
+
+    public List<Server> getUpdatedListOfServers() {
+        return fetchServer();
+    }
+
+    public void initWithNiwsConfig(IClientConfig clientConfig) {
+        this.clientConfig = clientConfig;
+
+    }
+
+    protected List<Server> derive(String value) {
+        List<Server> list = Lists.newArrayList();
+        if (Strings.isNullOrEmpty(value)) {
+            return list;
+        }
+        String[] serverArray = value.split(",");
+        for (String s : serverArray) {
+            list.add(new Server(s.trim()));
+        }
+        return list;
+    }
+
+    private List<Server> fetchServer() {
+        for (Server server : 
derive(this.clientConfig.get(CommonClientConfigKey.ListOfServers))) {
+            try {
+                String uiServerResponse = 
HttpClientTools.INSTANCE.get("http://"; + server.getHostPort() + "/ui/jetty", 
null);
+                logger.debug("uiServerResponse: {}", uiServerResponse);
+                JsonArray serverArray = gson.fromJson(uiServerResponse, 
JsonArray.class);
+                if (serverArray == null || serverArray.size() == 0) {
+                    logger.warn("emtry grpc server array, skip : {}", server);
+                    continue;
+                }
+                return 
Lists.newArrayList(StreamSupport.stream(serverArray.spliterator(), false).map(f 
-> new Server(f.getAsString())).collect(Collectors.toList()));
+            } catch (IOException e) {
+                logger.error(e.getMessage(), e);
+            }
+        }
+        logger.warn("none agentstream server return available grpc server.");
+        return servers;
+    }
+}
diff --git 
a/src/main/java/org/apache/skywalking/apm/ui/tools/CollectorUIServerGetterTimer.java
 
b/src/main/java/org/apache/skywalking/apm/ui/tools/CollectorUIServerGetterTimer.java
deleted file mode 100644
index fe9e50f..0000000
--- 
a/src/main/java/org/apache/skywalking/apm/ui/tools/CollectorUIServerGetterTimer.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.skywalking.apm.ui.tools;
-
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
-import org.apache.skywalking.apm.ui.config.UIConfig;
-import org.apache.skywalking.apm.ui.creator.UrlCreator;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.ApplicationContext;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author peng-yongsheng
- */
-public enum CollectorUIServerGetterTimer {
-    INSTANCE;
-
-    private Logger logger = 
LoggerFactory.getLogger(CollectorUIServerGetterTimer.class);
-
-    private Gson gson = new Gson();
-
-    public void start(ApplicationContext applicationContext) {
-        logger.info("collector ui server getter timer start");
-        final long timeInterval = 10 * 1000;
-        
-        UIConfig uiConfig = applicationContext.getBean(UIConfig.class);
-        UrlCreator urlCreator = applicationContext.getBean(UrlCreator.class);
-
-        Thread persistenceThread = new Thread(() -> {
-            while (true) {
-                try {
-                    List<String> servers = getServer(uiConfig);
-                    urlCreator.updateServerList(servers);
-                } catch (Throwable e) {
-                    logger.error(e.getMessage(), e);
-                } finally {
-                    try {
-                        Thread.sleep(timeInterval);
-                    } catch (InterruptedException ignored) {
-                    }
-                }
-            }
-        });
-        persistenceThread.setName("timerPersistence");
-        persistenceThread.start();
-    }
-
-    private List<String> getServer(UIConfig uiConfig) {
-        for (String server : uiConfig.getServers()) {
-            try {
-                String uiServerResponse = 
HttpClientTools.INSTANCE.get("http://"; + server + "/ui/jetty", null);
-                logger.debug("uiServerResponse: %s", uiServerResponse);
-                JsonArray serverArray = gson.fromJson(uiServerResponse, 
JsonArray.class);
-                if (serverArray == null || serverArray.size() == 0) {
-                    logger.warn("emtry grpc server array, skip : %s", server);
-                    continue;
-                }
-                List<String> servers = new ArrayList<>();
-                serverArray.forEach(serverElement -> 
servers.add(serverElement.getAsString()));
-                return servers;
-            } catch (IOException e) {
-                logger.error(e.getMessage(), e);
-            }
-        }
-        logger.warn("none agentstream server return available grpc server.");
-        return null;
-    }
-}
diff --git 
a/src/main/java/org/apache/skywalking/apm/ui/tools/RewritePathFilter.java 
b/src/main/java/org/apache/skywalking/apm/ui/tools/RewritePathFilter.java
new file mode 100644
index 0000000..9666761
--- /dev/null
+++ b/src/main/java/org/apache/skywalking/apm/ui/tools/RewritePathFilter.java
@@ -0,0 +1,63 @@
+/*
+ * 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.skywalking.apm.ui.tools;
+
+import com.netflix.zuul.ZuulFilter;
+import com.netflix.zuul.context.RequestContext;
+import lombok.RequiredArgsConstructor;
+
+import static 
org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
+
+/**
+ * Rewrite url to rewritePath
+ *
+ * @author gaohongtao
+ */
+@RequiredArgsConstructor
+public class RewritePathFilter extends ZuulFilter {
+
+    private static final String REQUEST_URI = "requestURI";
+
+    private static final int ORDER = PRE_DECORATION_FILTER_ORDER + 1;
+
+    private final String rewritePath;
+
+    @Override
+    public int filterOrder() {
+        return ORDER;
+    }
+
+    @Override
+    public String filterType() {
+        return "pre";
+    }
+
+    @Override
+    public boolean shouldFilter() {
+        RequestContext ctx = RequestContext.getCurrentContext();
+        return ctx.containsKey(REQUEST_URI);
+    }
+
+    @Override
+    public Object run() {
+        RequestContext ctx = RequestContext.getCurrentContext();
+        ctx.set(REQUEST_URI, rewritePath);
+        return null;
+    }
+}
diff --git 
a/src/main/java/org/apache/skywalking/apm/ui/tools/ServerSelector.java 
b/src/main/java/org/apache/skywalking/apm/ui/tools/ServerSelector.java
deleted file mode 100644
index 3b149ab..0000000
--- a/src/main/java/org/apache/skywalking/apm/ui/tools/ServerSelector.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.skywalking.apm.ui.tools;
-
-import java.util.List;
-
-import org.springframework.stereotype.Component;
-
-/**
- * @author peng-yongsheng
- */
-@Component
-public class ServerSelector {
-
-    private int index = 0;
-
-    public String select(List<String> serverList) {
-        String server = null;
-        int tryCnt = 0;
-        do {
-            int size = serverList.size();
-            int selectIndex = Math.abs(index) % size;
-            index++;
-            try {
-                server = serverList.get(selectIndex);
-            } catch (Exception ignored) {
-            }
-            if (null == server) {
-                tryCnt++;
-                try {
-                    Thread.sleep(10);
-                } catch (InterruptedException ignored) {
-                }
-            } else {
-                return server;
-            }
-        } while (tryCnt < 3);
-        throw new RuntimeException("select server fail.");
-    }
-}
diff --git a/src/main/resources/application.yml 
b/src/main/resources/application.yml
index a6d36f5..0d44bda 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -18,11 +18,15 @@
 
 server:
   port: 8080
-collector:
-  servers: 127.0.0.1:10800
 zuul:
   ignoredServices: '*'
   routes:
     api:
       path: /api/**
-      url: http://127.0.0.1:12800/graphql
+      serviceId: collector
+
+collector:
+  path: /graphql
+  ribbon:
+    listOfServers: 127.0.0.1:10800
+    NIWSServerListClassName: 
org.apache.skywalking.apm.ui.tools.CollectorServerList

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to