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 包安装到本地。

猜你喜欢

转载自blog.csdn.net/zxcvqwer19900720/article/details/85321932