Commit 0c6a784e authored by Gao Hongtao's avatar Gao Hongtao Committed by 吴晟
Browse files

Modify webapp for v6 (#1829)

* Remove backend service discovery

* Add undefined node icon
parent a9489b70
Loading
Loading
Loading
Loading
+0 −89
Original line number Diff line number Diff line
/*
 * 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<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;
    }
}
+67 −0
Original line number Diff line number Diff line
/*
 * 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
+2 −1
Original line number Diff line number Diff line
@@ -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
      
+0 −95
Original line number Diff line number Diff line
/*
 * 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<String, String> serverListClassNames = new HashMap<String, String>();

    @Before
    public void initServerListClassNames() {
        for (Iterator<?> iter = ((AbstractEnvironment) env).getPropertySources().iterator(); iter.hasNext();) {
            Object propertySource = iter.next();
            if (propertySource instanceof MapPropertySource) {
                Map<String, Object> mapPropertySource = ((MapPropertySource) propertySource).getSource();
                for (Map.Entry<String, Object> 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<String, String> entry : serverListClassNames.entrySet()) {
            String clientName = entry.getKey();
            String serverListClassName = entry.getValue();
            ServerList<Server> 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<Server> getLoadBalancer(String name) {
        return (ZoneAwareLoadBalancer<Server>)this.factory.getLoadBalancer(name);
    }
}