在最初開始構建微服務系統的時候可能服務並不多,我們可以通過做一些靜態配置來完成服務的調用。比如,有兩個服務A和B,其中服務A需要調用服務B來完成一個業務操作時,為了實現服務B的高可用,不論采用服務端負載均衡還是客戶端負載均衡,都需要手工維護服務B的具體實例清單。但是隨着業務的發展,系統功能越來越復雜,相應的微服務應用也不斷增加,我們的靜態配置會變得越來越難以維護。並且面對不斷發展的業務,我們的集群規模、服務的位置、服務的命名等都有可能發生變化,如果還是通過手工維護的方式,那么極易發生錯誤或是命名沖突問題。同事,對於這類靜態內容的維護也必將消耗大量的人力。
為了解決上述問題,Spring Cloud Eureka 應運而生。Spring Cloud Eureka 是 Spring Cloud Netflix微服務套件中的一部分,它基於Netflix Eureka做了二次封裝,主要負責完成微服務架構中的服務治理功能。它既包含了服務端組件,也包含了客戶端組件,並且服務端與客戶端均采用Java編寫,所以Eureka主要適用於通過Java實現的分布式系統,或是與JVM兼容語言構建的系統。下面我們來構建一些簡單實例,學習如何使用Eureka構建注冊中心以及進行注冊與發現服務。
一、搭建服務注冊中心
首先,創建一個基礎的Spring Boot工程,命名為eureka-server,並在pom.xml中引入必要的依賴內容,代碼如下
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.5.10.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <dependencies> 9 <!-- Eureka服務端開始 --> 10 <dependency>
<groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-starter-eureka-server</artifactId> 12 </dependency> 13 <!-- Eureka服務端結束 --> 14 </dependencies> 16 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-dependencies</artifactId> 22 <version>Edgware.SR2</version> 23 <type>pom</type> 24 <scope>import</scope> 25 </dependency> 26 </dependencies> 27 </dependencyManagement>
通過@EnableEurekaServer注解啟動一個服務注冊中心提供給其他應用進行對話。這一步非常簡單,只需在一個普通的Spring Boot應用中添加這個注解就能開啟此功能,比如下面的例子:
1 @EnableEurekaServer 2 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})① 3 public class EurekaServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(EurekaServerApplication.class, args); 7 } 8 }
在默認設置下,該服務注冊中心也會將自己作為客戶端來嘗試注冊它自己,所以我們需要禁用它的客戶端注冊行為,只需在application.properties中增加如下配置:
1 server.port=1111 2 3 eureka.instance.hostname=localhost 4 eureka.client.register-with-eureka=false #由於該應用為注冊中心,所以設置為false,代表不向注冊中心注冊自己 5 eureka.client.fetch-registry=false #由於注冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置為false 6 eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
在完成了上面的配置后,啟動應用並訪問http://localhost:1111/。可以看到如下圖所示的Eureka信息面板,其中Instances currently registered with Eureka欄是空的,說明該注冊中心還沒有注冊任何服務。

二、注冊服務提供者
在完成了服務注冊中心的搭建之后,接下來我們嘗試將一個既有的Spring Boot應用加入Eureka的服務治理體系中去。
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.5.10.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <properties> 9 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 10 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 11 <java.version>1.8</java.version> 12 </properties> 13 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.boot</groupId> 21 <artifactId>spring-boot-starter-web</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-eureka</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-test</artifactId> 30 <scope>test</scope> 31 </dependency> 32 </dependencies> 33 <dependencyManagement> 34 <dependencies> 35 <dependency> 36 <groupId>org.springframework.cloud</groupId> 37 <artifactId>spring-cloud-dependencies</artifactId> 38 <version>Edgware.SR2</version> 39 <type>pom</type> 40 <scope>import</scope> 41 </dependency> 42 </dependencies> 43 </dependencyManagement>
接着添加/hello接口
1 @RestController 2 public class HelloController { 3 @RequestMapping("/hello") 4 public String hello() { 5 return "hello eureka"; 6 } 7 }
然后,在主類中通過加上@EnableDiscoveryClient 注解,激活Eureka對服務的發現
1 @EnableDiscoveryClient② 2 @SpringBootApplication 3 public class ProvideApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ProvideApplication.class, args); 7 } 8 }
最后,我們需要在application.properties配置文件中,通過spring.application.name屬性來為服務命名,比如命名為hello-service。再通過eureka.client.serviceUrl.defaultZone屬性來制定服務注冊中心的地址,這里我們指定為上述構建的服務注冊中心地址,完整配置如下所示:
1 server.port=8090 2 3 spring.application.name=hello-service 4 5 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
下面我們分別啟動服務注冊中心以及這里改造后的hello-service服務,通過訪問Eureka的信息面板,在Instances currently registered with Eureka可以看到

至此,服務注冊成功!
注釋一:springboot項目啟動時,如果沒有配置數據庫配置,啟動時會拋出如下異常。
Description:
Cannot determine embedded database driver class for database type NONE Action: If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
因為springboot會自動注入數據源,而項目如果沒有配,就會拋出該異常。
如果只是簡單的想建個項目,並不需要數據庫支持,那么可以添加exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class}不去注入數據源。
注釋二:從Spring Cloud Edgware開始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相關依賴,並進行相應配置,即可將微服務注冊到服務發現組件上。
Spring Cloud為什么要這么設計/改進呢?
這是由於在實際項目中,我們可能希望實現“不同環境不同配置”的效果——例如:在開發環境中,不注冊到Eureka Server上,而是服務提供者、服務消費者直連,便於調測;在生產環境中,我們又希望能夠享受服務發現的優勢——服務消費者無需知道服務提供者的絕對地址。為適應該需求,Spring Cloud Commons進行了改進
如不想將服務注冊到Eureka Server,只需設置spring.cloud.service-registry.auto-registration.enabled=false ,或@EnableDiscoveryClient(autoRegister = false) 即可。
