一 开源项目地址
https://github.com/cakin24/spring-cloud-code/tree/master/ch17-1/ch17-1-1-gateway
二 核心代码
@SpringBootApplication
public class SpringCloudGatewayApplication {
/**
* 基本的转发
* 当访问http://localhost:8080/jd
* 转发到http://jd.com
* @param builder
* @return
*/
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
// 关键是这里,Java流式API定义RouteLocator
return builder.routes()
//basic proxy
.route(r ->r.path("/jd")
.uri("http://jd.com:80/").id("jd_route")
).build();
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudGatewayApplication.class, args);
}
}
三 测试