Spring Cloud學(xué)習(xí)02-服務(wù)注冊(cè)與發(fā)現(xiàn)

Spring Cloud學(xué)習(xí)02-服務(wù)注冊(cè)與發(fā)現(xiàn)

Spring Cloud簡(jiǎn)介

Spring Cloud是一個(gè)基于Spring Boot實(shí)現(xiàn)的云應(yīng)用開(kāi)發(fā)工具,它為基于JVM的云應(yīng)用開(kāi)發(fā)中的配置管理、服務(wù)發(fā)現(xiàn)、斷路器、智能路由、微代理、控制總線(xiàn)、全局鎖、決策競(jìng)選、分布式會(huì)話(huà)和集群狀態(tài)管理等操作提供了一種簡(jiǎn)單的開(kāi)發(fā)方式。

Spring Cloud包含了多個(gè)子項(xiàng)目(針對(duì)分布式系統(tǒng)中涉及的多個(gè)不同開(kāi)源產(chǎn)品),比如:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等項(xiàng)目。

微服務(wù)架構(gòu)

“微服務(wù)架構(gòu)”在這幾年非常的火熱,以至于關(guān)于微服務(wù)架構(gòu)相關(guān)的產(chǎn)品社區(qū)也變得越來(lái)越活躍(比如:netflix、dubbo),Spring Cloud也因Spring社區(qū)的強(qiáng)大知名度和影響力也被廣大架構(gòu)師與開(kāi)發(fā)者備受關(guān)注。

那么什么是“微服務(wù)架構(gòu)”呢?簡(jiǎn)單的說(shuō),微服務(wù)架構(gòu)就是將一個(gè)完整的應(yīng)用從數(shù)據(jù)存儲(chǔ)開(kāi)始垂直拆分成多個(gè)不同的服務(wù),每個(gè)服務(wù)都能獨(dú)立部署、獨(dú)立維護(hù)、獨(dú)立擴(kuò)展,服務(wù)與服務(wù)間通過(guò)諸如RESTful API的方式互相調(diào)用。

對(duì)于“微服務(wù)架構(gòu)”,大家在互聯(lián)網(wǎng)可以搜索到很多相關(guān)的介紹和研究文章來(lái)進(jìn)行學(xué)習(xí)和了解。也可以閱讀始祖Martin Fowler的《Microservices》,本文不做更多的介紹和描述。

服務(wù)注冊(cè)與發(fā)現(xiàn)

在簡(jiǎn)單介紹了Spring Cloud和微服務(wù)架構(gòu)之后,下面回歸本文的主旨內(nèi)容,如何使用Spring Cloud搭建服務(wù)注冊(cè)與發(fā)現(xiàn)模塊。

這里我們會(huì)用到Spring Cloud Netflix,該項(xiàng)目是Spring Cloud的子項(xiàng)目之一,主要內(nèi)容是對(duì)Netflix公司一系列開(kāi)源產(chǎn)品的包裝,它為Spring Boot應(yīng)用提供了自配置的Netflix OSS整合。通過(guò)一些簡(jiǎn)單的注解,開(kāi)發(fā)者就可以快速的在應(yīng)用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)。它主要提供的模塊包括:服務(wù)發(fā)現(xiàn)(Eureka),斷路器(Hystrix),智能路有(Zuul),客戶(hù)端負(fù)載均衡(Ribbon)等。

所以,我們這里的核心內(nèi)容就是服務(wù)發(fā)現(xiàn)模塊:Eureka。

Eureka簡(jiǎn)介

EurekaNetflix開(kāi)發(fā)的,一個(gè)基于REST服務(wù)的,服務(wù)注冊(cè)與發(fā)現(xiàn)的組件

它主要包括兩個(gè)組件:Eureka Server和Eureka Client

Eureka Client:一個(gè)Java客戶(hù)端,用于簡(jiǎn)化與Eureka Server的交互(通常就是微服務(wù)中的客戶(hù)端和服務(wù)端)

