一、建立springboot项目添加依赖java
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
二、配置application.ymlweb
server: port: 5000 spring: application: name: hello-gateway cloud: gateway: routes: - id: gateway1 uri: https://www.jianshu.com/p/44a0d6adcdea predicates: - Path=/p/44a0d6adcdea
三、配置启动类spring
@SpringBootApplication public class HelloGatewayApplication { public static void main(String[] args) { SpringApplication.run(HelloGatewayApplication.class, args); } @Bean public RouteLocator routeLocator(RouteLocatorBuilder builder){ return builder.routes() .route(r->r.path("/java-concurrent").uri("http://ifeve.com")) .build(); } }
项目springboot
访问http://localhost:5000/p/44a0d6adcdea、http://ifeve.com/java-concurrent/app
总结访问http://localhost:5000/p/44a0d6adcdea则会转发到https://www.jianshu.com/p/44a0d6adcdeaspring-boot
访问http://ifeve.com/java-concurrent/,则会转发到http://ifeve.com/java-concurrent/ui
一、能够看出,地址栏中的url是不会变的,说明gateway是转发而不是重定向url
二、同时能够发现gateway不会组件不像springcloud其余组建要使用@EnableXXX,如eureka要使用@EnableEurekaServer 、 Feign要使用@EnableFeignClients等等server
三、这个项目能够看出gateway支持两种方式yml、和配置类flux