参考
- https://blog.csdn.net/DCTANT/article/details/108125229 (boot与cloud的版本关系)
- https://blog.csdn.net/yuanchangliang/article/details/109579705
- https://blog.csdn.net/qq_38380025/article/details/102968559
- 本文代码 下载
环境
环境 | 版本 | 说明 |
---|---|---|
windows | 10 | |
vs code | 1.85.1 | |
Spring Boot Extension Pack | v0.2.1 | vscode插件 |
Extension Pack for Java | v0.25.15 | vscode插件 |
JDK | 11 | |
Springboot | 2.3.12.RELEASE | |
mybatis-spring-boot-starter | 2.1.4 | mvn依赖 |
spring-cloud-dependencies | Hoxton.SR12 | mvn依赖 |
spring-cloud-starter-gateway | 未设置 | mvn依赖 |
spring-cloud-starter-netflix-hystrix | 未设置 | mvn依赖,本文章使用的多模块开发,网关模块中不引入本依赖会报错 |
spring-boot-dependencies | 2.3.12.RELEASE | mvn依赖 |
Apache Maven | 3.8.6 |
正文
项目为多模块项目,包含子模块:gateway(网关)、common(公共)、api(接口)三个子模块。
步骤
- 根目录准备
- 在项目根目录的 pom.xml
dependencyManagement/dependencies
中指定 Spring Boot、Spring Cloud 与三个子模块的版本号。 - 在项目根目录的 pom.xml
packaging
设置为 pom(没有这个节点就添加上)。 - 在项目根目录的 pom.xml 添加
modules
, 并将三个子模块引入(没有这个节点就添加上)。
- 在项目根目录的 pom.xml
- gateway 子模块
- pom.xml 引入
spring-cloud-starter-gateway
与spring-cloud-starter-netflix-hystrix
依赖。 - pom.xml 一定要删除
spring-boot-starter-web
依赖,不然会导致启动失败。(gateway导入了webflux包,会有冲突) - pom.xml 指定 parent。
- application.properties 文件设置对应配置转发规则:
server.port=8080 # spring.cloud.gateway.routes[0].id=student # 要转发到哪里 spring.cloud.gateway.routes[0].uri=http://127.0.0.1:8081 # 转发的规则,有很多种,这里是转发 /student/ 下所有请求 spring.cloud.gateway.routes[0].predicates[0]=Path=/student/**
- 入口文件注解
@SpringBootApplication
修改为@SpringCloudApplication
- pom.xml 引入
- api 子模块,本模块就是一个正常的 web 项目。
- pom.xml 指定 parent。
- 暴露接口
/student/findById
,接收一个 id 参数,返回 Student 对象。@RestController @RequestMapping("/student") public class StudentController { @Resource StudentService studentService; @GetMapping("findById") public Student findById(@RequestParam(name = "id") Long id) { return studentService.findById(id); } }
- common 子模块,本模块是公共模块,本项目示例中存放数据库实体。
- pom.xml 指定 parent。
POM 代码
完整代码在文章顶部
- 根目录 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaqiuchu</groupId>
<artifactId>demo</artifactId>
<version>${xiaqiuchu.demo.version}</version>
<name>demo</name>
<description>网关项目测试</description>
<packaging>pom</packaging>
<modules>
<module>gateway</module>
<module>api</module>
<module>common</module>
</modules>
<properties>
<xiaqiuchu.demo.version>0.0.1</xiaqiuchu.demo.version>
<!-- https://blog.csdn.net/DCTANT/article/details/108125229 springboot与cloud版本关系 -->
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
</properties>
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.xiaqiuchu</groupId>
<artifactId>api</artifactId>
<version>${xiaqiuchu.demo.version}</version>
</dependency>
<dependency>
<groupId>com.xiaqiuchu</groupId>
<artifactId>gateway</artifactId>
<version>${xiaqiuchu.demo.version}</version>
</dependency>
<dependency>
<groupId>com.xiaqiuchu</groupId>
<artifactId>common</artifactId>
<version>${xiaqiuchu.demo.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
- gateway 子模块 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaqiuchu</groupId>
<name>gateway</name>
<description>网关</description>
<artifactId>gateway</artifactId>
<parent>
<groupId>com.xiaqiuchu</groupId>
<artifactId>demo</artifactId>
<version>${xiaqiuchu.demo.version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<!-- 网关与 web 互斥-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency> -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.xiaqiuchu.gateway.DemoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- api 子模块 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaqiuchu</groupId>
<name>api</name>
<description>接口</description>
<artifactId>api</artifactId>
<parent>
<groupId>com.xiaqiuchu</groupId>
<artifactId>demo</artifactId>
<version>${xiaqiuchu.demo.version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.xiaqiuchu</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.xiaqiuchu.api.DemoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- common 子模块 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaqiuchu</groupId>
<name>common</name>
<description>公共模块</description>
<artifactId>common</artifactId>
<parent>
<groupId>com.xiaqiuchu</groupId>
<artifactId>demo</artifactId>
<version>${xiaqiuchu.demo.version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
标签:spring,boot,Boot2,pom,xiaqiuchu,Spring,org,Gateway,cloud
From: https://www.cnblogs.com/xiaqiuchu/p/17931230.html