1、父工程POM文件中:
<dependencyManagement>
<dependencies>
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
這里需要注意的是,一般的依賴都可以從Maven Repository中導入,但是spring cloud dependencies現在在里面是找不到的了。
所以,在pom文件內,自己指定了遠程倉庫(通過代碼中repositroy配置),遠程倉庫地址:https://repo.spring.io/milestone/,在這個里面你是可以找到最新的springcloud的各個依賴的,這里截取部分:

可以看到,當前最新的版本是Greewich.SR1,今年1月22日更新的。我選擇的是之前一點的版本也是現在spring官網的最新推薦版本Greewich.RELEASE,各取所需吧。
2、導入依賴之后,新建子模塊Eureka-server,pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Netflix Eureka服務端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3、配置文件application.yml:
spring: application: name: eureka-server # 自己設置一個端口號,方便之后訪問 server: port: 6543 # Eureka-server的簡單配置 eureka: instance: hostname: 127.0.0.1 client: register-with-eureka: false fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4、新建啟動類Application並配置開啟服務:
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5、啟動服務查看服務:

Eureka的服務端配置完成。
6、新建一個微服務Test:
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
application.yml
spring:
application:
name: test
server:
port: 8002
eureka:
instance:
hostname: 127.0.0.1
client:
# 配置為true,注冊進服務中心(Eureka server)
register-with-eureka: true
# 配置Eureka server的服務URL
service-url:
defaultZone: http://${eureka.instance.hostname}:7001/eureka/
Application:
@EnableEurekaClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
7、開啟Test微服務,刷新Eureka Server的頁面,就會發現有一個微服務已經注冊進了服務中心了。
個人能力有限,歡迎交流學習,幫助指明錯誤,希望轉載注明出處,謝謝。
