These are chat archives for spring-cloud/spring-cloud

9th
Feb 2019
Archimedes Trajano
@trajano
Feb 09 05:45
Spencer Gibb
@spencergibb
Feb 09 17:05
@trajano RE: serviceId and metadata, not sure what you mean, can you elaborate?
Archimedes Trajano
@trajano
Feb 09 17:29
@spencergibb my current workaround is this
private static final Pattern DISCOVERY_CLIENT_ID_PATTERN = Pattern.compile("CompositeDiscoveryClient_(.+)");
    @Override
    public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain) {
        Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);

        final ServiceInstance serviceInstance;
        assert route != null;
        final Matcher m = DISCOVERY_CLIENT_ID_PATTERN.matcher(route.getId());
        if (m.matches()) {
            serviceInstance = discoveryClient.getInstances(m.group(1)).get(0);
        } else {
            log.trace("Unable to determine service instance for request");
            return chain.filter(exchange);
        }

        final Map<String, String> metadata = serviceInstance.getMetadata();
        final String serviceId = metadata.get(Labels.SERVICE_ID);
basically I am making an assumption that the service ID will be part of the Route ID and have a specific prefix.
Spencer Gibb
@spencergibb
Feb 09 17:36
If you use the discovery client route locator you can customize the predicates and filters. The context of the Spring Expression Language (SpEL) expressions is a ServiceInstance. https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/multi/multi__configuration_2.html#_discoveryclient_route_definition_locator
Archimedes Trajano
@trajano
Feb 09 17:36
So far I did get my objective done which was to create a discoveryclient that works with Docker Swarm
Spencer Gibb
@spencergibb
Feb 09 17:37
You could add your custom filter with a parameter like metadata['serviceId'] or whatever is in Labels.SERVICE_ID
I'd be interested to see that discovery client
Archimedes Trajano
@trajano
Feb 09 17:40
@spencergibb actually that link you gave isn't a good one to find out what I needed. It's this one I can probably change spring.cloud.gateway.discovery.locator.route-id-prefix which is documented here http://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/multi/multi__appendix_compendium_of_configuration_properties.html
just one line there :)
Spencer Gibb
@spencergibb
Feb 09 17:41
But I'm saying that the discovery client route locator already does what your filter does (get a ServiceInstance for a particular service) and rather than do it again, you can get the information at the same time
there you go, made a gist for you
I actually need it as part of my filter because the filter is the one that needs the service instance.
Unless your claim is that the client route locator can be autowired
In which case... let me check what I can do there
Spencer Gibb
@spencergibb
Feb 09 17:44
You can add a filter definition
Archimedes Trajano
@trajano
Feb 09 17:45
Hmm not sure what you mean. I only started working on Spring Cloud Gateway a few days ago
Like I wrote my first question here a few hours after I started.
Spencer Gibb
@spencergibb
Feb 09 17:45
I'm saying you can avoid doing the work twice, but I don't have time at the moment to show a proper sample
Archimedes Trajano
@trajano
Feb 09 17:46
What's a "filter definition"? :)
Maybe that would help
Spencer Gibb
@spencergibb
Feb 09 17:47
spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.predicates[1].name: Host
spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"
spring.cloud.gateway.discovery.locator.filters[0].name: Hystrix
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"
defines two filters per route
Archimedes Trajano
@trajano
Feb 09 17:58
So instead of a global filter, I create a discovery locator filter.
Spencer Gibb
@spencergibb
Feb 09 17:58
Just a normal filter you assign each route
Archimedes Trajano
@trajano
Feb 09 17:59
Hmm let me see how this would go thanks.
For this I have to figure out how to register a filter by name.
I think.
Shouldn't be hard to find
Archimedes Trajano
@trajano
Feb 09 19:48
Woo! getting there
@spencergibb using your RewritePathGatewayFilterFactory as reference
Archimedes Trajano
@trajano
Feb 09 19:59
Thanks I ported my code over, a few less lines but I can see more of what i can do from there.