1. 程式人生 > >SpringCloud踩坑筆記(一)-------專案初建

SpringCloud踩坑筆記(一)-------專案初建

       本系列文章旨在記錄在學習使用 SpringCloud 的過程中,發現並且解決或者未解決的問題,會以實際的專案搭建為主要過程,不會去闡述 SpringCloud 的定義和基礎概念,這些已經有各路大神很充分的文章了。可以參考 《Spring Cloud 中文索引》

        Spring Cloud 本身是依賴於 Spring Boot 的,所以我們可以像建立 Spring Boot 專案一樣,去建立一個專案,然後加入 Spring Cloud 的 maven 依賴即可。

1. 建立專案,引入依賴

<?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">
    <modelVersion>4.0.0</modelVersion>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.18.RELEASE</version>
    </parent>
    
    <groupId>com.walli</groupId>
    <artifactId>spring-cloud</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>  
                    <executable>true</executable>  
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

        需要注意的是,使用了 dependencyManagement 來管理 Spring Cloud 的依賴包的版本,好處是當你需要使用 Spring Cloud 中的某個功能模組時,不需要在 dependency 中額外去宣告版本了,統一使用 spring-cloud-dependencies 宣告的版本即可。另外,spring-cloud-dependencies 的版本申明可以參考 《Spring Cloud版本 version命名說明》

2. 在主函式入口宣告 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3. 編譯、啟動即可

        如果有額外配置,則跟 spring boot 一樣, 在 application.yml 中進行配置即可,Spring Cloud 本質還是個 Spring Boot 專案。

        編譯過程中我遇到的問題是經常 maven 報錯中斷,主要原因是網路問題,無法從 https://mvnrepository.com/ 下載 jar 包,解決辦法是多試幾次,或者自己手動下載 jar 包安裝到本地。