发布时间:2024-02-16 12:00
项目源码:https://gitee.com/zhangchouchou/spring-cloud-demo
后文链接:https://blog.csdn.net/weixin_42547014/article/details/120334570
创建父工程
建议采用 maven 方式创建,这样可以控制 spring boot 的版本。比如应该使用的 spring boot 的版本是 2.3,而自动创建的时候最低版本只能选择到 2.4。
创建子模块
根据业务需求,创建子模块。项目构建的过程中一定会经历工程重构的过程。创建项目的流程就是先实现功能,再进行工程优化和重构。
以下为创建 spring cloud 项目的一个简单例子
创建完成后需要对IDEA进行一些设置
添加 *idea
和 *iml
字符编码都调整为 UTF-8
我用的是 JDK11
添加
<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.0modelVersion>
<groupId>org.zjh.springclouddemogroupId>
<artifactId>SpringCloudDemoartifactId>
<version>1.0-SNAPSHOTversion>
<modules>
<module>cloud-provider-payment8001module>
modules>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>11maven.compiler.source>
<maven.compiler.target>11maven.compiler.target>
<junit.version>4.13.1junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.18.12lombok.version>
<mysql.version>8.0.25mysql.version>
<druid.version>1.2.6druid.version>
<mybatis.spring.boot.version>2.1.4mybatis.spring.boot.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.3.12.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR12version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
<exclusions>
<exclusion>
<groupId>com.sungroupId>
<artifactId>toolsartifactId>
exclusion>
<exclusion>
<groupId>com.sungroupId>
<artifactId>jconsoleartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.spring.boot.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
<optional>trueoptional>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.3.12.RELEASEversion>
<configuration>
<fork>truefork>
<additionalProperties>trueadditionalProperties>
configuration>
plugin>
plugins>
build>
project>
查看springCloud和springBoot版本对应关系
配置的时候,SpringBoot 和 SpringCloud 的版本需要对应上,查看版本对应的方式如下:
若
org.springframework.boot
报错,如下所示:
解决方式:添加上SpringBoot版本号即可
2.3.12.RELEASE
注:创建子模块命名格式为模块功能+端口号
创建方式选择maven,因为使用spring initializr创建在版本选择部分可能没有对应的spring boot版本
该模块为服务提供模块
<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">
<parent>
<artifactId>SpringCloudDemoartifactId>
<groupId>org.zjh.springclouddemogroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-provider-payment8001artifactId>
<properties>
<maven.compiler.source>11maven.compiler.source>
<maven.compiler.target>11maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
lombok介绍
lombok(小辣椒),自动创建实体类中的 get/set 等方法,以及有参和无参构造函数,使代码更加简洁
常用注解:
- @Data ——> 自动构建get、set方法
- @AllArgsConstructor ——> 全属性构造函数
- @NoArgsConstructor ——> 无参构造函数