Reputation: 265
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!
Upvotes: 7
Views: 7488
Reputation: 2417
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
Upvotes: 2
Reputation: 299
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();
}
Upvotes: 7
Reputation: 41
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.
Upvotes: 4