Search in sources :

Example 6 with Endpoint

use of io.gravitee.definition.model.Endpoint in project gravitee-gateway by gravitee-io.

the class EndpointDiscoveryConsulVerticle method startWatch.

private void startWatch(Api api) {
    EndpointDiscoveryService discoveryService = api.getServices().get(EndpointDiscoveryService.class);
    if (api.isEnabled() && discoveryService != null && discoveryService.getProvider() == EndpointDiscoveryProvider.CONSUL) {
        LOGGER.info("Add Consul.io support for API id[{}] name[{}]", api.getId(), api.getName());
        ConsulEndpointDiscoveryConfiguration providerConfiguration = (ConsulEndpointDiscoveryConfiguration) discoveryService.getConfiguration();
        URI consulUri = URI.create(providerConfiguration.getUrl());
        ConsulClientOptions options = new ConsulClientOptions().setHost(consulUri.getHost()).setPort((consulUri.getPort() != -1) ? consulUri.getPort() : CONSUL_DEFAULT_PORT).setDc(providerConfiguration.getDc()).setAclToken(providerConfiguration.getAcl());
        if (HTTPS_SCHEME.equalsIgnoreCase(consulUri.getScheme())) {
            // SSL is not configured but the endpoint scheme is HTTPS so let's enable the SSL on Vert.x HTTP client
            // automatically
            options.setSsl(true).setTrustAll(true);
        }
        LOGGER.info("Consul.io configuration: endpoint[{}] dc[{}] acl[{}]", consulUri.toString(), options.getDc(), options.getAclToken());
        Watch<ServiceEntryList> watch = Watch.service(providerConfiguration.getService(), vertx, options);
        watch.setHandler(event -> {
            if (event.succeeded()) {
                LOGGER.debug("Receiving a Consul.io watch event for service: name[{}]", providerConfiguration.getService());
                List<ServiceEntry> entries = event.nextResult().getList();
                // Handle new services or updated services
                for (ServiceEntry service : event.nextResult().getList()) {
                    Service service1 = service.getService();
                    service1.setNodeAddress(service.getNode().getAddress());
                    handleRegisterService(api, service1);
                }
                // Handle de-registered services
                if (event.prevResult() != null) {
                    List<ServiceEntry> oldEntries = event.prevResult().getList();
                    if (oldEntries.size() > entries.size()) {
                        // Select only de-registered services
                        oldEntries.removeAll(entries);
                        for (ServiceEntry oldEntry : oldEntries) {
                            handleDeregisterService(api, oldEntry.getService());
                        }
                    }
                }
            } else {
                LOGGER.info("Unexpected error while watching services catalog", event.cause());
            }
        }).start();
        this.watches.put(api.getId(), watch);
    }
}
Also used : ConsulEndpointDiscoveryConfiguration(io.gravitee.definition.model.services.discovery.consul.ConsulEndpointDiscoveryConfiguration) EventListener(io.gravitee.common.event.EventListener) HttpClientOptions(io.gravitee.definition.model.HttpClientOptions) Logger(org.slf4j.Logger) EndpointDiscoveryService(io.gravitee.definition.model.services.discovery.EndpointDiscoveryService) EndpointDiscoveryProvider(io.gravitee.definition.model.services.discovery.EndpointDiscoveryProvider) LoggerFactory(org.slf4j.LoggerFactory) Event(io.gravitee.common.event.Event) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) ConsulEndpointDiscoveryConfiguration(io.gravitee.definition.model.services.discovery.consul.ConsulEndpointDiscoveryConfiguration) HashMap(java.util.HashMap) DiscoveredEndpoint(io.gravitee.gateway.services.endpoint.discovery.consul.endpoint.DiscoveredEndpoint) Future(io.vertx.core.Future) io.vertx.ext.consul(io.vertx.ext.consul) Endpoint(io.gravitee.definition.model.Endpoint) List(java.util.List) Reactable(io.gravitee.gateway.reactor.Reactable) EventManager(io.gravitee.common.event.EventManager) AbstractVerticle(io.vertx.core.AbstractVerticle) Map(java.util.Map) Api(io.gravitee.gateway.handlers.api.definition.Api) URI(java.net.URI) ReactorEvent(io.gravitee.gateway.reactor.ReactorEvent) EndpointDiscoveryService(io.gravitee.definition.model.services.discovery.EndpointDiscoveryService) EndpointDiscoveryService(io.gravitee.definition.model.services.discovery.EndpointDiscoveryService) List(java.util.List) URI(java.net.URI)

Example 7 with Endpoint

use of io.gravitee.definition.model.Endpoint in project gravitee-gateway by gravitee-io.

the class EndpointHealthcheckResolver method resolve.

/**
 * Returns a {@link Stream} of {@link Endpoint} which have to be health-checked.
 *
 * @param api
 * @return
 */
