Netflix / zuul

Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.
Apache License 2.0
13.52k stars 2.38k forks source link

Failed to run the sample #454

Open ikonglong opened 6 years ago

ikonglong commented 6 years ago

I'm interested in zuul2, and i want to get to know what the sample do. I followed the Getting-Started-2.0 document to make the sample running, but then the error occurred: ConnectTimeoutException: Connect to ec2-50-19-255-39.compute-1.amazonaws.com:7001 timed out.

Could you tell me What should I prepare for running the sample on my localhost and how to try out the features that the sample provides.

tluo-github commented 6 years ago

you can see log:

2018-05-30 15:23:55,463 INFO  com.netflix.appinfo.RefreshableAmazonInfoProvider [main] Datacenter is: Amazon
2018-05-30 15:23:55,479 WARN  com.netflix.discovery.internal.util.Archaius1Utils [main] Cannot find the properties specified : eureka-client. This may be okay if there are other environment specific properties or the configuration is installed with a different mechanism.
2018-05-30 15:23:55,486 WARN  com.netflix.discovery.internal.util.Archaius1Utils [main] Cannot find the properties specified : eureka-client. This may be okay if there are other environment specific properties or the configuration is installed with a different mechanism.
2018-05-30 15:23:55,807 WARN  com.netflix.appinfo.AmazonInfo$Builder [main] Skipping the rest of AmazonInfo init as we were not able to load instanceId after the configured number of retries: 3, per fail fast configuration: true

because the default is to use eureka 2 to connect to the aws data center. you can change application.properties

tluo-github commented 6 years ago

@Jovons hi I registered on eurek1. modify two files:application.properties,ZuulSampleModule.java. application.properties

eureka.serviceUrl.default=http://localhost:8000/eureka

ZuulSampleModule.java

bind(EurekaInstanceConfig.class).to(MyDataCenterInstanceConfig.class);

But ribbon forwarding uses static configuration:

ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
edu-microsevice-1.ribbon.DeploymentContextBasedVipAddresses=edu-microsevice-1
edu-microsevice-2.ribbon.DeploymentContextBasedVipAddresses=edu-microsevice-2

com.netflix.zuul.sample.filters.inbound.Routes.groovy

// Route healthchecks to the healthcheck endpoint.;
        if (path.equalsIgnoreCase("/healthcheck")) {
            context.setEndpoint(Healthcheck.class.getCanonicalName())
        }
        else if (serviceId == "edu-microsevice-1") {
            context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME)
            context.setRouteVIP("edu-microsevice-1")
        }
        else if(serviceId == "edu-microsevice-2"){

            context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME)
            context.setRouteVIP("edu-microsevice-2")
        }
        else {
            context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME)
            context.setRouteVIP("api")
        }

Do you know how to configure dynamically? the same issues https://github.com/Netflix/zuul/issues/442

ikonglong commented 6 years ago

@tluolovembtan I referred to your comments, and I set up a eureka server v2 on my localhost, then I changed application.properties as below:

region=default

eureka.name=zuul
eureka.port=7001
eureka.vipAddress=${eureka.name}:${eureka.port}
eureka.preferSameZone=true
eureka.registration.enabled=true

# Loading Filters
zuul.filters.root=src/main/groovy/com/netflix/zuul/sample/filters
zuul.filters.locations=${zuul.filters.root}/inbound,${zuul.filters.root}/outbound,${zuul.filters.root}/endpoint
zuul.filters.packages=com.netflix.zuul.filters.common

eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8080/eureka/v2/

api.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
api.ribbon.DeploymentContextBasedVipAddresses=${eureka.name}:${eureka.port}

And I revised ZuulSampleModule.java as below:

public class ZuulSampleModule extends AbstractModule {
    @Override
    protected void configure() {
        // Add the following line
        bind(EurekaInstanceConfig.class).to(MyDataCenterInstanceConfig.class);
    }
}

