SpringBoot+SpringCloud+SpringCloudAlibaba【01】:父项目搭建


※、开发工具版本
(1)windows版本:win10
(2)IntelliJ IDEA版本:2021.2.3
(3)JDK:jdk-8u301
(4)maven版本:3.8.3
 
※、maven依赖版本
(1)springBoot依赖版本:2.4.6
(2)SpringCloud依赖版本:2020.0.4
(3)SpringCloudAlibaba依赖版本:2.2.6.RELEASE
 
※、新建父工程
 

 ※、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!--指定父依赖springBoot版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.6</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <!-- 当前项目版本 -->
    <groupId>com.noire</groupId>
    <artifactId>noire-guardian-zero</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--pom:标志为父工程-->
    <packaging>pom</packaging>

    <!--子模块-->
    <modules>
        <module>spring-boot</module>
    </modules>

    <!--依赖版本管理-->
    <properties>
        <!--指定maven编译版本-->
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!--SpringCloud版本-->
        <spring.clound.version>2020.0.4</spring.clound.version>
        <!--SpringCloud Alibaba版本-->
        <alibaba.cloud.version>2.2.6.RELEASE</alibaba.cloud.version>
    </properties>

    <!--声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,-->
    <!--并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。-->
    <dependencyManagement>
        <dependencies>
            <!--SpringCloud依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.clound.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SpringCloud Alibaba依赖-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--依赖管理-->
    <dependencies>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--指定JDK编译版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 打包跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
        <!--maven打包需要包括如下文件(把xml文件移到src/main/java目录里也需要配置下面这段代码)-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>
 
※、添加SpringBoot项目测试运行是否成功
(1)新建spring-boot模块,以后spring-boot相关的内容都会放在此模块里

(2)接着在spring-boot模块里再建一个子模块,用于启动测试,方法和上面一样,名字为:spring-boot-start,目录结构如下

(3)添加启动类和测试方法,目录结构如下
(4)Hello控制器代码
package com.boot.start.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Hello 控制器
 *
 * @Author: Noire
 * @Date: 2021/11/5 23:10
 */
@RestController
public class HelloController {

    @GetMapping(value = "/hello")
    public String testHello() {
        return "hello Spring Boot!";
    }

}
(5)启动类代码
package com.boot.start;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 项目启动类
 *
 * @Author: Noire
 * @Date: 2021/11/5 23:10
 */
@Slf4j
@SpringBootApplication
public class BootStartApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(BootStartApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        log.info("\n----------------------------------------------------------\n\t" +
                "Application init is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + "/\n\t" +
                "----------------------------------------------------------");
    }

}
(6)application.yml配置文件代码
spring:
  profiles:
    active: dev
(7)application-dev.yml配置文件代码
#设置端口号
server:
  port: 60000
(8)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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!--指定父依赖版本-->
    <parent>
        <artifactId>spring-boot</artifactId>
        <groupId>com.noire</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <!--当前项目版本:会引用父工程的groupId和version,除非需要自定义,否则不需要重复写-->
    <artifactId>spring-boot-start</artifactId>
    <version>1.0.1</version>

    <!--依赖版本管理-->
    <properties>
        <!--指定maven编译版本-->
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <!--依赖管理-->
    <dependencies>
        <!--SpringBoot的web环境依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!--插件管理-->
    <build>
        <plugins>
            <!--SpringBoot打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(9)启动项目访问:http://ip地址/hello,如:http://192.168.1.4:60000/hello


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM