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