Then the sample app started successfully! But I don't know how to play with it. I think I misunderstood the sample app before. Now I think it's a proxy, and also a consumer that consumes the backend services. So the prop 'api.ribbon.DeploymentContextBasedVipAddresses' must point to the targeted backend service which contains some available endpoints I can play with. Is it right?

I set [api.ribbon.DeploymentContextBasedVipAddresses] to the local running sample app, but it doesn't expose any service endpoint. So I can't play with it. Is it right?

And so the prop 'api.ribbon.DeploymentContextBasedVipAddresses' must point to 'api-test.netflix.net:7001' . Is it right?

tluo-github commented 6 years ago

I built a microservices environment using spring cloud components. eg : spring cloud zuul, spring cloud eureka , spring cloud config , and other spring boot microservices.So I have to solve two problems。

Q1. Zuul 2 Register to eureka1. The solution is as above。

Q2.Dynamic route eureka service. I customize NettyOriginManager.java

CustomNettyOrigin.java

package com.example.eduzuul2.extension;

import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.spectator.api.Registry;
import com.netflix.zuul.origins.BasicNettyOrigin;

public class CustomNettyOrigin extends BasicNettyOrigin {

    public CustomNettyOrigin(String name, String vip, Registry registry) {
        super(name, vip, registry);
    }

    @Override
    protected IClientConfig setupClientConfig(String name) {

        DefaultClientConfigImpl niwsClientConfig = DefaultClientConfigImpl.getClientConfigWithDefaultValues(name);
        niwsClientConfig.set(CommonClientConfigKey.ClientClassName, name);
        niwsClientConfig.loadProperties(name);
        //如果不在配置文件中,默认serviceId = vip
        // routerVip 和 serviceId的映射关系可以自己定制
        if (niwsClientConfig.resolveDeploymentContextbasedVipAddresses() == null) {
            niwsClientConfig.setProperty(CommonClientConfigKey.DeploymentContextBasedVipAddresses, name);
        }

        return niwsClientConfig;
    }
}

CustomNettyOriginManager.java

package com.example.eduzuul2.extension;

import com.netflix.spectator.api.Registry;
import com.netflix.zuul.context.SessionContext;
import com.netflix.zuul.origins.OriginManager;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.concurrent.ConcurrentHashMap;

@Singleton
public class CustomNettyOriginManager implements OriginManager<CustomNettyOrigin> {
    private final Registry registry;
    private final ConcurrentHashMap<String, CustomNettyOrigin> originMappings;

    @Inject
    public CustomNettyOriginManager(Registry registry) {
        this.registry = registry;
        this.originMappings = new ConcurrentHashMap();
    }

    public CustomNettyOrigin getOrigin(String name, String vip, String uri, SessionContext ctx) {
        return (CustomNettyOrigin)this.originMappings.computeIfAbsent(name, (n) -> {
            return this.createOrigin(name, vip, uri, false, ctx);
        });
    }

    public CustomNettyOrigin createOrigin(String name, String vip, String uri, boolean useFullVipName, SessionContext ctx) {
        return new CustomNettyOrigin(name, vip, this.registry);
    }
}

ZuulSampleModule.java

/*
 * Copyright 2018 Netflix, Inc.
 *
 *      Licensed 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 com.netflix.zuul.sample;

import com.example.eduzuul2.extension.CustomNettyOriginManager;
import com.google.inject.AbstractModule;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.MyDataCenterInstanceConfig;
import com.netflix.discovery.AbstractDiscoveryClientOptionalArgs;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.netty.common.accesslog.AccessLogPublisher;
import com.netflix.netty.common.status.ServerStatusManager;
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.zuul.BasicRequestCompleteHandler;
import com.netflix.zuul.FilterFileManager;
import com.netflix.zuul.RequestCompleteHandler;
import com.netflix.zuul.context.SessionContextDecorator;
import com.netflix.zuul.context.ZuulSessionContextDecorator;
import com.netflix.zuul.init.ZuulFiltersModule;
import com.netflix.zuul.netty.server.BaseServerStartup;
import com.netflix.zuul.netty.server.ClientRequestReceiver;
import com.netflix.zuul.origins.BasicNettyOriginManager;
import com.netflix.zuul.origins.OriginManager;
import com.netflix.zuul.stats.BasicRequestMetricsPublisher;
import com.netflix.zuul.stats.RequestMetricsPublisher;

/**
 * Zuul Sample Module
 *
 * Author: Arthur Gonigberg
 * Date: November 20, 2017
 */