public List<EndpointRule> resolve(Api api) {
    HealthCheckService rootHealthCheck = api.getServices().get(HealthCheckService.class);
    Stream<Endpoint> endpoints = api.getProxy().getEndpoints().stream();
    // Only HTTP endpoint
    Stream<HttpEndpoint> httpEndpoints = endpoints.filter(endpoint -> endpoint.getType() == EndpointType.HTTP).map(endpoint -> (HttpEndpoint) endpoint);
    // Filtering endpoints according to tenancy configuration
    if (gatewayConfiguration.tenant().isPresent()) {
        String tenant = gatewayConfiguration.tenant().get();
        httpEndpoints = httpEndpoints.filter(endpoint -> endpoint.getTenants() != null && endpoint.getTenants().contains(tenant));
    }
    // Remove backup endpoints
    httpEndpoints = httpEndpoints.filter(endpoint -> !endpoint.isBackup());
    // Keep only endpoints where health-check is enabled or not settled (inherit from service)
    httpEndpoints = httpEndpoints.filter(endpoint -> (endpoint.getHealthCheck() == null) || (endpoint.getHealthCheck() != null && endpoint.getHealthCheck().isEnabled()));
    return httpEndpoints.map((Function<HttpEndpoint, EndpointRule>) endpoint -> new DefaultEndpointRule(api.getId(), endpoint, (endpoint.getHealthCheck() == null || endpoint.getHealthCheck().isInherit()) ? rootHealthCheck : endpoint.getHealthCheck())).collect(Collectors.toList());
}
Also used : Endpoint(io.gravitee.definition.model.Endpoint) List(java.util.List) Stream(java.util.stream.Stream) EndpointType(io.gravitee.definition.model.EndpointType) DefaultEndpointRule(io.gravitee.gateway.services.healthcheck.rule.DefaultEndpointRule) HealthCheckService(io.gravitee.definition.model.services.healthcheck.HealthCheckService) Autowired(org.springframework.beans.factory.annotation.Autowired) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Api(io.gravitee.definition.model.Api) GatewayConfiguration(io.gravitee.gateway.env.GatewayConfiguration) Function(java.util.function.Function) DefaultEndpointRule(io.gravitee.gateway.services.healthcheck.rule.DefaultEndpointRule) HealthCheckService(io.gravitee.definition.model.services.healthcheck.HealthCheckService) Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint)

Example 8 with Endpoint

use of io.gravitee.definition.model.Endpoint in project gravitee-gateway by gravitee-io.

the class RequestResponseInvalidContentTest method before.

@Override
public void before(Api api) {
    super.before(api);
    try {
        Endpoint edpt = api.getProxy().getEndpoints().iterator().next();
        URL target = new URL(edpt.getTarget());
        URL newTarget = new URL(target.getProtocol(), target.getHost(), wireMockRule.port(), target.getFile());
        edpt.setTarget(newTarget.toString());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Endpoint(io.gravitee.definition.model.Endpoint) URL(java.net.URL)

Example 9 with Endpoint

use of io.gravitee.definition.model.Endpoint in project gravitee-gateway by gravitee-io.

the class EndpointStatusDecoratorTest method testEndpointStatus_upAfterTransitionallyDownAndFinallyDown.

@Test
public void testEndpointStatus_upAfterTransitionallyDownAndFinallyDown() {
    Endpoint endpoint = createEndpoint();
    EndpointStatusDecorator manager = new EndpointStatusDecorator(endpoint);
    manager.updateStatus(false);
    Assert.assertEquals(Endpoint.Status.TRANSITIONALLY_DOWN, endpoint.getStatus());
    manager.updateStatus(false);
    Assert.assertEquals(Endpoint.Status.TRANSITIONALLY_DOWN, endpoint.getStatus());
    manager.updateStatus(false);
    Assert.assertEquals(Endpoint.Status.DOWN, endpoint.getStatus());
}
Also used : Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Test(org.junit.Test)

Example 10 with Endpoint

use of io.gravitee.definition.model.Endpoint in project gravitee-gateway by gravitee-io.

the class EndpointStatusDecoratorTest method testEndpointStatus_up.

@Test
public void testEndpointStatus_up() {
    Endpoint endpoint = createEndpoint();
    EndpointStatusDecorator manager = new EndpointStatusDecorator(endpoint);
    manager.updateStatus(true);
    Assert.assertEquals(Endpoint.Status.UP, endpoint.getStatus());
}
Also used : Endpoint(io.gravitee.definition.model.Endpoint) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) Test(org.junit.Test)

Aggregations

Endpoint (io.gravitee.definition.model.Endpoint)17 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)12 Test (org.junit.Test)10 DiscoveredEndpoint (io.gravitee.gateway.services.endpoint.discovery.consul.endpoint.DiscoveredEndpoint)3 URL (java.net.URL)3 Api (io.gravitee.gateway.handlers.api.definition.Api)2 List (java.util.List)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Event (io.gravitee.common.event.Event)1 EventListener (io.gravitee.common.event.EventListener)1 EventManager (io.gravitee.common.event.EventManager)1 GraviteeMapper (io.gravitee.definition.jackson.datatype.GraviteeMapper)1 Api (io.gravitee.definition.model.Api)1 EndpointType (io.gravitee.definition.model.EndpointType)1 HttpClientOptions (io.gravitee.definition.model.HttpClientOptions)1 EndpointDiscoveryProvider (io.gravitee.definition.model.services.discovery.EndpointDiscoveryProvider)1 EndpointDiscoveryService (io.gravitee.definition.model.services.discovery.EndpointDiscoveryService)1 ConsulEndpointDiscoveryConfiguration (io.gravitee.definition.model.services.discovery.consul.ConsulEndpointDiscoveryConfiguration)1 HealthCheckService (io.gravitee.definition.model.services.healthcheck.HealthCheckService)1 Request (io.gravitee.definition.model.services.healthcheck.Request)1