SpringCloud微服务搭建

一.准备工作:

  1. IDEA:版本2021.1

    在这里插入图片描述

  2. maven:3.6.3
    在这里插入图片描述

  3. 自己的仓库:配置本地仓库步骤
    在这里插入图片描述

二. 新建SpringCloud项目:

1.idea中:File–>new–>Project

SpringCloud微服务搭建_第1张图片
选中Spring Initializr–按下图填写,Name填写你自己的项目名称。

SpringCloud微服务搭建_第2张图片
点Next:勾选Spring Web–>Finished

SpringCloud微服务搭建_第3张图片
配置本地Maven仓库:

SpringCloud微服务搭建_第4张图片
删除src,mvnw,mvnw.cmd

SpringCloud微服务搭建_第5张图片

SpringCloud微服务搭建_第6张图片
修改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
SpringCloud微服务搭建_第7张图片
Name写自己模块名称

SpringCloud微服务搭建_第8张图片

选中Spring Cloud Discovery—>勾选Eureka Server–>Finish

SpringCloud微服务搭建_第9张图片
修改eureka的pom文件:

SpringCloud微服务搭建_第10张图片
重点:给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>

SpringCloud微服务搭建_第11张图片
更改eureka的application.properties文件名后缀,更改为application.yml

SpringCloud微服务搭建_第12张图片
SpringCloud微服务搭建_第13张图片
在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注解

SpringCloud微服务搭建_第14张图片
3.新建Module—> app

SpringCloud微服务搭建_第15张图片
SpringCloud微服务搭建_第16张图片
修改模块app的pom文件

SpringCloud微服务搭建_第17张图片
完整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

SpringCloud微服务搭建_第18张图片
SpringCloud微服务搭建_第19张图片
并在模块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注解

SpringCloud微服务搭建_第20张图片

@RunWith注解

4.启动模块Eureka的启动类

SpringCloud微服务搭建_第21张图片

浏览器测试Eureka
http://localhost:8901/

SpringCloud微服务搭建_第22张图片
5.最基本的over了

你可能感兴趣的:(spring,cloud,微服务,java)