服務註冊表是一個記錄當前可用服務實例的網絡信息的數據庫,是服務發現機制的核心。服務註冊表提供查詢API和管理API,使用查詢API得到可用的服務實例,使用管理API實現註冊和註銷;html
服務註冊很好理解,就是服務啓動時,將服務的網絡地址註冊到服務註冊表中;java
服務發現組件會經過一些機制定時檢測已註冊的服務,若是發現某服務沒法訪問了(多是某幾個心跳週期後),就將該服務從服務註冊表中移除。mysql
服務發現是基於微服務架構的關鍵原則之一。嘗試配置每一個客戶端或某種形式的約定可能很是困難,能夠很是脆弱。git
Netflix服務發現服務器和客戶端是Eureka。能夠將服務器配置和部署爲高可用性,每一個服務器將註冊服務的狀態複製到其餘服務器。github
當客戶端註冊Eureka時,它提供關於自身的元數據,例如主機和端口,健康指示符URL,主頁等。web
Eureka從屬於服務的每一個實例接收心跳消息。若是心跳失敗超過可配置的時間表,則一般將該實例從註冊表中刪除。正則表達式
要在您的項目中包含Eureka客戶端,請使用組org.springframework.cloud
和工件ID spring-cloud-starter-eureka
的啓動器。算法
有關 使用當前的Spring Cloud發佈列表設置構建系統的詳細信息,請參閱Spring Cloud項目頁面。spring
服務發現方式參看地址:http://www.cnblogs.com/bjlhx/p/8796241.htmlsql
Eureka是Netflix開發的服務發現框架,自己是一個基於REST的服務,主要用於定位運行在AWS域中的中間層服務,以達到負載均衡和中間層服務故障轉移的目的。
Spring Cloud將它集成在其子項目spring-cloud-netflix中,以實現Spring Cloud的服務發現功能。
Eureka 項目2.0將會帶來更好的擴展性,而且使用細粒度的訂閱模型取代了基於拉取的模型
git地址:https://github.com/Netflix/Eureka
region和zone(或者Availability Zone)均是AWS的概念。在非AWS環境下,咱們能夠簡單地將region理解爲Eureka集羣,zone理解成機房。這樣可理解下圖——一個Eureka集羣被部署在了zone1機房和zone2機房中。
參看:https://blog.csdn.net/awschina/article/details/17639191
上圖是來自Eureka官方的架構圖,大體描述了Eureka集羣的工做過程。
詳述:
- Application Service 就至關於本書中的服務提供者(用戶微服務),Application Client就至關於本書中的服務消費者(電影微服務);
- Make Remote Call,能夠簡單理解爲調用RESTful的接口;
- us-east-1c、us-east-1d等是zone,它們都屬於us-east-1這個region;
由圖可知,Eureka包含兩個組件:Eureka Server 和 Eureka Client。
Eureka Server提供服務註冊服務,各個節點啓動後,會在Eureka Server中進行註冊,這樣Eureka Server中的服務註冊表中將會存儲全部可用服務節點的信息,服務節點的信息能夠在界面中直觀的看到。
Eureka Client是一個Java客戶端,用於簡化與Eureka Server的交互,客戶端同時也具有一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。
在應用啓動後,將會向Eureka Server發送心跳(默認週期爲30秒)。若是Eureka Server在多個心跳週期內沒有接收到某個節點的心跳,Eureka Server將會從服務註冊表中把這個服務節點移除(默認90秒)。
Eureka Server之間將會經過複製的方式完成數據的同步。(詳見Eureka高可用章節)
Eureka還提供了客戶端緩存的機制,即便全部的Eureka Server都掛掉,客戶端依然能夠利用緩存中的信息消費其餘服務的API。
綜上,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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Spring Cloud In Action</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.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-actuator</artifactId>-->
<!--</dependency>-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</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>
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
package com.didispace;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Applicationeureka {
public static void main(String[] args) {
new SpringApplicationBuilder(Applicationeureka.class).web(true).run(args);
}
}
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * 在這裏咱們使用@SpringBootApplication指定這是一個 spring boot的應用程序. * @author Angel -- 守護天使 * @version v.0.1 * @date 2016年12月10日 */ @SpringBootApplication @EnableEurekaClient @MapperScan("com.itmuch.cloud.*")//掃描:該包下相應的class,主要是MyBatis的持久化類. public class App{ public static void main(String[] args) { SpringApplication.run(App.class, args); } }
git代碼:https://gitee.com/cyj930307/springcloud_textbook.git
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
server: port: 8761 security: basic: enabled: true user: name: root password: 123456 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://root:123456@localhost:8761/eureka
server: port: 7900 eureka: client: serviceUrl: defaultZone: http://root:123456@localhost:8761/eureka/ instance: prefer-ip-address: true metadata-map: my-metadata: CYJ spring: datasource: url: jdbc:mysql://localhost:3306/cyj?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC username : root password : 123456 driverClassName : com.mysql.jdbc.Driver max-active: 20 max-idle: 8 min-idle: 8 initial-size: 10 mybatis: mapper-locations: classpath*:mybatis/*Mapper.xml type-aliases-package: com.itmuch.cloud.mybatis.entity
1
2
|
#開啓健康檢查(須要spring-boot-starter-actuator依賴)
eureka.client.healthcheck.enabled = true
|