轉載請表明出處 https://blog.csdn.net/Amor_Leo/article/details/87873898 謝謝
Spring Cloud Eureka
Spring Cloud Eureka 概述
Eureka是Netflix開發的服務發現框架,SpringCloud將它集成在自己的子項目spring-cloud-netflix中,實現SpringCloud的服務發現功能。Eureka包含兩個組件:Eureka Server和Eureka Client。
Eureka Server提供服務註冊服務,各個節點啓動後,會在Eureka Server中進行註冊,這樣EurekaServer中的服務註冊表中會存儲所有可用服務節點的信息,服務節點的信息可以在界面中直觀的看到。
Eureka Client是一個java客戶端,用於簡化與Eureka Server的交互,客戶端同時也就別一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啓動後,將會向Eureka Server發送心跳,默認週期爲30秒,告訴Eureka Server; Eureka Server在啓動的時候會創建一個定時任務,默認每隔一段時間(默認60秒)將當前服務註冊表中超時(默認90秒)沒有續約的服務節點移除。
Eureka Server 在運行期間會統計心跳失敗的比例在15分鐘內是否低於85%,如果出現低於的情況,Eureka就認爲客戶端與註冊中心出現了網絡故障,Eureka Server 會自動進入自我保護機制,將當前的實例註冊信息保護起來,讓這些實例不會過期,此時會出現以下幾種情況:
- Eureka Server不再從註冊列表中移除因爲長時間沒收到心跳而應該過期的服務。
- Eureka Server仍然能夠接受新服務的註冊和查詢請求,但是不會被同步到其它節點上,保證當前節點依然可用。
- 當網絡穩定時,當前Eureka Server新的註冊信息會被同步到其它節點中。
但是在保護期內如果服務剛好這個服務提供者非正常下線,此時服務消費者就會拿到一個無效的服務實例,此時會調用失敗,對於這個問題需要服務消費者端要有一些容錯機制,如重試、斷路器等。也可以eureka.server.enable-self-preservation=false來關閉保護機制,這樣可以確保註冊中心中不可用的實例被及時的剔除,但不推薦。
Eureka Server之間通過複製的方式完成數據的同步,Eureka還提供了客戶端緩存機制,即使所有的Eureka Server都掛掉,客戶端依然可以利用緩存中的信息消費其他服務的API。
Spring Cloud Eureka 搭建
單機
服務端 Eureka Server
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- yml
server:
port: 8761 #註冊中心的地址
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 1 #心跳檢測檢測時間,每間隔1s,向服務端發送一次心跳,表明自己依然”存活“
lease-expiration-duration-in-seconds: 2 #續約時間,告訴服務端,如果我2s之內沒有給你發心跳,就代表我“死”了,將我剔除
server:
enable-self-preservation: false #關閉自我保護機制
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false # 對於註冊中心的優化,表明不向註冊中心註冊自己
fetch-registry: false # 註冊中心的優化,代表該註冊中心服務,不拉取任何的服務列表
management:
endpoints:
web:
exposure:
include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- Application類上
@EnableEurekaServer //開啓eureka server功能
Eureka是有圖形界面的,我們打開瀏覽器,訪問 http://localhost:8761/
- 如果是在虛擬機上用Docker運行
- 修改yml
server: port: 8761 #註冊中心的地址 spring: application: name: eureka-server eureka: instance: hostname: ${HOST} lease-renewal-interval-in-seconds: 1 #心跳檢測檢測時間,每間隔1s,向服務端發送一次心跳,表明自己依然”存活“ lease-expiration-duration-in-seconds: 2 #續約時間,告訴服務端,如果我2s之內沒有給你發心跳,就代表我“死”了,將我剔除 server: enable-self-preservation: false #關閉自我保護機制 client: service-url: defaultZone: ${EUREKA_SERVER_URL} register-with-eureka: false # 對於註冊中心的優化,表明不向註冊中心註冊自己 fetch-registry: false # 註冊中心的優化,代表該註冊中心服務,不拉取任何的服務列表 management: endpoints: web: exposure: include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- 打包jar 編寫dockerfile
FROM java:8-alpine MAINTAINER "LHL <[email protected]>" ADD *.jar app.jar EXPOSE 8761 ENTRYPOINT ["java","-jar","/app.jar"]
- 構建鏡像
docker build ./eureka -t "eureka-server:1.0"
- 創建並運行容器
docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.110:8761/eureka -e HOST=192.168.0.110 -d eureka-server:1.0
客戶端 Eureka Client
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml
eureka:
client:
service‐url:
defaultZone: http://localhost:8761/eureka
instance:
prefer‐ip‐address: true #訪問路徑可以顯示ip地址
instance-id: USER-SERVICE #自定義服務名稱
management:
endpoints:
web:
exposure:
include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- Application類上
@EnableDiscoveryClient
Eureka集羣(本地)與SpringSecurity結合 (springBoot2.1以上版本)
修改host
C:\Windows\System32\drivers\etc
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
服務端 Eureka Server
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
- yml
- application-peer1.yml
server: port: 8761 spring: application: name: eureka-server security: user: name: amor password: amor eureka: instance: hostname: peer1 lease-renewal-interval-in-seconds: 1 #心跳檢測檢測時間,每間隔1s,向服務端發送一次心跳,表明自己依然”存活“ lease-expiration-duration-in-seconds: 2 #續約時間,告訴服務端,如果我2s之內沒有給你發心跳,就代表我“死”了,將我剔除 server: enable-self-preservation: false #關閉自我保護機制 client: service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer2:8762/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer3:8763/eureka/ register-with-eureka: false # 對於註冊中心的優化 fetch-registry: false # 註冊中心的優化,代表該註冊中心服務,不拉取任何的服務列表 management: endpoints: web: exposure: include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- application-peer2.yml
server: port: 8762 spring: application: name: eureka-server security: user: name: amor password: amor eureka: instance: hostname: peer2 lease-renewal-interval-in-seconds: 1 #心跳檢測檢測時間,每間隔1s,向服務端發送一次心跳,表明自己依然”存活“ lease-expiration-duration-in-seconds: 2 #續約時間,告訴服務端,如果我2s之內沒有給你發心跳,就代表我“死”了,將我剔除 server: enable-self-preservation: false #關閉自我保護機制 client: service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer1:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer3:8763/eureka/ register-with-eureka: false # 對於註冊中心的優化 fetch-registry: false # 註冊中心的優化,代表該註冊中心服務,不拉取任何的服務列表 management: endpoints: web: exposure: include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- application-peer3.yml
server: port: 8763 spring: application: name: eureka-server security: user: name: amor password: amor eureka: instance: hostname: peer3 lease-renewal-interval-in-seconds: 1 #心跳檢測檢測時間,每間隔1s,向服務端發送一次心跳,表明自己依然”存活“ lease-expiration-duration-in-seconds: 2 #續約時間,告訴服務端,如果我2s之內沒有給你發心跳,就代表我“死”了,將我剔除 server: enable-self-preservation: false #關閉自我保護機制 client: service-url: defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer1:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer2:8762/eureka/ register-with-eureka: false # 對於註冊中心的優化 fetch-registry: false # 註冊中心的優化,代表該註冊中心服務,不拉取任何的服務列表 management: endpoints: web: exposure: include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- Application類上
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
//springBoot2.1以上版本 (如果不加 註冊不了)
@EnableWebSecurity
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
}
- 啓動
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer3
Eureka是有圖形界面的,我們打開瀏覽器,訪問 http://localhost:8761/
客戶端 Eureka Client
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml
eureka:
client:
serviceUrl:
defaultZone: http://${eurekaUserName}:${eurekaPassWord}@peer1:8761/eureka/,http://${eurekaUserName}:${eurekaPassWord}@peer2:8762/eureka/,http://${eurekaUserName}:${eurekaPassWord}@peer3:8763/eureka/
instance:
prefer‐ip‐address: true
instance-id: USER-SERVICE #自定義服務名稱
eurekaUserName: amor
eurekaPassWord: amor
management:
endpoints:
web:
exposure:
include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- Application類上
@EnableDiscoveryClient
高可用集羣(Docker)
服務端 Eureka Server
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- yml
server:
port: 8761 #註冊中心的地址
spring:
application:
name: eureka-server
eureka:
instance:
hostname: ${HOST}
lease-renewal-interval-in-seconds: 1 #心跳檢測檢測時間,每間隔1s,向服務端發送一次心跳,表明自己依然”存活“
lease-expiration-duration-in-seconds: 2 #續約時間,告訴服務端,如果我2s之內沒有給你發心跳,就代表我“死”了,將我剔除
server:
enable-self-preservation: false #關閉自我保護機制
client:
service-url:
defaultZone: ${EUREKA_SERVER_URL}
register-with-eureka: false # 對於註冊中心的優化,表明不向註冊中心註冊自己
fetch-registry: false # 註冊中心的優化,代表該註冊中心服務,不拉取任何的服務列表
management:
endpoints:
web:
exposure:
include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- Application類上
@EnableEurekaServer //開啓eureka server功能
- 打包jar 編寫dockerfile
FROM java:8-alpine
MAINTAINER "LHL <[email protected]>"
ADD *.jar app.jar
EXPOSE 8761
ENTRYPOINT ["java","-jar","/app.jar"]
- 構建鏡像
docker build ./eureka -t "eureka-server:1.0"
- 創建並運行容器
- 192.168.0.110
docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.111:8761/eureka,http://192.168.0.112:8761/eureka -e HOST=192.168.0.110 -d eureka-server:1.0
- 192.168.0.111
docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.110:8761/eureka,http://192.168.0.112:8761/eureka -e HOST=192.168.0.111 -d eureka-server:1.0
- 192.168.0.112
docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.110:8761/eureka,http://192.168.0.111:8761/eureka -e HOST=192.168.0.112 -d eureka-server:1.0
客戶端 Eureka Client
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml
eureka:
client:
service‐url:
defaultZone: http://192.168.0.110:8761/eureka,http://192.168.0.111:8761/eureka,http://192.168.0.112:8761/eureka
instance:
prefer‐ip‐address: true #訪問路徑可以顯示ip地址
instance-id: USER-SERVICE #自定義服務名稱
management:
endpoints:
web:
exposure:
include: "*" #因爲springboot2.1必須加上,支持訪問/actuator/hystrix.stream
- Application類上
@EnableDiscoveryClient