java B2B2C springmvc mybatis电子商务平台源码-服务的注册与发现(Eureka)
1、介紹
對于微服務(wù)的治理而言,其核心就是服務(wù)的注冊和發(fā)現(xiàn)。在SpringCloud 中提供了多種服務(wù)注冊與發(fā)現(xiàn)組件:Eureka,Consul,Zookeeper。官方推薦使用Eureka。
需要JAVA Spring Cloud大型企業(yè)分布式微服務(wù)云構(gòu)建的B2B2C電子商務(wù)平臺源碼 一零三八七七四六二六
說明:Eureka是Netflix開源的一款提供服務(wù)注冊和發(fā)現(xiàn)的產(chǎn)品,它提供了完整的Service Registry和Service Discovery實(shí)現(xiàn)。也是springcloud體系中最重要最核心的組件之一。管理的服務(wù)包含了Spring Cloud的其他服務(wù)組件如:熔斷、負(fù)載、降級等。
2、理解
以往服務(wù)間資源的獲取都是通過相互調(diào)用,比如A獲取B相關(guān)資源,A調(diào)用B提供的API獲取相關(guān)資源 。加入Eureka之后,則B提供服務(wù)資源需要在Eureka服務(wù)中心注冊一遍,A獲取服務(wù)資源也需要在Eureka服務(wù)中心注冊一遍,從而獲取B服務(wù)的資源。監(jiān)控Eureka服務(wù)中心可以監(jiān)控AB服務(wù)調(diào)用的使用情況。
3、核心內(nèi)容
上文說過,對于服務(wù)治理而言,其核心部分就是服務(wù)的發(fā)現(xiàn)與注冊,而Spring Cloud推薦使用的是Eureka。因?yàn)樵诜植际较到y(tǒng)中,有一個(gè)CAP定理,即 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分區(qū)容錯(cuò)性),三者不可兼得。Zookeeper是Hadoop的子項(xiàng)目,它的作用也多作為服務(wù)的發(fā)現(xiàn)與注冊。 Zookeeper在CAP定理中滿足的CP,也就是一致性和分區(qū)容錯(cuò)性,但是它不能保證服務(wù)的可用性。比如,服務(wù)消費(fèi)者通過注冊列表獲取數(shù)據(jù)時(shí),倘若,Zookeeper正在選主導(dǎo)致服務(wù)不可用,亦或者大多數(shù)服務(wù)宕機(jī)。在一般分布式系統(tǒng)的數(shù)據(jù)存儲場景,數(shù)據(jù)一致性應(yīng)該是首先被保證的。然而在服務(wù)發(fā)現(xiàn)的場景中,服務(wù)消費(fèi)者能夠消費(fèi)才是首先保證的,即服務(wù)的可用性才是首先保證。
3.1、Eureka組件
Eureka由多個(gè)instance(服務(wù)實(shí)例)組成,這些服務(wù)實(shí)例可以分為兩種:Eureka Server和Eureka Client。為了便于理解,我們將Eureka client再分為Service Provider(服務(wù)提供者)和Service Consumer(服務(wù)消費(fèi)者)。
Eureka Server:服務(wù)的注冊中心,負(fù)責(zé)維護(hù)注冊的服務(wù)列表。
Service Provider:服務(wù)提供方,作為一個(gè)Eureka Client,向Eureka Server做服務(wù)注冊、續(xù)約和下線等操作,注冊的主要數(shù)據(jù)包括服務(wù)名、機(jī)器ip、端口號、域名等等。
Service Consumer:服務(wù)消費(fèi)方,作為一個(gè)Eureka Client,向Eureka Server獲取Service Provider的注冊信息,并通過遠(yuǎn)程調(diào)用與Service Provider進(jìn)行通信。
3.2、Eureka Server
Eureka Server作為一個(gè)獨(dú)立的部署單元,以REST API的形式為服務(wù)實(shí)例提供了注冊、管理和查詢等操作。同時(shí),Eureka Server也為我們提供了可視化的監(jiān)控頁面,可以直觀地看到各個(gè)Eureka Server當(dāng)前的運(yùn)行狀態(tài)和所有已注冊服務(wù)的情況。
Eureka Server節(jié)點(diǎn)啟動(dòng)后,會首先嘗試從鄰近節(jié)點(diǎn)獲取所有實(shí)例注冊表信息,完成初始化。Eureka Server通過getEurekaServiceUrls()方法獲取所有的節(jié)點(diǎn),并且會通過心跳續(xù)約的方式定期更新。 默認(rèn)配置下,如果Eureka Server在一定時(shí)間內(nèi)沒有接收到某個(gè)服務(wù)實(shí)例的心跳,Eureka Server將會注銷該實(shí)例(默認(rèn)為90秒,通過eureka.instance.lease-expiration-duration-in-seconds配置)。當(dāng)Eureka Server節(jié)點(diǎn)在短時(shí)間內(nèi)丟失過多的心跳時(shí)(比如發(fā)生了網(wǎng)絡(luò)分區(qū)故障),那么這個(gè)節(jié)點(diǎn)就會進(jìn)入自我保護(hù)模式。
自我保護(hù)模式: 默認(rèn)配置下,如果Eureka Server每分鐘收到心跳續(xù)約的數(shù)量低于一個(gè)閾值(instance的數(shù)量(60/每個(gè)instance的心跳間隔秒數(shù))自我保護(hù)系數(shù)),并且持續(xù)15分鐘,就會觸發(fā)自我保護(hù)。在自我保護(hù)模式中,Eureka Server會保護(hù)服務(wù)注冊表中的信息,不再注銷任何服務(wù)實(shí)例。當(dāng)它收到的心跳數(shù)重新恢復(fù)到閾值以上時(shí),該Eureka Server節(jié)點(diǎn)就會自動(dòng)退出自我保護(hù)模式。它的設(shè)計(jì)理念就是寧可保留錯(cuò)誤的服務(wù)注冊信息,也不盲目注銷任何可能健康的服務(wù)實(shí)例。該模式可以通過eureka.server.enable-self-preservation = false來禁用,同時(shí)eureka.instance.lease-renewal-interval-in-seconds可以用來更改心跳間隔,eureka.server.renewal-percent-threshold可以用來修改自我保護(hù)系數(shù)(默認(rèn)0.85)。
3.3、Eureka Client
服務(wù)注冊 :啟動(dòng)時(shí),會調(diào)用服務(wù)注冊方法,向Eureka Server注冊自己的信息。Eureka Server會維護(hù)一個(gè)已注冊服務(wù)的列表。當(dāng)實(shí)例狀態(tài)發(fā)生變化時(shí)(如自身檢測認(rèn)為Down的時(shí)候),也會向Eureka Server更新自己的服務(wù)狀態(tài),同時(shí)用replicateToPeers()向其它Eureka Server節(jié)點(diǎn)做狀態(tài)同步。
續(xù)約與剔除 :服務(wù)實(shí)例啟動(dòng)后,會周期性地向Eureka Server發(fā)送心跳以續(xù)約自己的信息,避免自己的注冊信息被剔除。續(xù)約的方式與服務(wù)注冊基本一致,首先更新自身狀態(tài),再同步到其它Peer。如果Eureka Server在一段時(shí)間內(nèi)沒有接收到某個(gè)微服務(wù)節(jié)點(diǎn)的心跳,Eureka Server將會注銷該微服務(wù)節(jié)點(diǎn)(自我保護(hù)模式除外)。
服務(wù)消費(fèi) :Service Consumer本質(zhì)上也是一個(gè)Eureka Client。它啟動(dòng)后,會從Eureka Server上獲取所有實(shí)例的注冊信息,包括IP地址、端口等,并緩存到本地。這些信息默認(rèn)每30秒更新一次。前文提到過,如果與Eureka Server通信中斷,Service Consumer仍然可以通過本地緩存與Service Provider通信。
三處緩存:
Eureka Server對注冊列表進(jìn)行緩存,默認(rèn)時(shí)間為30s。
Eureka Client對獲取到的注冊信息進(jìn)行緩存,默認(rèn)時(shí)間為30s。
Ribbon會從上面提到的Eureka Client獲取服務(wù)列表,將負(fù)載均衡后的結(jié)果緩存30s。
4、代碼實(shí)現(xiàn)
我們做三個(gè)角色
Eureka Server :提供服務(wù)注冊和發(fā)現(xiàn);
Service Provider :服務(wù)提供方,將自身服務(wù)注冊到Eureka,從而使服務(wù)消費(fèi)方能夠找到;
Service Consumer: 服務(wù)消費(fèi)方,從Eureka獲取注冊服務(wù)列表,從而能夠消費(fèi)服務(wù)。
5、Eureka Server
maven項(xiàng)目的pom.xml文件
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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> <spring-cloud.version>Dalston.SR2</spring-cloud.version> </properties><dependencies> <!--Eureka服務(wù)端--> <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> <plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin> </plugins> </build> 復(fù)制代碼項(xiàng)目的啟動(dòng)配置文件:application.properties
spring.application.name=eureka-server server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 復(fù)制代碼配置說明:其中serviceUrl代表注冊中心,集群多個(gè),逗號分隔,fetch-registry(默認(rèn)開啟)表示從注冊中中心獲取服務(wù);register-with-eureka(默認(rèn)開啟):是否將自身注冊到服務(wù)中心。
開啟Eureka服務(wù):
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication {public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args);} } 復(fù)制代碼6、Eureka Provider
maven項(xiàng)目的pom.xml文件:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.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.SR2</spring-cloud.version> </properties><dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId> </dependency><!-- spring boot實(shí)現(xiàn)Java Web服務(wù)--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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> 復(fù)制代碼項(xiàng)目啟動(dòng)配置文件application.properties:
#應(yīng)用(服務(wù))名稱 spring.application.name=eureka-provider server.port=8762 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 復(fù)制代碼注解@EnableDiscoveryClient服務(wù)化:
@EnableDiscoveryClient @SpringBootApplication public class EurekaProviderApplication {public static void main(String[] args) { new SpringApplicationBuilder(EurekaProviderApplication.class).web(true).run(args); }} 復(fù)制代碼訪問控制器:
@RestController public class ProviderNode1Controller {@GetMapping({"","/"}) public String index(){ return "Hi,dy_bom! this is provider-node1 of peer!";} 復(fù)制代碼7、Eureka與Spring Cloud Security 結(jié)合做權(quán)限認(rèn)證
在pom.xml中加入Security的依賴
<!--權(quán)限認(rèn)證--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId> </dependency> 復(fù)制代碼properties定義權(quán)限的用戶名密碼
#權(quán)限認(rèn)證(默認(rèn)就是開啟的) security:basic:enabled: trueuser:name: eurekapassword: 123456 復(fù)制代碼8、Eureka集群
properties配置文件
#對等意識,集群Eureka服務(wù)器配置 --- spring:application:name: spring-cloud-eurekaprofiles: peer1 server:port: 8001eureka:instance:hostname: peer1client: serviceUrl:defaultZone: http://peer2:8002/eureka/,http://peer3:8003/eureka/ --- spring:application:name: spring-cloud-eurekaprofiles: peer2 server:port: 8002 eureka:instance:hostname: peer2client: serviceUrl:defaultZone: http://peer1:8001/eureka/,http://peer3:8003/eureka/ --- spring:application:name: spring-cloud-eurekaprofiles: peer3 server:port: 8003 eureka:instance:hostname: peer3client: serviceUrl:defaultZone: http://peer1:8001/eureka/,http://peer2:8001/eureka/ 復(fù)制代碼啟動(dòng)后,我們可以看到三個(gè)節(jié)點(diǎn)是兩兩互相注冊的。各個(gè)節(jié)點(diǎn)沒有主次之分,對等。
java B2B2C springmvc mybatis電子商務(wù)平臺源碼
轉(zhuǎn)載于:https://juejin.im/post/5ca47bc9f265da309c584871
總結(jié)
以上是生活随笔為你收集整理的java B2B2C springmvc mybatis电子商务平台源码-服务的注册与发现(Eureka)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入解析go依赖注入库go.uber.o
- 下一篇: 计算机英语课外知识竞赛,高2012级英语