SpringCloud gateway学习之helloWorld

1、POM.xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>com.demo</groupId>
 6     <artifactId>gateway</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <name>gateway</name>
 9     <description>Demo project for Spring Boot</description>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.1.8.RELEASE</version>
15         <relativePath/> <!-- lookup parent from repository -->
16     </parent>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <!--cloud 引来-->
25     <dependencyManagement>
26         <dependencies>
27             <dependency>
28                 <groupId>org.springframework.cloud</groupId>
29                 <artifactId>spring-cloud-dependencies</artifactId>
30                 <version>Greenwich.SR3</version>
31                 <type>pom</type>
32                 <scope>import</scope>
33             </dependency>
34         </dependencies>
35     </dependencyManagement>
36 
37     <dependencies>
38         <dependency>
39             <groupId>org.springframework.cloud</groupId>
40             <artifactId>spring-cloud-starter-gateway</artifactId>
41         </dependency>
42     </dependencies>
43 
44     <build>
45         <plugins>
46             <plugin>
47                 <groupId>org.springframework.boot</groupId>
48                 <artifactId>spring-boot-maven-plugin</artifactId>
49             </plugin>
50         </plugins>
51     </build>
52 
53 </project>

2、Application.yml配置文件:

 1 spring:
 2   cloud:
 3     gateway:
 4       routes:
 5         - id: app
 6           uri: http://www.baidu.com
 7           predicates:
 8             - Path=/app/**
 9           filters:
10             - StripPrefix=1

3、运行:

运行程序,在浏览器中输入:http://localhost:8080/app,浏览器会自动跳转到http://www.baidu.com

4、坑点:

(1) 最大的坑点就是版本问题,如果没有用对应版本,则运行会报错甚至运行不起来;spring-boot-starter-parent2.0.4.RELEASE版本时,spring-cloud-dependencies要用Finchley.RELEASE;当spring-boot-starter-parent2.1.8.RELEASE版本时,spring-cloud-dependencies要用Greenwich.SR3

(2) spring-cloud-starter-gateway包和spring-boot-starter-web包不能并存,如果两个都依赖了,运行会报错,得删掉spring-boot-starter-web依赖后才能运行起来;

posted on 2019-11-11 14:09  小夏coding  阅读(533)  评论(0编辑  收藏  举报

导航