1、SpringCloud Eureka 服務治理

注意:我的筆記很多來源於翟永超老師寫的《Spring Cloud 微服務實戰》,一些資料來源於百度!如果本筆記有錯誤,請私信給我謝謝!

一、Spring Cloud Eureka簡介
Spring Cloud Eureka 是 Spring Cloud Netflix 微服務套件的一部分,基於 Netflix Eureka 做了二次封裝,主要負責完成微服務架構中的服務治理功能,服務治理可以說是微服務架構中最爲核心和基礎的模塊,他主要用來實現各個微服務實例的自動化註冊與發現
  • 服務註冊:在服務治理框架中,通常都會構建一個註冊中心,每個服務單元向註冊中心登記自己提供的服務,將主機與端口號、版本號、通信協議等一些附加信息告知註冊中心,註冊中心按照服務名分類組織服務清單,服務註冊中心還需要以心跳的方式去監控清單中的服務是否可用,若不可用需要從服務清單中剔除,達到排除故障服務的效果。
  • 服務發現:由於在服務治理框架下運行,服務間的調用不再通過指定具體的實例地址來實現,而是通過向服務名發起請求調用實現。
Spring Cloud Eureka 使用 Netflix Eureka 來實現服務註冊與發現,即包括了服務端組件,也包含了客戶端組件,並且服務端和客戶端均採用Java編寫,所以Eureka主要適用與通過Java實現的分佈式系統,或是與JVM兼容語言構建的系統,但是,由於Eureka服務端的服務治理機制提供了完備的RESTful API,所以他也支持將非Java語言構建的微服務納入Eureka的服務治理體系中來。
  • Eureka服務端:我們也稱爲服務註冊中心,他同其他服務註冊中心一樣,支持高可用配置。
  • Eureka客戶端:主要處理服務的註冊和發現,客戶端服務通過註冊和參數配置的方式,嵌入在客戶端應用程序的代碼中,在應用程序運行時,Eureka客戶端向註冊中心註冊自身提供的服務並週期性的發送心跳來更新他的服務租約。
  • 搭建服務註冊中心
    創建一個基礎的 Spring Boot 工程,在 pom.xml 中引入必須的依賴內容 spring-cloud-starter-eureka-server ,通過 @EnableEurekaServer 註解啓動一個服務註冊中心提供給其他應用進行對話

二、搭建服務註冊中心
首先創建一個SpringBoot工程,命名爲eureka-server,並在pom.xml中引入必要的依賴內容,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>

<groupId>com.wl.test</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-server</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR5</spring-cloud.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>eureka-server</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
通過@EnableEurekaServer註解啓動一個服務註冊中心提供給其他應用程序進行對話:



在默認的情況下,該服務中心也會將自己作爲客戶端來嘗試註冊自己,所以我們需要禁用他的客戶端註冊行爲:

spring.application.name 是一個很重要的參數,他標明瞭服務的名稱(不是實例的名稱),消費者可以通過該名稱調用實例!


啓動並訪問 http://localhost:8080/ :

其中Instances currently registered with Eureka是空的,說明該註冊中心還沒有註冊任何服務!

三、註冊一個服務提供者
新建一個maven工程:eureka-service-hello,pom.xml文件裏面填寫:

<?xml version="1.0" encoding="UTF-8"?>
<modelVersion>4.0.0</modelVersion>

<groupId>com.wl.test</groupId>
<artifactId>eureka-service-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-service-hello</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
不同eureka-server工程的,本工程的依賴是:spring-cloud-starter-eureka,eureka-server是:spring-cloud-starter-eureka-server,本工程還增加一個web依賴!

創建HelloController:


配置application.yml文件


在啓動類上註解@EnableDiscoveryClient或者@EnableEurekaClient表示可以發現服務:

注意:@EnableEurekaClient和@EnableDiscoveryClient
spring cloud中discovery service有許多種實現(eureka、consul、zookeeper等等),@EnableDiscoveryClient基於spring-cloud-commons, @EnableEurekaClient基於spring-cloud-netflix。
其實用更簡單的話來說,就是如果選用的註冊中心是eureka,那麼就推薦@EnableEurekaClient,也可以使用@EnableDiscoveryClient,如果是其他的註冊中心,那麼推薦使用@EnableDiscoveryClient

啓動註冊中心,啓動服務提供者!
註冊中心啓動後,啓動服務提供者,註冊中心會打印以下log:


我們訪問 http://localhost:8080/ :

可以看見有一個名字爲EUREKA-SERVICE-HELLO的服務了,狀態是up!

我們鼠標移到鏈接上面:

我們可以通過設置將該連接設置成ip加端口的形式,更加直觀!(注意這個連接名字叫做 instance_id)

顯示的是:



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