第05課:服務註冊與發現

咱們知道,微服務是一個架構思想,而 Spring Cloud 集成了用以實現微服務架構的方方面面。從本文開始,我將帶領你們逐個擊破 Spring Cloud 的各個模塊。java

本文,咱們先來學習服務的註冊與發現,Spring Cloud Netflix 的 Eureka 組件是服務於發現模塊,下面咱們將學習它。spring

服務註冊與發現模塊分爲服務註冊中心和服務提供者,接下來,我將一一講解。瀏覽器

服務註冊中心架構

首先,建立一個 Maven 主工程,主工程的 pom.xml 添加以下內容:app

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
                <exclusions>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

接着,在主工程基礎上建立兩個 module:一個 module 爲服務註冊中心,一個 module 爲服務提供者(即客戶端)。負載均衡

下面將詳細演示如何建立服務註冊中心。spring-boot

1.右鍵工程 -> New -> Module,以下圖所示:
圖片描述微服務

2.選擇 next,輸入 moudle 名,以下圖所示:學習

圖片描述
圖片描述

3.點擊 next -> finish,以下圖所示:
圖片描述fetch

4.而後在 pom.xml 添加依賴:

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

建立啓動類 Application.java:

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

這裏,咱們注意到除了前面提到的 @SpringBootApplication外,這個類還增長了一個註解:EnableEurekaServer,這個註解的做用就是標註該應用程序是一個註冊中心,只是添加這個註解還不夠,還須要增長配置。

在 resources 下面建立 application.yml 並添加以下內容:

server:
  port: 8761
eureka:
  server:
    enable-self-preservation: false
  instance:
    preferIpAddress: true
    hostname: ${spring.cloud.client.ipAddress}
    instanceId: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

啓動該應用程序,打開瀏覽器並訪問:http://localhost:8761。若是看到以下界面,說明註冊中心已經啓動起來了:
圖片描述

下面說明一下注冊中心各個配置項的含義:

  • eureka.server.enable-self-preservation:是否開啓自我保護,默認爲 true,在開啓自我保護的狀況下,註冊中心在丟失客戶端時,會進入自動保護模式,註冊中心並不會將該服務從註冊中心刪除掉。這裏我設置爲 false,即關閉自我保護。根據個人經驗,若是設置爲 true,在負載均衡條件下,一個服務掛掉後,註冊中心並無刪掉該服務,會致使客戶端請求的時候可能會請求到該服務,致使系統沒法訪問,因此我推薦將這個屬性設置爲 false。
  • eureka.instance.preferIpAddress:是否以 IP 註冊到註冊中心,Eureka 默認是以 hostname
    來註冊的。
  • -client.serviceUrl.defaultZone:註冊中心默認地址。

建議讀者按照以上的配置項寫就好了。

**服務提供者**

咱們有了註冊中心,那麼就能夠建立一個服務提供者(即客戶端)註冊到註冊中心去了。

一樣地,按照註冊中心的建立方式,建立一個 module,而且在 pom.xml 添加以下內容:

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

而後建立 Application.java:

@SpringBootApplication
@EnableEurekaClient
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

這裏用到了一個註解:EnableEurekaClient,標註了此註解,說明該項目是一個服務提供者。

而後建立配置文件 application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: eurekaclient

其中,spring.application.name 爲該服務的名字,eureka.client.serviceUrl.defaultZone 的做用是指定註冊中心的地址。

而後啓動該工程,從新訪問:http://localhost:8761,便可看到以下界面:

圖片描述

咱們能夠看到,剛剛建立的服務提供者 eurekaclient 已經被註冊到註冊中心了。

以上就是本文關於 Eureka 服務註冊與發現的所有內容,僅僅只學習上面的知識還遠遠不夠,咱們把全部的模塊都學習完後,在第14章將帶領你們結合 Spring Cloud 的各個模塊實現一套完整的基於 Spring Cloud 微服務架構的實例。

相關文章
相關標籤/搜索