SpringCloud 服務的註冊與發現_1

SpringCloud 服務發現組件 Eureka

1. 服務概念 (註冊中心)

服務註冊與發現對於微服務系統來說非常重要。有了服務發現與註冊,你就不需要整天改服務調用的配置文件了,你只需要使用服務的標識符,就可以訪問到服務。

 

2. 服務註冊和發現

Eureka介紹:

Netflix開源了他們另一個架構——Eureka開發的服務發現框架,它是一個RESTful服務,用來定位運行在AWS域(Region)中的中間層服務。

Eureka由兩個組件組成:Eureka服務器Eureka客戶端Eureka服務器用作服務註冊服務器。Eureka客戶端是一個java客戶端,用來簡化與服務器的交互、作爲輪詢負載均衡器,並提供服務的故障切換支持。Netflix在其生產環境中使用的是另外的客戶端,它提供基於流量、資源利用率以及出錯狀態的加權負載均衡

ZooKeeper是一個分佈式的,開放源碼的分佈式應用程序協調服務,是GoogleChubby一個開源的實現,是HadoopHbase的重要組件。它是一個爲分佈式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分佈式同步、組服務等。

 

所有的服務端及訪問服務的客戶端都需要連接到註冊管理器(eureka服務器)。服務在啓動時會自動註冊自己到eureka服務器,每一個服務都有一個名字,這個名字會被註冊到eureka服務器。使用服務的一方只需要使用該名字加上方法名就可以調用到服務。

Spring cloud的服務註冊及發現,不僅僅只有eureka,還支持ZookeeperConsul。默認情況下是eurekaspring 封裝了eureka,使其非常簡單易用,只需要比傳統應用增加一行代碼就可以使用了,這一行代碼就是一個註解。我們按以下步驟實現服務註冊和發現功能。

 

1)首選需要建立eureka服務器

你只需要創建一個空的maven工程,並引入spring boot的相關starter即可,然後創建一個近乎空的執行類,工程如下圖:

 

 

Main類中我們加入如下代碼:

package cn.et;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer
public class Main {
 
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}


可以看到只需要使用@EnableEurekaServer註解就可以讓應用變爲Eureka服務器,這是因爲spring boot封裝了Eureka Server,讓你可以嵌入到應用中直接使用。至於真正的EurekaServerNetflix公司的開源項目,也是可以單獨下載使用的。

 

application.yml配置文件中使用如下配置:


server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


  在application.properties配置文件中使用如下配置:

  server.port=8761

  eureka.instance.hostname=localhost

  eureka.client.registerWithEureka=false

  eureka.client.fetchRegistry=false

  eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/


 做完這些後當然,還要改一下pom文件,增加eureka-serverstarter即可:

 

<dependency>

                            <groupId>org.springframework.cloud</groupId>

                            <artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

 

詳細配置如下: pom.xml

<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>cn.et</groupId>
  <artifactId>SPRINGCLOUD_EUREKASERVER</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</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>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>
</project>


如此eureka服務器就完成了,在命令行啓動就可以了。

 

2)讓服務使用eureka服務器

 

application.name是指定進行服務註冊時該服務的名稱。這個名稱就是後面調用服務時的服務標識符(這是服務發現的功能,我們在後面章節具體介紹)。當然,pom文件也需要增加:

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

 

 

3)、使用@EnableEurekaServer註解

@EnableEurekaServer,啓動一個服務註冊中心提供給其他應用進行對話

 

 

4、啓動springboot,並訪問http://localhost:8761/

 

 

問題:客戶端的負載均衡??

 

3. 發送郵件爲案例 來闡述原理

 

任何一個項目都要有一個註冊中心(高可用),如果其掛了,就不能調用,所以說是很重要的。

 

配置文件

Application.yml:

server:

  port: 8761

 

eureka:

  instance:

    hostname: localhost

  client:

    registerWithEureka: false

    fetchRegistry: false

    serviceUrl:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

注:裏面必須要有空格,否則會報錯。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章