從零開始玩轉SpringCloud(一):Eureka註冊中心
Eureka
介紹:Eureka是Spring Cloud Netflix微服務套件中的一部分,可以與Springboot構建的微服務很容易的整合起來。Eureka包含了服務器端和客戶端組件。服務器端,也被稱作是服務註冊中心,用於提供服務的註冊與發現。Eureka支持高可用的配置,當集羣中有分片出現故障時,Eureka就會轉入自動保護模式,它允許分片故障期間繼續提供服務的發現和註冊,當故障分片恢復正常時,集羣中其他分片會把他們的狀態再次同步回來。客戶端組件包含服務消費者與服務生產者。在應用程序運行時,Eureka客戶端向註冊中心註冊自身提供的服務並週期性的發送心跳來更新它的服務租約。同時也可以從服務端查詢當前註冊的服務信息並把他們緩存到本地並週期性的刷新服務狀態。
Eureka-Server服務搭建
- 引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 在Application中使用@EnableEurekaServer
package com.example.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
- 配置文件
spring:
application:
name: eureka-server # cAPP名稱,在Eureka註冊名稱
profiles:
active: peer
eureka:
instance:
hostname: peer # 服務註冊中心實例的主機名
client:
register-with-eureka: false # 是否註冊自己
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false #關閉自我保護機制
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8760
至此一個Eureka-Server就搭建好了。
另外
Eureka-Server服務高可用
說到高可用,就是要保證一個節點掛掉,不會影響整個系統的運行。解決辦法就是多部署幾個實例,搭建集羣,那麼一個實例節點掛掉,其他實例仍可提供服務。
- 新建三個配置文件application-peer1.yml、application-peer2.yml、application-peer3,
spring:
application:
name: eureka-server
profiles:
active: peer1
eureka:
instance:
hostname: peer1 #服務註冊中心實例的主機名
client:
serviceUrl: # 另外幾個註冊中心地址
defaultZone: http://localhost:8762/eureka, http://localhost:8763/eureka
server:
enableSelfPreservation: false #關閉自我保護
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8761
spring:
application:
name: eureka-server
profiles:
active: peer2
eureka:
instance:
hostname: peer2 #服務註冊中心實例的主機名
client:
serviceUrl: # 另外幾個註冊中心地址
defaultZone: http://localhost:8761/eureka, http://localhost:8763/eureka
server:
enableSelfPreservation: false #關閉自我保護
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8762
spring:
application:
name: eureka-server
profiles:
active: peer3
eureka:
instance:
hostname: peer3 #服務註冊中心實例的主機名
client:
serviceUrl: # 另外幾個註冊中心地址
defaultZone: http://localhost:8761/eureka, http://localhost:8762/eureka
server:
enableSelfPreservation: false #關閉自我保護
logging:
level:
com:
netflix:
eureka: info
discovery: info
server:
port: 8763
- idea下按配置文件啓動多個項目,Active profiles指定啓動配置文件。分別啓動剛剛寫好的三個配置文件即可。
- 生產環境下是否需要動態配置註冊中心,目前的配置對生產環境動態配置非常不友好
客戶端配置
- 引入Eureka Client依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在Application中使用@EnableEurekaServer
package com.example.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
- 配置文件
spring:
application:
name: gateway # 在eureka-server註冊名稱
eureka:
client:
service-url:
defaultZone: http://localhost:8760/eureka/
# #高可用配置
# defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
server:
port: 8081
- 至此已EurekaClient已經搭建成功