Eureka Server:提供服務(wù)注冊(cè)和發(fā)現(xiàn)的能力(通常就是微服務(wù)中的注冊(cè)中心)

各個(gè)微服務(wù)啟動(dòng)時(shí),會(huì)通過(guò)Eureka Client向Eureka Server注冊(cè)自己,Eureka Server會(huì)存儲(chǔ)該服務(wù)的信息

也就是說(shuō),每個(gè)微服務(wù)的客戶(hù)端和服務(wù)端,都會(huì)注冊(cè)到Eureka Server,這就衍生出了微服務(wù)相互識(shí)別的話(huà)題

同步:每個(gè)Eureka Server同時(shí)也是Eureka Client(邏輯上的)

多個(gè)Eureka Server之間通過(guò)復(fù)制的方式完成服務(wù)注冊(cè)表的同步,形成Eureka的高可用

識(shí)別:Eureka Client會(huì)緩存Eureka Server中的信息

即使所有Eureka Server節(jié)點(diǎn)都宕掉,服務(wù)消費(fèi)者仍可使用緩存中的信息找到服務(wù)提供者(筆者已親測(cè))

續(xù)約:微服務(wù)會(huì)周期性(默認(rèn)30s)地向Eureka Server發(fā)送心跳以Renew(續(xù)約)自己的信息(類(lèi)似于heartbeat)

續(xù)期:Eureka Server會(huì)定期(默認(rèn)60s)執(zhí)行一次失效服務(wù)檢測(cè)功能

它會(huì)檢查超過(guò)一定時(shí)間(默認(rèn)90s)沒(méi)有Renew的微服務(wù),發(fā)現(xiàn)則會(huì)注銷(xiāo)該微服務(wù)節(jié)點(diǎn)

Spring Cloud已經(jīng)把Eureka集成在其子項(xiàng)目Spring Cloud Netflix里面

關(guān)于Eureka配置的最佳實(shí)踐,可參考:https://github.com/spring-cloud/spring-cloud-netflix/issues/203

更多介紹,可參考:http://cloud.spring.io/spring-cloud-static/Camden.SR4/#spring-cloud-eureka-server

下面我們動(dòng)手來(lái)做一些嘗試。

創(chuàng)建服務(wù)注冊(cè)中心

創(chuàng)建一個(gè)基礎(chǔ)的Spring Boot工程,工程目錄如下:


并在pom.xml中引入需要的依賴(lài)內(nèi)容:

```

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.3.RELEASE</version>

<relativePath/></parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>

<dependencyManagement>

<dependencies>

<dependency>

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

<artifactId>spring-cloud-dependencies</artifactId>

<version>Dalston.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

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

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

</dependency>

org.springframework.cloud

spring-cloud-starter-eureka

--><dependency>

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

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

</dependency>

</dependencies>

```

通過(guò)@EnableEurekaServer注解啟動(dòng)一個(gè)服務(wù)注冊(cè)中心提供給其他應(yīng)用進(jìn)行對(duì)話(huà)。這一步非常的簡(jiǎn)單,只需要在一個(gè)普通的Spring Boot應(yīng)用中添加這個(gè)注解就能開(kāi)啟此功能,比如下面的例子:

@SpringBootApplication

@EnableEurekaServer

public classApplicaiton {

public static voidmain(String[] args) {

//SpringApplication.run(Applicaiton.class,args);

//new SpringApplicationBuilder(Applicaiton.class).web(true).run(args);

newSpringApplicationBuilder(Applicaiton.class).run(args);

}

}

在默認(rèn)設(shè)置下,該服務(wù)注冊(cè)中心也會(huì)將自己作為客戶(hù)端來(lái)嘗試注冊(cè)它自己,所以我們需要禁用它的客戶(hù)端注冊(cè)行為,只需要在application.properties中問(wèn)增加如下配置:

server.port=1111

