【问题标题】:spring cloud gateway and eureka serverspring 云网关和尤里卡服务器
【发布时间】:2018-02-17 05:18:36
【问题描述】:

我一直在尝试找到与 eureka 服务器以及一些 Hystrix 示例集成的 Spring Cloud Gateway 的运行示例,但到目前为止我找不到。 有什么地方可以找到吗?我真的很想看到使用 Spring Cloud Gateway,替换我当前的 Zuul API 服务。

谢谢!

【问题讨论】:

标签: spring-boot spring-cloud netflix-eureka netflix-zuul spring-cloud-netflix


【解决方案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

【讨论】:

  • 这很好用。这是我相信的格林威治版本。
【解决方案2】:

在 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();
}

【讨论】:

    【解决方案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 的实际情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-02
      • 2018-04-15
      • 2019-11-16
      • 1970-01-01
      • 2019-02-15
      • 2017-09-23
      • 1970-01-01
      • 2015-09-29
      相关资源
      最近更新 更多