SpringCloud Gateway 的初体验
引入gateway
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <groupId>com.ooyhao</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version>
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
|
配置文件配置映射
1 2 3 4 5 6 7 8 9 10 11 12
| spring: application: name: spring-cloud-gateway cloud: gateway: routes: - id: 163_route uri: http://www.163.com/ predicates: - Path=/163 server: port: 8080
|
配置文件配置映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
@Configuration public class RouteConfig {
@Bean public RouteLocator routeLocator(RouteLocatorBuilder builder){ return builder.routes() .route("ooyhao_route",r -> r.path("/164") .uri("http://www.163.com/")) .build(); } }
|
测试:
当我使用https://localhost:8080/163 和 https://localhost:8080/164访问时,都可以转发到https://www.163.com.
效果如下:
总结
这一节主要是简单尝试一下SpringCloud Gateway的使用,利用配置属性文件和配置类的方式来实现路径转发,这是就可以实现屏蔽真实路径的效果。