eureka.client.register-with-eureka=false#設(shè)置是否從注冊(cè)中心獲取注冊(cè)信息(缺省true),因?yàn)檫@是一個(gè)單點(diǎn)的EurekaServer

#不需要同步其他EurekaServer節(jié)點(diǎn)的數(shù)據(jù),故設(shè)為falseeureka.client.fetch-registry=false#設(shè)置是否將自己作為客戶(hù)端注冊(cè)到注冊(cè)中心(缺省true#這里為不需要(查看@EnableEurekaServer注解的源碼,會(huì)發(fā)現(xiàn)它間接用到了@EnableDiscoveryClient#在未設(shè)置defaultZone的情況下,注冊(cè)中心在本例中的默認(rèn)地址就是http://127.0.0.1:1100/eureka/

#但奇怪的是,啟動(dòng)注冊(cè)中心時(shí),控制臺(tái)還是會(huì)打印這個(gè)地址的節(jié)點(diǎn):http://localhost:8761/eureka/

#而實(shí)際服務(wù)端注冊(cè)時(shí),要使用1100端口的才能注冊(cè)成功,8761端口的會(huì)注冊(cè)失敗并報(bào)告異常--說(shuō)法不正確

#打印信息如下:2017-05-24 11:16:49.148INFO 14800 --- [main] c.n.eureka.DefaultEurekaServerContext: Initializing ...

2017-05-24 11:16:49.156INFO 14800 --- [main] c.n.eureka.cluster.PeerEurekaNodes: Adding new peer nodes [http://localhost:1111/eureka/]eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/#實(shí)際測(cè)試:若修改尾部的eureka為其他的,比如/myeureka,注冊(cè)中心啟動(dòng)沒(méi)有問(wèn)題,但服務(wù)端在注冊(cè)時(shí)會(huì)失敗#報(bào)告異常:com.netflix.discovery.shared.transport.TransportException:cannot execute request on any known servereureka.server.enable-self-preservation=falseeureka.server.eviction.interval-timer-in-ms=4000

為了與后續(xù)要進(jìn)行注冊(cè)的服務(wù)區(qū)分,這里將服務(wù)注冊(cè)中心的端口通過(guò)server.port屬性設(shè)置為1111。

啟動(dòng)工程后,訪(fǎng)問(wèn):http://localhost:1111/

可以看到下面的頁(yè)面,其中還沒(méi)有發(fā)現(xiàn)任何服務(wù)


創(chuàng)建服務(wù)提供方

下面我們創(chuàng)建提供服務(wù)的客戶(hù)端,并向服務(wù)注冊(cè)中心注冊(cè)自己。

假設(shè)我們有一個(gè)提供計(jì)算功能的微服務(wù)模塊,我們實(shí)現(xiàn)一個(gè)RESTful API,通過(guò)傳入兩個(gè)參數(shù)a和b,最后返回a + b的結(jié)果。

首先,創(chuàng)建一個(gè)基本的Spring Boot應(yīng)用,在pom.xml中,加入如下配置:

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.3.RELEASE</version>

<relativePath/></parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>

<dependencyManagement>

<dependencies>

<dependency>

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

<artifactId>spring-cloud-dependencies</artifactId>

<version>Dalston.RELEASE</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

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

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

</dependency>

<dependency>

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

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

</dependency>

org.springframework.cloud

spring-cloud-starter-eureka-server

-->

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

其次,實(shí)現(xiàn)/add請(qǐng)求處理接口,通過(guò)DiscoveryClient對(duì)象,在日志中打印出服務(wù)實(shí)例的相關(guān)內(nèi)容。

@RestController

public classComputerController {

private finalLoggerlogger= Logger.getLogger(getClass());

@Autowired

privateDiscoveryClientdiscoveryclient;

@Autowired

privateEurekaClienteurekaClient;

@RequestMapping(value="/add",method= RequestMethod.GET)

publicInteger add(@RequestParamInteger a,@RequestParamInteger b) {

//ServiceInstance instance = client.getLocalServiceInstance();

InstanceInfo instance =eurekaClient.getNextServerFromEureka("compute-service",false);

Integer r = a + b;

logger.info("/add, host:"+instance.getHostName() +",serviceId:"+ instance.getInstanceId() +",result:"+ r);

returnr;

}

}

最后在主類(lèi)中通過(guò)加上@EnableEurekaClient注解,該注解能激活Eureka中的DiscoveryClient實(shí)現(xiàn),才能實(shí)現(xiàn)Controller中對(duì)服務(wù)信息的輸出。

@SpringBootApplication

@EnableEurekaClient

public classComputeServiceApplication {

public static voidmain(String[] args) {

//new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);

newSpringApplicationBuilder(ComputeServiceApplication.class).run(args);

}

}

我們?cè)谕瓿闪朔?wù)內(nèi)容的實(shí)現(xiàn)之后,再繼續(xù)對(duì)application.properties做一些配置工作,具體如下:

