1. 程式人生 > >Spring Cloud官方文檔中文版-服務發現:Eureka服務端

Spring Cloud官方文檔中文版-服務發現:Eureka服務端

show hub script trying 不同 clu ask local code

官方文檔地址為:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server

文中例子我做了一些測試在:http://git.oschina.net/dreamingodd/spring-cloud-preparation

Service Discovery: Eureka Server 服務發現:Eureka服務端

How to Include Eureka Server 如何創建Eureka服務端

To include Eureka Server in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-eureka-server. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

使用Eureka服務需要引入org.springframework.cloud的spring-cloud-starter-eureka-server項目。可以參考http://projects.spring.io/spring-cloud/來創建你的第一個Eureka服務。

How to Run a Eureka Server 如何運行Eureka服務端

@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

The server has a home page with a UI, and HTTP API endpoints per the normal Eureka functionality under /eureka/*.

Eureka服務端有一個默認的UI主頁,每個Eureka服務端你都有一個HTTP API節點在/eureka/*

Eureka background reading: see flux capacitor and google group discussion.

想了解更多Eureka背景知識,推薦閱讀 https://github.com/cfregly/fluxcapacitor/wiki/NetflixOSS-FAQ#eureka-service-discovery-load-balancerhttps://groups.google.com/forum/?fromgroups#!topic/eureka_netflix/g3p2r7gHnN0

TIP Due to Gradle’s dependency resolution rules and the lack of a parent bom feature, simply depending on spring-cloud-starter-eureka-server can cause failures on application startup. To remedy this the Spring Boot Gradle plugin must be added and the Spring cloud starter parent bom must be imported like so:

gradle引入:

build.gradle

buildscript {
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.5.RELEASE")
  }
}
apply plugin: "spring-boot"
dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.RELEASE"
  }
}

High Availability, Zones and Regions 高可用,地區和地域

The Eureka server does not have a backend store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory). Clients also have an in-memory cache of eureka registrations (so they don’t have to go to the registry for every single request to a service).

Eureka服務器並不在後端存儲,但註冊機中的服務實例都必須用心跳信息維持他們的最新的註冊狀態(也就是說可以在內存完成)。客戶端同樣也有一份註冊信息緩存存在內存裏。

By default every Eureka server is also a Eureka client and requires (at least one) service URL to locate a peer. If you don’t provide it the service will run and work, but it will shower your logs with a lot of noise about not being able to register with the peer. 默認情況下,每一個Eureka服務器也時一個Eureka客戶端,同樣需要(至少一個)service URL來定位節點。雖然即使開發人員不提供仍然能跑,但是會大量打出無法註冊的垃圾log。

See also below for details of Ribbon support on the client side for Zones and Regions.

Standalone Mode 單機模式

The combination of the two caches (client and server) and the heartbeats make a standalone Eureka server fairly resilient to failure, as long as there is some sort of monitor or elastic runtime keeping it alive (e.g. Cloud Foundry). In standalone mode, you might prefer to switch off the client side behaviour, so it doesn’t keep trying and failing to reach its peers. Example:

只要存在某種監控或彈性運行時間來使服務存活(如Cloud Foundry),兩個緩存和心跳協議的組合就能讓單機的Eureka服務器對故障保有相當的彈性。單機模式下,開發人員可能更喜歡關閉服務器的客戶端行為,這樣服務端就不必一直嘗試失敗地訪問節點。

application.yml (Standalone Eureka Server)

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Notice that the serviceUrl is pointing to the same host as the local instance.

註意serviceUrl跟本地實例是一樣的。

Peer Awareness 節點感知(高可用)

Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. In fact, this is the default behaviour, so all you need to do to make it work is add a valid serviceUrl to a peer, e.g.

Eureka服務器通過跑多個實例並要求註冊彼此,可以變得更具彈性。實際上,這是Eureka的默認使用方式,那麽你需要給節點加入有效的serviceUrl使其正常工作。例如:

application.yml (Two Peer Aware Eureka Servers)

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/
---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/

In this example we have a YAML file that can be used to run the same server on 2 hosts (peer1 and peer2), by running it in different Spring profiles. You could use this configuration to test the peer awareness on a single host (there’s not much value in doing that in production) by manipulating /etc/hosts to resolve the host names. In fact, the eureka.instance.hostname is not needed if you are running on a machine that knows its own hostname (it is looked up using java.net.InetAddress by default).

通過運行不同的Spring profile,本例中的YAML配置文件是在不同主機上的同一服務器(節點1和節點2)。開發人員通過操縱/etc/hosts偽造127.0.0.1的主機名,可以在同一主機上測試(這樣做生產環境上沒有價值)。實際上,eureka.instance.hostname這個配置項在生產環境沒有用(PC會查詢java.net.InetAddres獲取到)。

You can add multiple peers to a system, and as long as they are all connected to each other by at least one edge, they will synchronize the registrations amongst themselves. If the peers are physically separated (inside a data centre or between multiple data centres) then the system can in principle survive split-brain type failures.

只要節點之間互相連接,開發人員可以為系統添加多個節點,它們之間會同步註冊信息。如果節點是物理分離的(在一個或多個數據中心),那麽系統原則上可以無視split-brain型故障而運行。

Prefer IP Address 使用IP

In some cases, it is preferable for Eureka to advertise the IP Adresses of services rather than the hostname. Set eureka.instance.preferIpAddress to true and when the application registers with eureka, it will use its IP Address rather than its hostname.

某些情況下,使用IP比hostname好。eureka.instance.preferIpAddress=true可以做到這一點。

dreamingodd原創文章,如轉載請註明出處。

Spring Cloud官方文檔中文版-服務發現:Eureka服務端