配置 Eureka

原文鏈接:https://github.com/Netflix/eureka/wiki/Configuring-Eureka

Eureka 包含兩個組件(components): Eureka ClientEureka Server. 如果你的架構使用了 Eureka. 通常意味着你的程序包含兩個應用程序。

  • Application Client 它會使用 Eureka Client 向 Application Service 發送請求。
  • Application Service 它會接收 Application Client 發送的請求,然後返回一個響應。

搭建會包含以下部分:

  • Eureka Server
  • Eureka Client for the application client
  • Eureka Client for the application service

Eureka 可以運行在亞馬遜雲 和 飛亞馬遜雲 環境中。

如果你運行在雲環境下,你將要用 java 命令行 傳遞屬性 -Deureka.datacenter=cloud. 只有這樣, Eureka Client/Server 才知道初始化 AWS 雲所特有的信息。

配置 Eureka Client

  • JDK 1.8 或在 更高的版本

你有幾種方式獲取 Eureka client 而鏡子文件。最好獲取最新的版本,因爲最新版會修復更多的bugs.

  • 你可以通過URL 直接下載 Eureka Client 的 二進字文件,傳送門

  • 你可以添加 eureka client 的 maven 依賴

<dependency>
  <groupId>com.netflix.eureka</groupId>
  <artifactId>eureka-client</artifactId>
  <version>1.1.16</version>
 </dependency>
  • 你還可以根據說明從源碼直接構建,在這裏

配置

配置 Eureka client 最簡單的方法是使用屬性文件(property files). 默認情況下,Eureka client 會在 classpath 下搜索名叫 eureka-client.properties 的屬性文件。接着會搜索環境特定覆蓋中特定於環境的屬性文件。這裏的環境通常指 測試環境 或者 生產環境,用命令行參數 -Deureka.environment 指定。 以切換 eureka client. 這個情況下,client 會搜索 eureka-client-test.properties 或者 eureka-client-product.properties.

你可以在這裏看一下默認配置的例子。把這些配置拷貝下來,只修改你需要的部分,然後把他們放到你的類路徑下面。如果你想改變文件的名稱,那就需要使用命令行參數 -Deureka.client.props=xxxxx(不需要帶後綴)進行切換。xxxxx 就是你要搜索的文件名稱。

  • sample-eureka-client.properties
 
###Eureka Client configuration for Sample Eureka Client

# see the README in eureka-examples to see an overview of the example set up

# note that for a purely client usage (e.g. only used to get information about other services,
# there is no need for registration. This property applies to the singleton DiscoveryClient so
# if you run a server that is both a service provider and also a service consumer,
# then don't set this property to false.
eureka.registration.enabled=false

## configuration related to reaching the eureka servers
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8080/eureka/v2/

eureka.decoderName=JacksonJson
  • sample-eureka-service.properties
 
###Eureka Client configuration for Sample Service that register with Eureka

# see the README in eureka-examples to see an overview of the example set up

## configurations related to self identification for registration.
## There are other properties that can be defined, see eureka-client/../CloudInstanceConfig for full details.
# where am I deployed?
eureka.region=default

# what is my application name? (clients can query on this appName)
eureka.name=sampleRegisteringService

# what is my application virtual ip address? (clients can query on this vipAddress)
eureka.vipAddress=sampleservice.mydomain.net

# what is the port that I serve on? (Change this if port 8001 is already in use in your environment)
eureka.port=8001

## configuration related to reaching the eureka servers
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8080/eureka/v2/

文件中的屬性表明了他們是什麼,至少需要配置下面這些項:

Application Name (eureka.name)
Application Port (eureka.port)
Virtual HostName (eureka.vipAddress)
Eureka Service Urls (eureka.serviceUrls)

更高級的配置,可以參考 EurekaInstanceConfig.javaEurekaClientConfig.java 的代碼。

配置 Eureka Server 端

要求

  • JDK 1.8 或更高的版本
  • Tomcat 6.0.10 或更高的版本。

你可以通過兩種放鬆獲取 Eureka server 的 二進字文件。

配置

Eureka Server 有兩部分配置

  • 和 Eureka Client 一樣的配置,就像上面描述的那樣。
  • Eureka Server 配置。

配置 Eureka 服務器的最簡單方法是使用類似於上述 Eureka 客戶端的屬性文件。首先,配置與上面指定的服務器一起運行的 Eureka 客戶端。Eureka 服務器本身觸發了一個 Eureka 客戶端,它用於查找其他 Eureka 服務器。因此,您需要首先爲Eureka 服務器配置 Eureka 客戶端,就像對連接到 Eureka 服務的任何其他客戶端一樣。Eureka 服務器將使用其 Eureka 客戶端配置來標識具有相同名稱 (即) eureka.name

配置 Eureka 客戶端後,如果您在 AWS 中運行,則可能需要配置 Eureka 服務器。默認情況下,Eureka 服務器在類路徑中搜索屬性文件 eureka-server.properties。它進一步搜索環境特定覆蓋中特定於環境的屬性文件。環境通常是測試或程序,由 -Deureka.environment.環境 java 命令行切換到 eureka 服務器(不帶 .properties 後綴)提供。因此,服務器還會搜索 eureka-server-test.properties 或者 eureka-server-prod.properties。

在本地環境上配置

當運行 eureka 服務器進行本地開發時,通常需要等待 3 分鐘,直到它完全啓動。這是因爲默認服務器行爲,即搜索對等體以同步,並在找不到可用的對等體時重試。通過設置屬性 eureka.numberRegistrySyncRetries=0,可以減少此等待時間。

在 AWS 上配置

如果在 AWS 中運行,則需要其他配置,如此處所述。有關更高級的服務器配置,請參閱此處提供的選項

如果要構建 WAR 存檔,則可以在 eureka-server/conf 下編輯文件,並且生成在創建存檔之前負責將屬性文件放在 WEB-INF/classes。

如果要從 maven 下載存檔,則可以自行合併 WEB-INF/類下編輯的屬性文件。

運行演示應用程序可以幫助您更好地瞭解配置。

Client/Server 版本兼容問題

我們使用 eureka 的語義版本控制,並將維護次要版本升級之間的客戶端/服務器協議兼容性(即 1.x 版本在客戶端和服務器部署之間應具有兼容的協議)。通常,讓服務器使用比客戶端較新的版本始終更安全。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章