我一直试图找到一个与eureka服务器集成的Spring cloud网关的运行示例,以及一些Hystrix示例,但到目前为止我还无法找到。 有什么地方可以找到它吗?我真的希望看到正在使用的Spring cloud网关,取代我目前的Zuul API服务。
谢谢!
答案 0 :(得分:7)
在Finchley.M5中,API已更改
@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();
}
答案 1 :(得分:3)
您可以将Spring Cloud Gateway与Spring Cloud Config和Spring Cloud Eureka结合使用。通过这种方式,网关的配置可能如下所示:
@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();
}
并且,如spencergibb所述,添加发现功能:
@Bean
public DiscoveryClientRouteDefinitionLocator discoveryClientRouteLocator(DiscoveryClient discoveryClient) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient);
}
这对Finchley.M3来说是实际的。
答案 2 :(得分:1)
此配置对我有用:
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>
代码
@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);
}
}
属性
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true