关于java:spring-cloud-gateway路由过滤器

40次阅读

共计 2424 个字符,预计需要花费 7 分钟才能阅读完成。

1 pom.xml 中引入依赖, 最新 GA 版本

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
      <version>3.1.0</version>
    </dependency>

2 启动类

@SpringBootApplication
public class DemoApplication {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {return builder.routes().route("routeA", r -> r.path("/pathA")
        .uri("http://www.a.com")).build();}

    public static void main(String[] args) {System.setProperty("reactor.netty.ioWorkerCount", "n");
        SpringApplication.run(DemoApplication.class, args);
    }

}

3 yaml

server:
    port: 80
logging:
    level:
        org.springframework.cloud.gateway: INFO
        org.springframework.http.server.reactive: INFO
        org.springframework.web.reactive: INFO
        reactor.ipc.netty: INFO
        reactor.netty: INFO
spring:
    cloud:
        gateway:
            routes:
                - id: routeB
                  uri: http://www.b.com
                  predicates:
                      - Path=/pathB

4 启动
这样启动后具备一个最简略的工程能力,外面具备两条动态路由性能:
pathA 转发至 www.a.com
pathB 转发至 www.b.com

5 动静路由
需路实现该接口:

package org.springframework.cloud.gateway.route;

public interface RouteDefinitionRepository extends RouteDefinitionLocator, RouteDefinitionWriter {}

接下来来一个简略的小例子看一下:

@Component
public class DemoRouteDefinitionRepository implements RouteDefinitionRepository {
    private int idx = 1;

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {System.out.println("=========== 执行 getRouteDefinitions");
        RouteDefinition definition = new RouteDefinition();
        definition.setId("routeC");
        if (idx++ % 2 == 0) {definition.setUri(URI.create("http://www.c1.com"));
        } else {definition.setUri(URI.create("http://www.c2.com"));
        }

        PredicateDefinition predicate = new PredicateDefinition();
        predicate.setName("Path");
        Map<String, String> map = new HashMap<>();
        map.put("pattern", "/pathC");
        predicate.setArgs(map);

        definition.setPredicates(Arrays.asList(predicate));
        return Flux.fromIterable(Arrays.asList(definition));
    }

    @Override
    public Mono<Void> save(Mono<RouteDefinition> route) {return null;}

    @Override
    public Mono<Void> delete(Mono<String> routeId) {return null;}

}

能够看到 getRouteDefinitions()提供路由信息,启动的时候会读取一次。在这里为模仿一下动静的情景,须要有事件去触发调用 getRouteDefinitions()来更新路由,触发的动作是发送一个 RefreshRoutesEvent 事件:

@RestController
public class HomeController implements ApplicationEventPublisherAware{
    private ApplicationEventPublisher publisher;

    @GetMapping("/change")
    public Mono<Void> msg() {publisher.publishEvent(new RefreshRoutesEvent(this));
        return Mono.empty();}

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {this.publisher = applicationEventPublisher;}
}

每调用一次 /change,路由信息会变动一次,当然,也能够减少一条或者删除一条路由信息,本例子中 save、delete 都未实现。

正文完
 0