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

19th
Sep 2018
Tommy Ludwig
@shakuzen
Sep 19 2018 07:44
Is the abscence of a Finchley.SR2 on the milestones page an indication there is no currently planned release date for SR2? https://github.com/spring-cloud/spring-cloud-release/milestones?state=open
Marcin Grzejszczak
@marcingrzejszczak
Sep 19 2018 13:30
there will be one
Andrew Fitzgerald
@fitzoh
Sep 19 2018 13:36
@spencergibb I’m under the impression that there’s nothing in SCG to allow sticky routing, is that correct?
Spencer Gibb
@spencergibb
Sep 19 2018 15:29
@Fitzoh no
Andrew Fitzgerald
@fitzoh
Sep 19 2018 15:31
cool
might write something up on the approach we’re using if there’s potentially interest
Spencer Gibb
@spencergibb
Sep 19 2018 15:31
:+1:
Andrew Fitzgerald
@fitzoh
Sep 19 2018 15:32
short version:
/**
 * Uses cookies to ensure that the same server is routed to for all subsequent requests.
 * On the initial request when no cookie is set, a random server id is chosen and sent back to the client.
 * On subsequent requests, the cookie is validated to make sure the server is still active.
 * If the cookie is invalid, a new one is generated.
 * <p>
 * Also sets the {@link StickyRoutingFilter#GATEWAY_ROUTING_ATTRIBUTE } for use in {@link CfUrlDeterminer}.
 */
@Component
public class StickyRoutingFilter implements GatewayFilter {
    public static final String GATEWAY_ROUTING_ATTRIBUTE = "connect-gateway-routing";
    public static final String GATEWAY_ROUTING_COOKIE = "connect-gateway-routing";
    private final ServerList serverList;

    public StickyRoutingFilter(ServerList serverList) {
        this.serverList = serverList;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        modifyExchange(exchange);


        System.out.println("in filter");
        System.out.println(exchange.getRequest().getURI().getPath());

        return chain.filter(exchange);
    }

    public void modifyExchange(ServerWebExchange exchange) {
        String serverIdentifier = getExistingCookieValue(exchange)
                .filter(serverList::isValidServerIdentifier)
                .orElseGet(() -> {
                    String server = serverList.getRandomServerIdentifier();
                    exchange.getResponse().addCookie(ResponseCookie.from(GATEWAY_ROUTING_COOKIE, server).build());
                    return server;
                });
        exchange.getAttributes().put(GATEWAY_ROUTING_ATTRIBUTE, serverIdentifier);

    }

    private Optional<String> getExistingCookieValue(ServerWebExchange exchange) {
        List<HttpCookie> cookies = exchange.getRequest().getCookies().get(GATEWAY_ROUTING_COOKIE);
        if (cookies == null || cookies.isEmpty()) {
            return Optional.empty();
        }
        return Optional.of(cookies.get(0).getValue());
    }
}
Eric Deandrea
@edeandrea
Sep 19 2018 20:52
Can a Spring boot EnvironmentPostProcessor read values that are provided from a config server by spring-cloud-config-client? When debugging I don’t see the configService property source in the ConfigurableEnvironment.getPropertySources()