public class ZuulSampleModule extends AbstractModule {
    @Override
    protected void configure() {
        // sample specific bindings
        bind(BaseServerStartup.class).to(SampleServerStartup.class);

        // use provided basic netty origin manager
        // bind(OriginManager.class).to(BasicNettyOriginManager.class);
        bind(OriginManager.class).to(CustomNettyOriginManager.class);

        // zuul filter loading
        install(new ZuulFiltersModule());
        bind(FilterFileManager.class).asEagerSingleton();

        // general server bindings
        bind(ServerStatusManager.class); // health/discovery status
        bind(SessionContextDecorator.class).to(ZuulSessionContextDecorator.class); // decorate new sessions when requests come in
        bind(Registry.class).to(DefaultRegistry.class); // atlas metrics registry
        bind(RequestCompleteHandler.class).to(BasicRequestCompleteHandler.class); // metrics post-request completion
        bind(AbstractDiscoveryClientOptionalArgs.class).to(DiscoveryClient.DiscoveryClientOptionalArgs.class); // discovery client
        bind(RequestMetricsPublisher.class).to(BasicRequestMetricsPublisher.class); // timings publisher

        bind(EurekaInstanceConfig.class).to(MyDataCenterInstanceConfig.class);

        // access logger, including request ID generator
        bind(AccessLogPublisher.class).toInstance(new AccessLogPublisher("ACCESS",
                (channel, httpRequest) -> ClientRequestReceiver.getRequestFromChannel(channel).getContext().getUUID()));
    }
}
artgon commented 6 years ago

There seems to be some confusion here about Eureka 1/2. To be clear, Zuul 2 uses Eureka 1.

The path discovery/v2 is not referring to Eureka version but rather data model for the RESTful API.

Also, I'm going to add a guide for routing to clarify the Ribbon configuration.

devup2 commented 6 years ago

I am unable to run zuul 2 sample with eureka 1 as well.

Filter sets route vip for /gadget as: context.setEndpoint(ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME) context.setRouteVIP("api")

Configs: eureka.serviceUrl.default=http://localhost:8761/eureka api.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList api.ribbon.DeploymentContextBasedVipAddresses=localhost:${eureka.port}

When accessing http://localhost:9000/api/status returns 502 2018-06-19 17:32:11,559 WARN com.netflix.loadbalancer.BaseLoadBalancer [Salamander-ClientToZuulWorker-0] LoadBalancer [gadget]: Error choosing server for key null Error is thrown due to key being null : https://github.com/Netflix/zuul/blob/2.1/zuul-core/src/main/java/com/netflix/zuul/netty/connectionpool/DefaultClientChannelManager.java#L328

This method is being called from two other locations which set the key to null. a) https://github.com/Netflix/zuul/blob/2.1/zuul-core/src/main/java/com/netflix/zuul/netty/connectionpool/DefaultClientChannelManager.java#L309 b)https://github.com/Netflix/zuul/blob/2.1/zuul-core/src/main/java/com/netflix/zuul/origins/BasicNettyOrigin.java#L121

artgon commented 6 years ago

It's not failing because the key is null. The key will generally be null unless you use a balancing strategy other than round robin. It's either throwing an exception somewhere or can't find any servers in discovery.