Declaring Routes in Code Using Only Zuul 2

Viewed 34

I'm trying to declare routes in a Zuul 2 example project using code rather than a properties file. The example project was taken from here. I'd like to avoid using any Eureka, Spring, or Spring Cloud libraries. Is there a good way to do this?

The relevant bits are

application.properties

zuul.server.port.main=9001

# Configure filters
zuul.filters.root=src/main/groovy/info/novatec/zuul2/filters
zuul.filters.locations=${zuul.filters.root}/inbound,${zuul.filters.root}/outbound,${zuul.filters.root}/endpoint
zuul.filters.packages=com.netflix.zuul.filters.common

# Routing to proxied backend services
hello.ribbon.listOfServers=localhost:8081
hello.ribbon.client.NIWSServerListClassName=com.netflix.loadbalancer.ConfigurationBasedServerList

# Deactivate Eureka
eureka.shouldFetchRegistry=false
eureka.validateInstanceId=false
@next=application-${@environment}.properties

Routes.groovy

package info.novatec.zuul2.filters.inbound

import com.netflix.zuul.context.SessionContext
import com.netflix.zuul.filters.endpoint.ProxyEndpoint
import com.netflix.zuul.filters.http.HttpInboundSyncFilter
import com.netflix.zuul.message.http.HttpRequestMessage

/**
 * Routing filter on base of HttpInboundSyncFilter.<br/>
 * URIs of the proxied backend services are defined in {@code application.properties}
 */
class Routes extends HttpInboundSyncFilter {

    @Override
    HttpRequestMessage apply(HttpRequestMessage request) {
        SessionContext context = request.getContext()
        String path = request.getPath()

        context.setEndpoint(ProxyEndpoint.class.getCanonicalName())
        context.setRouteVIP(path.substring(1))

        return request
    }

    @Override
    int filterOrder() {
        return 0
    }

    @Override
    boolean shouldFilter(HttpRequestMessage request) {
        return true
    }
}

What I'd like to do is not declare the route at all in application.properties but rather in code, something like how Spring Cloud Gateway does it:

Spring Cloud Gateway route declaration sample:

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        RouteLocator routeLocator = builder.routes()
                .route(r -> r
                        .path("/test")
                        .uri("http://localhost:8080"))
                .build();
        return routeLocator;
    }

Thank you!

0 Answers
Related