一.准备工作:
-
IDEA:版本2021.1
-
maven:3.6.3
-
自己的仓库:配置本地仓库步骤
二. 新建SpringCloud项目:
1.idea中:File–>new–>Project
选中Spring Initializr–按下图填写,Name填写你自己的项目名称。
点Next:勾选Spring Web–>Finished
配置本地Maven仓库:
删除src,mvnw,mvnw.cmd
修改pom文件:
重点:添加<packaging>pom</packaging>
解释:
<packaging>pom</packaging>
是Maven项目中的一个配置元素,它指定了项目的打包类型。打包类型决定了项目的构建生命周期和输出的文件格式。
如果项目的打包类型为pom,那么表示该项目是一个父项目或者聚合项目,它不会产生其他的文件,只会执行package、install、deploy等命令。
通常,pom类型的项目用来管理子模块的依赖和插件等信息。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.boot.version>2.2.9.RELEASE</spring.boot.version>
</properties>
<packaging>pom</packaging>
完整pom文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ForDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ForDemo</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.boot.version>2.2.9.RELEASE</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.新建Module—Eureka
Name写自己模块名称
选中Spring Cloud Discovery—>勾选Eureka Server–>Finish
修改eureka的pom文件:
重点:给spring-cloud-starter-netflix-eureka-server加上version
<parent>
<groupId>com.example</groupId>
<artifactId>ForDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
完整pom文件:
<?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>
<parent>
<groupId>com.example</groupId>
<artifactId>ForDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改ForDemo的pom文件:
添加下图:
<modules>
<module>eureka</module>
</modules>
更改eureka的application.properties文件名后缀,更改为application.yml
在eureka的application.yml文件中新增配置信息:
server:
port: 8901
spring:
application:
name:discovery-eureka
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defalutZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在eureka启动类EurekaApplication.java中新增@EnableEurekaServer注解
3.新建Module—> app
修改模块app的pom文件
完整pom文件如下:
<?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>
<parent>
<groupId>com.example</groupId>
<artifactId>ForDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
修改模块app的application.properties—》application.yml
并在模块app的application.yml中新增配置信息:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8901/eureka/
server:
port: 8902
spring:
jackson:
serialization:
write-dates-as-timestamps: true
profiles:
active: local #启动默认local的yml配置
application:
name: application-provider #该模块的名字
#文件上传设置大小限制
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 10MB
拓展:
可以在此模块中创建local,dev,sit,uat,prod五个环境的配置文件
对应application-local.yml、application-dev.yml、application-sit.yml、application-uat.yml、application-prod.yml
在模块app的测试启动类中添加@RunWith注解
4.启动模块Eureka的启动类
浏览器测试Eureka
http://localhost:8901/
5.最基本的over了