server.port=2222

spring.application.name=compute-serviceeureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

eureka.instance.lease-renewal-interval-in-seconds=10eureka.instance.lease-expirtion-duration-in-seconds=30eureka.client.healthcheck.enabled=true

通過(guò)spring.application.name屬性,我們可以指定微服務(wù)的名稱(chēng)后續(xù)在調(diào)用的時(shí)候只需要使用該名稱(chēng)就可以進(jìn)行服務(wù)的訪(fǎng)問(wèn)。

eureka.client.serviceUrl.defaultZone屬性對(duì)應(yīng)服務(wù)注冊(cè)中心的配置內(nèi)容,指定服務(wù)注冊(cè)中心的位置。

為了在本機(jī)上測(cè)試區(qū)分服務(wù)提供方和服務(wù)注冊(cè)中心,使用server.port屬性設(shè)置不同的端口。

啟動(dòng)該工程后,再次訪(fǎng)問(wèn):http://localhost:1111/

可以看到,我們定義的服務(wù)被注冊(cè)了。


驗(yàn)證

由于發(fā)布的微服務(wù)所暴露出去的都是HTTP的接口,所以驗(yàn)證的話(huà),可以在瀏覽器訪(fǎng)問(wèn)下面的地址:

http:127.0.0.1:2222/add?a=3&b=13


目前為止,我們完成了Spring Cloud

Netflix Eureka搭建注冊(cè)中心的基本示例,不過(guò)也只是嘗嘗鮮

因?yàn)樗€存在著很多問(wèn)題,比如

什么是自我保護(hù)模式

服務(wù)提供方關(guān)閉之后,在注冊(cè)中心看到的狀態(tài)還是UP

注冊(cè)中心的服務(wù)提供方顯示的名字,是不是可以自定義

等等吧,這些問(wèn)題,請(qǐng)參見(jiàn)Eureka進(jìn)階篇

參考:http://blog.didispace.com/springcloud1/

http://jadyer.cn/2017/01/16/springcloud-eureka/

https://springcloud.cc/spring-cloud-dalston.html#_spring_cloud_netflix

eureka-server的配置文件:

server.port=1111

#設(shè)置是否從注冊(cè)中心獲取注冊(cè)信息(缺省true),因?yàn)檫@是一個(gè)單點(diǎn)的EurekaServer

#不需要同步其他EurekaServer節(jié)點(diǎn)的數(shù)據(jù),故設(shè)為false

eureka.client.register-with-eureka=false

#設(shè)置是否將自己作為客戶(hù)端注冊(cè)到注冊(cè)中心(缺省true)

#這里為不需要(查看@EnableEurekaServer注解的源碼,會(huì)發(fā)現(xiàn)它間接用到了@EnableDiscoveryClient)

#在未設(shè)置defaultZone的情況下,注冊(cè)中心在本例中的默認(rèn)地址就是http://127.0.0.1:1100/eureka/

