spring cloud gateway and eureka server
Asked Answered
K

3

7

I have been trying to find a running example of spring cloud gateway integrated with eureka server and also with some Hystrix examples but I could't find so far. Are there any place where I could find it? I really would like to see spring cloud gateway in use, replacing my current Zuul API service.

Thanks!

Kilbride answered 8/9, 2017 at 9:58 Comment(5)
It's a pre release, there probably aren't any samples beyond the docs and tests.Cobden
Hey @spencergibb, thanks for replying. I couldn't find much on the docs and tests, so I'll probably have to wait the release.Kilbride
did you look in the 2.0.x branch?Cobden
github.com/spring-cloud/spring-cloud-gateway/blob/2.0.x/docs/… github.com/spring-cloud/spring-cloud-gateway/blob/2.0.x/docs/… github.com/spring-cloud/spring-cloud-gateway/blob/2.0.x/…Cobden
github.com/spring-cloud/spring-cloud-gateway/blob/2.0.x/…Cobden
A
7

In Finchley.M5, API has changed

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder)
{
    GatewayFilter filter = new RewritePathGatewayFilterFactory()
            .apply("/admin/(?<segment>.*)", "/${segment}");

    return builder.routes()
            .route(r -> r.path("/admin/**")
                    .filter(filter)
                    //.uri("http://localhost:3000"))
                    .uri("lb://admin"))  // with load balancer through Eureka
            .build();
}
Amarillis answered 20/12, 2017 at 14:52 Comment(0)
G
4

You can use Spring Cloud Gateway in conjunction with Spring Cloud Config and Spring Cloud Eureka. In such way, configuration of a Gateway may look like:

@Bean
public RouteLocator customRouteLocator(
   return Routes.locator()
        .route("admin")
        .predicate(path("/admin/**"))
        .filter(rewritePath("/admin/(?<segment>.*)", "/${segment}"))
        //.uri("http://localhost:3000")
        .uri("lb://admin") // as registered in Eureka
        .build();
}

And, as was said by spencergibb, add the discovery capability:

@Bean
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteLocator(DiscoveryClient discoveryClient) {
    return new DiscoveryClientRouteDefinitionLocator(discoveryClient);
}

This is actual for Finchley.M3.

Grison answered 13/11, 2017 at 15:58 Comment(0)
G
2

This configuration is working for me:

Pom

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-transport-native-epoll</artifactId>
        <classifier>linux-x86_64</classifier>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
 </dependencies>

<dependencyManagement>
    <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.RC1</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>

Code

@SpringBootApplication
@Configuration
@EnableDiscoveryClient
public class GatewayApplication {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {

        return builder.routes()
                .route(
                        r -> r.path("/xxxxxs/**")
                                .uri("lb://xxxx-service")
                )
                .route(
                        r -> r.path("/yyyyyys/**")
                                .uri("lb://yyyyyy-service")
                )
                .route(
                        r -> r.path("/vvvvvs/**")
                                .uri("lb://vvvvvv-service")
                )
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

properties

eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
Gape answered 25/6, 2018 at 7:52 Comment(1)
This worked beautifully. This is the Greenwich release I believe.Azaleah

© 2022 - 2024 — McMap. All rights reserved.