Eureka 簡要介紹
Eureka 是 Netflix 開發的,一個基於 REST 服務的,服務註冊與發現的組件
Eureka集羣高可用配置
一般生產的環境中,一定要確保服務的正常使用,不能出現服務宕機的情況;在微服務架構下這種要求更加重要,註冊中心作爲微服務架構中的重要一環,在設計之初就已經考慮到服務的單點問題;單節點的eureka服務很難保證服務的不間斷,如果eureka服務宕機,則會導致整個項目應用都會出現問題,基於單點故障的問題,最好的解決方案就是採用eureka集羣部署方式搭建eureka的高可用,再多個註冊中心的模式下,多個eureka服務之間相關注冊,同步註冊數據,在一個eureka宕機的情況下,其他服務仍然可以提供服務註冊和服務發現功能,從而保證註冊中心的高可用。
搭建步驟
POM依賴
<dependencies>
<!--添加Eureka server的jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--添加安全認證的jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<!-- actuator監控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--注意此處如果不配置這個打包插件的話,可能會導致spring-boot-starter-actuator /info獲取空信息-->
<!--參考:https://blog.csdn.net/weixin_34402408/article/details/86001236-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
啓動類增加註解
/**
* Eureka服務註冊中心
* @author Taoweidong
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {
/**
* 啓動類
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class, args);
}
}
增加配置文件application.properties
# 應用啓動的端口號
server.port=8762
#激活配置文件,默認爲master
spring.profiles.active=slave
#服務名稱
spring.application.name=eureka-service
#安全認證相關配置
#開啓安全認證
security.basic.enabled=false
#安全認證用戶名
security.user.name=admin
#安全認證密碼
security.user.password=admin123
增加主註冊中心配置文件application-master.properties
#Eureka的相關配置
#應用的主機名稱
eureka.instance.hostname=master
# 通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server. 這兩個參數的默認值都是True,因此作爲客戶端是可不進行配置,使用默認值就可以了
#值爲false意味着自身僅作爲服務器,不作爲客戶端,進行集羣式設置爲true
eureka.client.register-with-eureka=false
# 值爲false意味着無需註冊自身
eureka.client.fetch-registry=false
#Eureka的自我保護機制,True表示開啓,false表示關閉,默認爲開啓狀態
eureka.server.enable-self-preservation=true
#清理間隔(單位毫秒)驅逐下線的服務,間隔是5秒,默認是60
eureka.server.eviction-interval-timer-in-ms=5000
#
# 指明瞭本應用的URL
eureka.client.serviceUrl.defaultZone=http://admin:admin123@${eureka.instance.hostname}:${server.port}/eureka/
增加從註冊中心配置application-slave.properties
#Eureka的相關配置
#應用的主機名稱
eureka.instance.hostname=slave
# 通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server. 這兩個參數的默認值都是True,因此作爲客戶端是可不進行配置,使用默認值就可以了
#值爲false意味着自身僅作爲服務器,不作爲客戶端,進行集羣式設置爲true
eureka.client.register-with-eureka=false
# 值爲false意味着無需註冊自身
eureka.client.fetch-registry=false
#Eureka的自我保護機制,True表示開啓,false表示關閉,默認爲開啓狀態
eureka.server.enable-self-preservation=true
#清理間隔(單位毫秒)驅逐下線的服務,間隔是5秒,默認是60
eureka.server.eviction-interval-timer-in-ms=5000
#
# 指明瞭本應用的URL
eureka.client.serviceUrl.defaultZone=http://admin:admin123@${eureka.instance.hostname}:${server.port}/eureka/
修改host文件-windows
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 master
127.0.0.1 slave
修改host文件-linux
結構圖
使用兩個端口啓動master和slave
啓動命令
# 殺死註冊中心進程
ps -ef | grep microservice-getway-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $2}' | xargs kill -9
# 啓動註冊中心
nohup java -jar microservice-discovery-eureka-0.0.1-SNAPSHOT.jar --server.port=8761 --spring.profiles.active=master > eureka-master.log 2>&1 &
nohup java -jar microservice-discovery-eureka-0.0.1-SNAPSHOT.jar --server.port=8762 --spring.profiles.active=slave > eureka-slave.log 2>&1 &