爲了防止代碼運行錯誤,建議使用一致的版本:java
刪除其目錄下的src包,只剩下pom.xmlspring
這裏打包選擇pom,最終其子項目重複依賴統一提取到這裏,這就是主項目的主要做用apache
選擇 Eureka Server ,項目建立完成bootstrap
此時pom.xml爲:springboot
<?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.mayi.springcloud</groupId> <artifactId>commonservice-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>commonservice-eureka</name> <description>eureka 註冊中心</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <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>Finchley.M9</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
將resources目錄下的application.properties更名爲bootstrap.yml(建議使用格式化ymal語言,好處本身琢磨吧^_^),ymal 校驗比較嚴格,個人是換行空兩格,保持統一,編輯器也是有提示的微信
server: port: 8761 spring: application: name: service-registry eureka: instance: prefer-ip-address: true client: fetch-registry: false register-with-eureka: false server: wait-time-in-ms-when-sync-empty: 0
解釋:架構
添加@EnableEurekaServer註解便可app
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class CommonserviceEurekaApplication { public static void main(String[] args) { SpringApplication.run(CommonserviceEurekaApplication.class, args); } }
使用SpringBootApp啓動,訪問地址:localhost:8761maven
正常打開以上網頁,說明Eureka註冊中心配置成功。編輯器
spring: application: name: service-eureka-client server: port: 8800
這裏建議使用@EnableEurekaClient註解來聲明客戶端,由於咱們使用的是Eureka註冊中心,若是使用zookeeper或其餘註冊中心使用@EnableDiscoveryClient,而對於Eureka註冊中心來講二者均可
package com.mayi.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class CommonserviceEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(CommonserviceEurekaClientApplication.class, args); } }
如圖,客戶端註冊成功:
接下來,我會依次整新文章,直至整個架構完成,若有興趣的朋友加我微信加入spring cloud社區羣
或關注微信公衆號:java架構師修行