在上一篇文章中粗略介紹了一下spring cloud 微服務(wù)架構(gòu),本節(jié)來重點介紹一下,如何使用Spring Cloud來搭建微服務(wù)的注冊與發(fā)現(xiàn)模塊
Spring Cloud服務(wù)治理之Spring Cloud Netflix
Spring Cloud Netflix 是Spring Cloud的子項目之一,主要是針對Netflix公司一系列的開源產(chǎn)品的封裝,Spring Cloud封裝之后的Netflix,開發(fā)者可以通過一些簡單的注解快速的在應(yīng)用中配置常用的模塊來實現(xiàn)分布式系統(tǒng)的快速構(gòu)建。它主要提供了如下模塊:服務(wù)發(fā)現(xiàn)與注冊(Eureka),斷路器(Hystrix),智能網(wǎng)關(guān)(Zuul),客戶端負(fù)載均衡(Ribbon)等。
下面的案例將演示如何使用Eureka模塊來快速構(gòu)建Spring Cloud 的服務(wù)發(fā)現(xiàn)與注冊模塊。
服務(wù)注冊中心
- 創(chuàng)建一個maven工程Services并在pom.xml中引入Spring Cloud依賴。
<?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>spring-cloud-demos</groupId>
<artifactId>services</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--spring cloud 依賴,parent引入,其他子模塊不用引用-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 2 .創(chuàng)建核心服務(wù)注冊發(fā)現(xiàn)模塊core-eureka-service,在Services工程下創(chuàng)建maven工程Module,并在pom.xml中引入Eureka依賴
<?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">
<parent>
<artifactId>services</artifactId>
<groupId>spring-cloud-demos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>spring cloud 案例 微服務(wù)注冊中心</name>
<description>spring cloud 案例 微服務(wù)注冊中心</description>
<artifactId>core-eureka-service</artifactId>
<packaging>jar</packaging>
<!--eureka依賴-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<!--maven打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 通過EnableEurekaServer注解來聲明該應(yīng)用為注冊中心服務(wù)可供其他客戶端注冊,代碼如下:
package com.snow.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//聲明為服務(wù)發(fā)現(xiàn)注冊中心
public class CoreEurekaServiceApplication {
public static void main(String[] args){
SpringApplication.run(CoreEurekaServiceApplication.class,args);
}
}
默認(rèn)情況下該服務(wù)注冊中心也會將自己注冊到注冊中心,我們需要修改一下配置文件來修改這些配置。
- 4.在resource目錄下創(chuàng)建application.yml文件,并做如下配置
spring:
application:
name: core-eureka-service #該服務(wù)唯一標(biāo)識名稱
server:
port: 8762 #端口號
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
register-with-eureka: false #不將自身注冊到注冊中心
fetch-registry: false # #表示是否從Eureka Server獲取注冊信息,默認(rèn)為true。因為這是一個單點的Eureka Server,不需要同步其他的Eureka Server節(jié)點的數(shù)據(jù),故而設(shè)為false。
serviceUrl:
#Eureka Server的訪問地址,服務(wù)注冊和client獲取服務(wù)注冊信息均通過該URL,多個服務(wù)注冊地址用,隔開
defaultZone: http://localhost:8762/eureka
- 5 .此時啟用CoreEurekaServiceApplication,工程啟動成功后訪問:http://localhost:8762/ 出現(xiàn)下圖簡單的Eureka管理界面

目前該注冊中心沒有注冊任何一個服務(wù)提供者實例。下面我們開始創(chuàng)建服務(wù)提供者。
服務(wù)提供者
- 在Services工程下創(chuàng)建maven Module 名稱 biz-provider1-service,并在pom.xml中加入Eureka依賴
<?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">
<parent>
<artifactId>services</artifactId>
<groupId>spring-cloud-demos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>spring cloud 案例 服務(wù)提供者1</name>
<packaging>jar</packaging>
<artifactId>biz-provider1-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 2 創(chuàng)建服務(wù)提供者1主類,并通過@EnableDiscoveryClient注解聲明該服務(wù)為EurekaService服務(wù)提供者,該注解能激活Eureka中的DiscoveryClient實現(xiàn)
package com.snow.spring.cloud.sp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 第一個微服務(wù)提供者
*/
@SpringBootApplication
@EnableDiscoveryClient
//@EnableEurekaClient
public class BizProviderServiceApp {
public static void main(String[] args){
SpringApplication.run(BizProviderServiceApp.class,args);
}
}
在這里推薦使用如果使用@EnableDiscoveryClient注解,也可以使用@EnableEurekaClient注解,但是推薦使用@EnableDiscoveryClient代替@EnableEurekaClient注解,因為@EnableDiscoveryClient是一個高度的抽象, 來自于spring-cloud-commons, 由于Spring Cloud選型是中立的因此抽象出該接口, 當(dāng)服務(wù)注冊中心選型改變?yōu)镋ureka,ZK,Consul時,不需要修改原有代碼中的注解。
- 3 在resource目錄下創(chuàng)建bootstrap.yml文件,并配置服務(wù)提供者配置
spring:
application:
name: biz-provider-service1 #標(biāo)識服務(wù)提供者名稱
---
spring:
profiles: p1 # profile別名,jar包運行時動態(tài)指定以實習(xí)動態(tài)加載配置文件內(nèi)容
server:
port: 8801
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
---
spring:
profiles: p2
server:
port: 8802
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
- 4 在biz-provider1-service項目下打開終端執(zhí)行命令打包:
mvn clean package
- 5 cd 到 target目錄下,分別執(zhí)行命令運行服務(wù)提供者,將服務(wù)分別運行在8801端口和8802端口上
java -jar biz-provider1-service-1.0-SNAPSHOT.jar --spring.profiles.active=p1
java -jar biz-provider1-service-1.0-SNAPSHOT.jar --spring.profiles.active=p2
訪問http://localhost:8762/查看EurekaService控制面板,發(fā)現(xiàn)biz-provider1-service已經(jīng)注冊到注冊中心。

至此,服務(wù)注冊與發(fā)現(xiàn)服務(wù)完成。相關(guān)代碼請參考:
碼市:spring-cloud-demos