Spring Cloud 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;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/get")
.uri("http://httpbin.org"))
.build();
}
}
这段代码定义了一个Spring Cloud Gateway应用程序,它使用RouteLocatorBuilder
来创建一个路由,这个路由会将匹配/get
路径的请求转发到http://httpbin.org
。这是一个简单的入门示例,展示了如何通过代码配置路由规则。
评论已关闭