#但奇怪的是,啟動(dòng)注冊(cè)中心時(shí),控制臺(tái)還是會(huì)打印這個(gè)地址的節(jié)點(diǎn):http://localhost:8761/eureka/

#而實(shí)際服務(wù)端注冊(cè)時(shí),要使用1100端口的才能注冊(cè)成功,8761端口的會(huì)注冊(cè)失敗并報(bào)告異常--說(shuō)法不正確

eureka.client.fetch-registry=false

#實(shí)際測(cè)試:若修改尾部的eureka為其他的,比如/myeureka,注冊(cè)中心啟動(dòng)沒(méi)有問(wèn)題,但服務(wù)端在注冊(cè)時(shí)會(huì)失敗

#報(bào)告異常:com.netflix.discovery.shared.transport.TransportException:cannot execute request on any known server

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

#關(guān)閉自我保護(hù)模式(缺省為打開(kāi))

eureka.server.enable-self-preservation=false

#續(xù)期時(shí)間,即掃描失敗服務(wù)的間隔時(shí)間(缺省為:60*1000ms)

eureka.server.eviction.interval-timer-in-ms=4000


<parent>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-parentartifactId>

<version>1.5.3.RELEASEversion>

<relativePath/>parent>

<properties>

<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>

<java.version>1.8java.version>

properties>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloudgroupId>

<artifactId>spring-cloud-dependenciesartifactId>

<version>Dalston.RELEASEversion>

<type>pomtype>

<scope>importscope>

dependency>

dependencies>

dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starterartifactId>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-testartifactId>

<scope>testscope>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-actuatorartifactId>

dependency>

<dependency>

<groupId>org.springframework.cloudgroupId>

<artifactId>spring-cloud-starter-configartifactId>

dependency>

org.springframework.cloud

spring-cloud-starter-eureka

--><dependency>

<groupId>org.springframework.cloudgroupId>

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

dependency>

dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-maven-pluginartifactId>

plugin>

plugins>

build>


eureka-client配置文件:

server.port=2222

#指定發(fā)布的微服務(wù)名(以后調(diào)用時(shí),只需該名稱(chēng)即可訪(fǎng)問(wèn)該服務(wù))

spring.application.name=compute-service

#指定服務(wù)注冊(cè)中心的地址

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

#修改顯示的微服務(wù)名為IP:端口

eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}

#eureka.instance-id:${spring.application.name}

#設(shè)置微服務(wù)調(diào)用地址為IP優(yōu)先(缺省為false)

eureka.instance.prefer-ip-address=true

#eureka.instance.ip-address=192.168.6.16

#心跳時(shí)間,即服務(wù)續(xù)約間隔時(shí)間(缺省為30s)

eureka.instance.lease-renewal-interval-in-seconds=10

#發(fā)呆時(shí)間,即服務(wù)續(xù)約到期時(shí)間(缺省為90s)

eureka.instance.lease-expirtion-duration-in-seconds=30

#開(kāi)啟健康檢查(依賴(lài)spring-boot-starter-actuator)

eureka.client.healthcheck.enabled=true


pom.xml文件:

<parent>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-parentartifactId>

<version>1.5.3.RELEASEversion>

<relativePath/>parent>

<properties>

<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>

<java.version>1.8java.version>

properties>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloudgroupId>

<artifactId>spring-cloud-dependenciesartifactId>

<version>Dalston.RELEASEversion>

<type>pomtype>

<scope>importscope>

dependency>

dependencies>

dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starterartifactId>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-testartifactId>

<scope>testscope>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-actuatorartifactId>

dependency>

<dependency>

<groupId>org.springframework.cloudgroupId>

<artifactId>spring-cloud-starter-configartifactId>

dependency>

<dependency>

<groupId>org.springframework.cloudgroupId>

<artifactId>spring-cloud-starter-eurekaartifactId>

dependency>

org.springframework.cloud

spring-cloud-starter-eureka-server

-->

dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-maven-pluginartifactId>

plugin>

plugins>

build>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容