From 0c6a784e99354c694a2747a4bb911f035c2cf8da Mon Sep 17 00:00:00 2001 From: Gao Hongtao Date: Sun, 28 Oct 2018 11:14:39 +0800 Subject: [PATCH] Modify webapp for v6 (#1829) * Remove backend service discovery * Add undefined node icon --- .../apm/webapp/proxy/CollectorServerList.java | 89 ----------------- .../apm/webapp/proxy/MvcConfig.java | 67 +++++++++++++ apm-webapp/src/main/resources/application.yml | 3 +- .../apm/webapp/proxy/NIWSServerListTest.java | 95 ------------------- 4 files changed, 69 insertions(+), 185 deletions(-) delete mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/CollectorServerList.java create mode 100644 apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/MvcConfig.java delete mode 100644 apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java diff --git a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/CollectorServerList.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/CollectorServerList.java deleted file mode 100644 index 2df2b0d67..000000000 --- a/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/CollectorServerList.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.webapp.proxy; - -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 { - private static final Logger logger = LoggerFactory.getLogger(CollectorServerList.class); - - private final CopyOnWriteArrayList servers = new CopyOnWriteArrayList<>(); - - private final Gson gson = new Gson(); - - private IClientConfig clientConfig; - - public List getInitialListOfServers() { - return fetchServer(); - } - - public List getUpdatedListOfServers() { - return fetchServer(); - } - - public void initWithNiwsConfig(IClientConfig clientConfig) { - this.clientConfig = clientConfig; - } - - protected List derive(String value) { - List 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 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/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/MvcConfig.java b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/MvcConfig.java new file mode 100644 index 000000000..81c40ba28 --- /dev/null +++ b/apm-webapp/src/main/java/org/apache/skywalking/apm/webapp/proxy/MvcConfig.java @@ -0,0 +1,67 @@ +/* + * 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.webapp.proxy; + +import java.io.IOException; +import java.util.Arrays; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.resource.PathResourceResolver; + +/** + * Additional MVC Configuration. + * + * @author gaohongtao + */ +@Configuration +@EnableWebMvc +public class MvcConfig extends WebMvcConfigurerAdapter { + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry + .addResourceHandler("/**") + .addResourceLocations("classpath:/public/") + .setCachePeriod(3600) + .resourceChain(true) + .addResolver(new PathResourceResolver() { + @Override protected Resource getResource(String resourcePath, Resource location) throws IOException { + Resource raw = super.getResource(resourcePath, location); + if (raw != null || !resourcePath.startsWith("img/node")) { + return raw; + } + Resource resource = location.createRelative("img/node/UNDEFINED.png"); + if (!resource.exists() || !resource.isReadable()) { + return null; + } + if (this.checkResource(resource, location)) { + return resource; + } + + if (this.logger.isTraceEnabled()) { + Resource[] allowedLocations = this.getAllowedLocations(); + this.logger.trace("Resource path \"" + resourcePath + "\" was successfully resolved but resource \"" + resource.getURL() + "\" is neither under the current location \"" + location.getURL() + "\" nor under any of the allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]")); + } + return null; + } + }); + } +} \ No newline at end of file diff --git a/apm-webapp/src/main/resources/application.yml b/apm-webapp/src/main/resources/application.yml index c7aa9f4fa..b5d5a2002 100644 --- a/apm-webapp/src/main/resources/application.yml +++ b/apm-webapp/src/main/resources/application.yml @@ -16,6 +16,7 @@ server: port: 8080 + zuul: ignoredServices: '*' routes: @@ -27,9 +28,9 @@ collector: path: /graphql ribbon: listOfServers: 127.0.0.1:10800 - NIWSServerListClassName: org.apache.skywalking.apm.webapp.proxy.CollectorServerList security: user: admin: password: admin + diff --git a/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java b/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java deleted file mode 100644 index 38748cec0..000000000 --- a/apm-webapp/src/test/java/org/apache/skywalking/apm/webapp/proxy/NIWSServerListTest.java +++ /dev/null @@ -1,95 +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.webapp.proxy; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.core.env.AbstractEnvironment; -import org.springframework.core.env.Environment; -import org.springframework.core.env.MapPropertySource; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; -import com.netflix.loadbalancer.ZoneAwareLoadBalancer; -import org.springframework.cloud.netflix.ribbon.SpringClientFactory; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest -@DirtiesContext -public class NIWSServerListTest { - @Autowired - private Environment env; - - @Autowired - private SpringClientFactory factory; - - private Map serverListClassNames = new HashMap(); - - @Before - public void initServerListClassNames() { - for (Iterator iter = ((AbstractEnvironment) env).getPropertySources().iterator(); iter.hasNext();) { - Object propertySource = iter.next(); - if (propertySource instanceof MapPropertySource) { - Map mapPropertySource = ((MapPropertySource) propertySource).getSource(); - for (Map.Entry entry : mapPropertySource.entrySet()) { - String key = entry.getKey(); - int index; - if (key.endsWith(".NIWSServerListClassName") && - (index = key.indexOf(".ribbon")) > 0) { - String clientName = key.substring(0, index); - serverListClassNames.put(clientName,(String)entry.getValue()); - } - } - } - } - } - - @Test - public void serverListClass() throws ClassNotFoundException { - for (String serverListClassName : serverListClassNames.values()) { - Class clazz = Class.forName(serverListClassName); - } - } - - @Test - public void serverListFliter() { - for (Map.Entry entry : serverListClassNames.entrySet()) { - String clientName = entry.getKey(); - String serverListClassName = entry.getValue(); - ServerList serverList = getLoadBalancer(clientName).getServerListImpl(); - assertNotNull("Client: " + clientName + "'s ServerListImpl is null",serverList); - assertEquals("Clinet: " + clientName + "'s ServerListImpl not Same with setting in configs", - serverListClassName, serverList.getClass().getName()); - } - } - - @SuppressWarnings("unchecked") - private ZoneAwareLoadBalancer getLoadBalancer(String name) { - return (ZoneAwareLoadBalancer)this.factory.getLoadBalancer(name); - } -}