一、前言
springboot是springcloud的基石,从springboot2.x开始,JDK要求的最低版本为1.8。
整个微服务生态已经在mac m1上已经有过完整搭建亲测,可以正常使用。
m1上jdk1.8版本可以选择zulujdk,或者直接选择jdk17.
二、创建项目
1、安装maven插件
springcloud项目默认依托于maven,首先要进行maven配置,不建议使用IDE默认maven。
maven默认访问官方仓库,经常超时,建议改为访问阿里云地址
以IDEA为例,打开属性设置,找到maven选项,安装默认下载的maven插件
打开maven安装目录,进入conf目录,复制一份settings.xml文件,重命名为settings-aliyun.xml。
打开文件,找到mirros标签,将标签内的内容用下面配置进行替换,修改为阿里云仓库
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<!--
<mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
-->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
2、创建父项目
一般情况,springcloud项目为多maven模块依赖项目,常见为父子目录结构
最外层的项目我们用来配置整个springcloud项目的依赖关系,仅作配置用,不涉及编码。
文件——新建——项目,开始创建父级项目
选择Maven,指定jdk版本(至少1.8),点击下一步
填写项目名称和选择项目位置,项目名称就是IDEA显示的名称
maven项目以组ID+工件ID来唯一表示一个项目,组ID一般是我们的包路径,如com.zhufeng
工件ID填写项目名即可,如zhufeng-cloud
项目创建后,目录结构如下图,zhufeng-cloud仅作为pom项目使用,可以把src及子目录删除。
此时zhufeng-cloud的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhufeng</groupId>
<artifactId>zhufeng-cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
选中zhufeng-cloud项目,右键——新建——模块
创建一个新的模块,父项选择刚才创建的zhufeng-cloud,然后填写新的项目名称,如下图:
创建zhufeng-web后,目录层级如下:
此时,在zhufeng-cloud的pom.xml中会多出一个module标签
<?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>
<groupId>com.zhufeng</groupId>
<artifactId>zhufeng-cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>zhufeng-web</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
zhufeng-web可以作为具有相同功能模块的分组,也可以把src目录删掉,然后创建两个新的模块,分别为zhufeng-web-user和zhufeng-web-msg,创建过程相同,父项选择zhufeng-web
这样一个springcloud项目的层级结构就创建好了