一 源码位置
https://github.com/cakin24/spring-cloud-code/tree/master/ch17-2/ch17-2-3-gateway
二 关键代码
package cn.springcloud.book.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@SpringBootApplication
public class SCGatewayApplication {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
// 主要是下面这几行代码
ZonedDateTime datetime1 = LocalDateTime.now().minusDays(1).atZone(ZoneId.systemDefault());
ZonedDateTime datetime2 = LocalDateTime.now().plusDays(1).atZone(ZoneId.systemDefault());
return builder.routes()
.route("between_route", r -> r.between(datetime1,datetime2)
.uri("http://baidu.com"))
.build();
}
public static void main(String[] args) {
SpringApplication.run(SCGatewayApplication.class, args);
}
}
三 测试
1 启动ch17-2/ch17-2-3-gateway
2 浏览器输入localhost:8080
3 页面跳转到百度主页,测试成功。