Spring Cloud学习笔记(二):创建项目(主工程/服务Eureka)

 创建主工程

点击file–>new–>project选择左侧栏Spring Initializr,选择JDK1.8,点击next

根据实际情况修改Group与Artifact,其他默认即可,然后next->next->finish

创建成功之后删除src

修改主工程打包模式为pom,parent配置指定为spring-boot-starter-parent的1.5.6版本

<?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>

   <groupId>com.liufei</groupId>
   <artifactId>spring-cloud-component</artifactId>
   <version>1.0.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <modules>
      <module>spring-cloud-eureka</module><!-- 注册中心 -->
   </modules>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <properties>
      <project.verion>1.0.0-SNAPSHOT</project.verion>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Dalston.SR4</spring-cloud.version>
      <org.apache.hadoop.version>3.0.0</org.apache.hadoop.version>
   </properties>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
               <skipTests>true</skipTests>
               <testFailureIgnore>true</testFailureIgnore>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>
创建子模块Eureka

项目名右键–>new–>module进入创建界面

后续步骤与创建主项目一致,创建后修改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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>com.liufei</groupId>
      <artifactId>spring-cloud-component</artifactId>
      <version>1.0.0-SNAPSHOT</version>
   </parent>

   <groupId>com.liufei</groupId>
   <artifactId>spring-cloud-eureka</artifactId>
   <version>1.0.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <description>REGISTER CENTER</description>


   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <!-- 项目依赖插件  -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <configuration>
               <outputDirectory>${project.build.directory}/eureka/lib</outputDirectory>
               <excludeTransitive>false</excludeTransitive> <!-- 表示是否不包含间接依赖的包 -->
               <stripVersion>false</stripVersion> <!-- 去除版本信息 -->
            </configuration>
            <executions>
               <execution>
                  <id>copy-dependencies</id>
                  <phase>package</phase>
                  <goals>
                     <goal>copy-dependencies</goal>
                  </goals>
                  <configuration>
                     <outputDirectory>${project.build.directory}/eureka/lib</outputDirectory>
                     <excludeTransitive>false</excludeTransitive> <!-- 表示是否不包含间接依赖的包 -->
                     <stripVersion>false</stripVersion> <!-- 去除版本信息 -->
                  </configuration>
               </execution>
            </executions>
         </plugin>
         <!-- 项目资源插件 -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
               <execution>
                  <id>copy-resources</id>
                  <phase>package</phase>
                  <goals>
                     <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                     <encoding>UTF-8</encoding>
                     <!-- 拷贝项目src/main/resources/下,除.bat以外的所有文件到conf/目录下 -->
                     <outputDirectory>${project.build.directory}/eureka/conf</outputDirectory>
                     <resources>
                        <resource>
                           <directory>src/main/resources/</directory>
                           <filtering>true</filtering>
                           <excludes>
                              <exclude>*.bat</exclude>
                              <exclude>*.sh</exclude>
                           </excludes>
                        </resource>
                     </resources>
                  </configuration>
               </execution>
               <execution>
                  <id>copy-command</id>
                  <phase>package</phase>
                  <goals>
                     <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                     <encoding>UTF-8</encoding>
                     <!-- 只拷贝项目src/main/resources/目录下的.bat文件到输出目录下 -->
                     <outputDirectory>${project.build.directory}/eureka/bin</outputDirectory>
                     <resources>
                        <resource>
                           <directory>src/main/resources</directory>
                           <filtering>true</filtering>
                           <includes>
                              <include>*.bat</include>
                              <include>*.sh</include>
                           </includes>
                        </resource>
                     </resources>
                  </configuration>
               </execution>
            </executions>
         </plugin>
         <!--打包插件-->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
               <outputDirectory>${project.build.directory}/eureka</outputDirectory>
               <archive>
                  <manifest>
                     <addClasspath>true</addClasspath>
                     <mainClass>com.hy.kedola.eureka.Application</mainClass>
                     <classpathPrefix>lib/</classpathPrefix>
                  </manifest>
                  <manifestEntries>
                     <!-- 在Class-Path下添加配置文件的路径 -->
                     <Class-Path>conf/</Class-Path>
                  </manifestEntries>
               </archive>
               <includes>
                  <!-- 打jar包时,只打包class文件 -->
                  <include>**/*.class</include>
               </includes>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

添加 application.yml配置文件,删除application.propertices,添加如下配置(为什么用YAML?个人感觉看起来比较直观)

最后一步,在启动类中添加@EnableEurekaServer,然后执行


启动后效果如下: