简介
本文只介绍spring cloud项目的搭建,后面文章会依次介绍其他微服务的加入。
关于spring cloud的介绍不在重复,可以参考这个总结 https://zhuanlan.zhihu.com/p/95696180?from_voters_page=true
本文使用的版本 spring cloud: Greenwich.SR3, spring boot:2.1.7.RELEASE
idea项目搭建
1.idea file-new 创建maven工程
2.为工程添加mvn镜像配置,我使用的阿里云mvn仓库,复制以下内容到本地的xxx.xml中,file-settings 找到maven,设置User setting file为xxx.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:\\Users\\用户名\\.m2\\repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
</profiles>
<activeProfiles>
</activeProfiles>
</settings>
3.pom文件中设置自身参数,并添加pom
<groupId>com.zjk.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
4.添加spring boot的parent依赖
<!-- ================================================= -->
<!-- Spring boot -->
<!-- ================================================= -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</parent>
5.添加spring cloud 依赖
<dependencyManagement>
<dependencies>
<!-- ================================================= -->
<!-- Spring Cloud -->
<!-- ================================================= -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.11.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
注意 spring cloud版本要与spring boot对应起来
可以参考
6.右击工程点击maven->reload project 可以下载依赖
依赖下载可能会出现一部分包不在External Libraries中,点击maven的包保存地址(setting-maven-Local repository的目录)发现依赖的jar都在了,这时可以重启下idea,我的就是这样都出现了
7.为项目设置SDK file-Project Structure,如果使用本文相同的版本需要设置SDK为Java1.8,最新的SDK15不支持这些版本,后续的启动会报错。
8.至此已配置完毕,删除project下的src目录,等待后面添加具体的微服务