1、簡介java
最近在看Spring Cloud微服務,接下來的時間和你們一塊兒分享我所看到的,公司如今用的是dubbo ,以後有時間也去了解了解dubbo的源碼。與dubbo相比較,Spring Cloud 在微服務方面有不少全面的實踐。今天主要和你們簡單介紹一下其中的一個組件Eureka註冊中心。Eureka同其餘服務註冊中心同樣,支持高可用配置。若是Eureka以集羣模式不熟,當集羣中有分片出現故障時,那麼Eureka就轉入自我保護模式。它容許在分片故障期間繼續提供服務的發現和註冊,當故障分片恢復運行時,集羣的其餘分片會把它們的狀態再次同步回來。git
2、實踐github
首先咱們建立一個Spring Boot的maven工程,名爲micro-service-integration。下面有一個子模塊名爲registration-center-web,該子模塊就是咱們今天介紹的註冊中心。其工程目錄以下:web
在父工程中pom.xml文件的內容以下:spring
<?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>spring.cloud</groupId> <artifactId>micro-service-integration</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>registration-center-web</module> </modules> <name>micro-service-integration</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.2.RELEASE</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>
在該父模塊引入 spring-boot-starter-parent做爲其父模塊,這樣能夠簡單的默認的使用Spring Boot 配置。而且引入Spring Cloud 的版本爲Dalston.RELEASE。apache
而在子模塊 registration-center-web 的pom.xml 依賴該Spring Cloud的版本。之後的其餘服務也依賴該版本的Spring Cloud。下面是該pom.xml的內容app
<?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"> <parent> <artifactId>micro-service-integration</artifactId> <groupId>spring.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>registration-center-web</artifactId> <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> <profiles> <profile> <id>register-first</id> <properties> <final.project.name>registration-center-first</final.project.name> <server.port>8881</server.port> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>register-second</id> <properties> <final.project.name>registration-center-second</final.project.name> <server.port>8882</server.port> </properties> </profile> <profile> <id>register-third</id> <properties> <final.project.name>registration-center-third</final.project.name> <server.port>8883</server.port> </properties> </profile> </profiles> <build> <finalName>${final.project.name}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
在該pom.xml 中,能夠根據profile中來制定不一樣的服務啓動端口。maven
接下來咱們來看一下java代碼函數
package com.qee.registrationcenter.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class RegistrationCenterApplication { public static void main(String[] args) { SpringApplication.run(RegistrationCenterApplication.class, args); } }
很是的簡單,主要經過main函數啓動該工程,2個註解 @SpringBootApplication 和 @EnableEurekaServer。而後咱們來看一下咱們application.properties文件。spring-boot
server.port=@server.port@ spring.application.name=registration-center-web server.register.port1=8881 server.register.port2=8882 server.register.port3=8883 eureka.instance.hostname=register.center.com #因爲該應用爲註冊中心,因此設置爲false,表明不向註冊中心註冊本身 eureka.client.register-with-eureka=true #因爲註冊中心的職責就是維護服務實例,因此他不須要去檢索服務 eureka.client.fetch-registry=true eureka.server.enable-self-preservation=false #默認的註冊域 #eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.register.port1}/eureka/,http://${eureka.instance.hostname}:${server.register.port2}/eureka/ #控制檯彩色輸出 spring.output.ansi.enabled=ALWAYS
這裏咱們是搭建一個高可用的註冊中心,其中有三個註冊中心分別爲K1,K2,K3,其中K1的端口爲8881 ,K2的端口爲8882,K3的端口爲8883。接着而後K1向K2,K3註冊中心註冊本身,而K2向K1,K3註冊中心註冊本身,而K3向K1,K2註冊中心註冊本身。因爲在啓動K1註冊中心時,K2和K3註冊中心還沒開啓,因此K1會報異常,可是服務仍是會正常啓動,同理K2也會因爲K3沒有啓動,因此也會報異常,可是啓動K3的時候,K1和K2註冊中心已經正常啓動,因此K3不會報異常。最後在各自的註冊中心能夠看到其餘2個註冊中心最爲服務註冊上去。各自的訪問地址爲 http://register.center.com:888一、http://register.center.com:888二、http://register.center.com:8883。
接着咱們啓動三個註冊中心,咱們看下以下結果:
3、分析
@EnableEurekaServer 該註解啓動一個服務註冊中心提供給其餘應用進行對話。
eureka.client.register-with-eureka : 該參數表明該Eureka應用(包括註冊中心)是否註冊到註冊中心中,若是隻是一個單一註冊中心,那麼把該參數設置爲false,表明不向註冊中心註冊本身。若是是搭建高可用的集羣註冊中心,則該屬性設置爲true。該屬性值默認true。
eureka.client.fetch-registry : 該參數表明是否須要檢索服務,若是是單一註冊中心則不須要去檢索服務,則設置爲false。該參數默認值爲true。
eureka.client.serviceUrl.defaultZone : 該參數指定默認註冊中心的註冊地址,其餘的微服務應用就是經過該屬性值來註冊服務。
eureka.server.enable-self-preservation :設置爲false 表明關閉註冊中心的保護機制,默認爲true。其餘的詳細屬性和配置能夠查看官方文檔。或者留言你們一塊兒討論。該工程的gitHub地址爲:https://github.com/vOoT/micro-service-integration.git