SpringCloud中eureka組件如何使用,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
成都創(chuàng)新互聯(lián)公司主營(yíng)儀征網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā)公司,儀征h5成都微信小程序搭建,儀征網(wǎng)站營(yíng)銷推廣歡迎儀征等地區(qū)企業(yè)咨詢
Eureka 是 Netflix 開(kāi)發(fā)的,一個(gè)基于 REST 服務(wù)的,服務(wù)注冊(cè)與發(fā)現(xiàn)的組件
它主要包括兩個(gè)組件:Eureka Server 和 Eureka Client
Eureka Client:一個(gè)Java客戶端,用于簡(jiǎn)化與 Eureka Server 的交互(通常就是微服務(wù)中的客戶端和服務(wù)端)
Eureka Server:提供服務(wù)注冊(cè)和發(fā)現(xiàn)的能力(通常就是微服務(wù)中的注冊(cè)中心)
工作結(jié)構(gòu)圖:
各個(gè)微服務(wù)啟動(dòng)時(shí),會(huì)通過(guò) Eureka Client 向 Eureka Server 注冊(cè)自己,Eureka Server 會(huì)存儲(chǔ)該服務(wù)的信息
也就是說(shuō),每個(gè)微服務(wù)的客戶端和服務(wù)端,都會(huì)注冊(cè)到 Eureka Server,這就衍生出了微服務(wù)相互識(shí)別的話題
同步:每個(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ù)約)信息(類似于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ì)注銷該微服務(wù)節(jié)點(diǎn)
Spring Cloud 已經(jīng)把 Eureka 集成在其子項(xiàng)目 Spring Cloud Netflix 里面
Eureka Server在springCloud集成
1.pom文件配置
<project xmlns="http://maven.apache.org/POM/4.0.0">
<dependencies>
<dependency><!--eureka-server注冊(cè)與發(fā)現(xiàn)依賴-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency><!--eureka-server安全管理依賴,例如用戶名,密碼登錄-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
2 yml配置文件
security: basic: enabled: true user: name: user password: password123 server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://user:password123@localhost:8761/eureka
3.啟動(dòng)類
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
SpringCloud 集成Eureka client
1.pom文件
<?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> <artifactId>microservice-provider-user</artifactId> <packaging>jar</packaging> <name>microservice-provider-user</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.itmuch.cloud</groupId> <artifactId>microservice-spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> </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> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h3database</groupId> <artifactId>h3</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
<dependency><!--客戶端依賴-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency><!--運(yùn)維監(jiān)控瀏覽器查看依賴-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.yml配置文件
server: port: 7900 spring: jpa: generate-ddl: false show-sql: true hibernate: ddl-auto: none datasource: platform: h3 schema: classpath:schema.sql data: classpath:data.sql application: name: microservice-provider-user logging: level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE com.itmuch: DEBUG eureka: client: healthcheck: enabled: true serviceUrl: defaultZone: http://user:password123@localhost:8761/eureka instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} metadata-map: zone: ABC # eureka可以理解的元數(shù)據(jù) lilizhou: BBC # 不會(huì)影響客戶端行為 lease-renewal-interval-in-seconds: 5
3.啟動(dòng)客戶端類
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class MicroserviceSimpleProviderUserApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args); } }
看完上述內(nèi)容,你們掌握SpringCloud中eureka組件如何使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:SpringCloud中eureka組件如何使用
文章起源:http://aaarwkj.com/article16/gpejgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、云服務(wù)器、網(wǎng)站內(nèi)鏈、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)