Eureka服務(wù)注冊(cè)中心

Eureka服務(wù)注冊(cè)中心

一、Eureka Server

Eureka Server是服務(wù)的注冊(cè)中心,這是分布式服務(wù)的基礎(chǔ),我們看看這一部分如何搭建。

首先,Spring Cloud是基于Spring Boot的,所以我們的項(xiàng)目都是Spring Boot項(xiàng)目。需要引入最基礎(chǔ)的Spring Boot的jar包,如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

然后,再引入Eureka Server的jar包:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

接下來(lái)我們編寫(xiě)啟動(dòng)類:

@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

其中@EnableEurekaServer標(biāo)志著這個(gè)服務(wù)是一個(gè)Eureka Server。

下面就是最重要的application.yml的配置,我們先從最簡(jiǎn)單的單例模式說(shuō)起。

單例模式

這種模式下,Eureka Server是一個(gè)單點(diǎn),如果發(fā)生故障,則整個(gè)注冊(cè)中心不可用,只適用于測(cè)試環(huán)境,具體配置如下:

spring:
  profiles: standalone
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:8761
    fetch-registry: false
    register-with-eureka: false
server:
  port: 8761

在單例模式下,我們關(guān)掉客戶端的行為。其中fetch-registry是抓取注冊(cè)的服務(wù),register-with-eureka是將自己本身向其他的Eureka Server 注冊(cè)。
這兩項(xiàng)在集群配置時(shí)是必須打開(kāi)的,這樣才能使注冊(cè)的服務(wù)同步到其他節(jié)點(diǎn)上。

在單例模式下,eureka.instance.hostname必須是localhost,而且defaultZone不能使用ip,要使用eureka.instance.hostname且走域名解析才可以。
這里我們配置的是localhost,不需要修改hosts文件。這塊不知道為什么Spring Cloud要這么設(shè)計(jì),小編在搭建集群時(shí)折騰了好長(zhǎng)時(shí)間。
我們啟動(dòng)服務(wù),訪問(wèn)http://localhost:8761,可以看到Eureka的管理頁(yè)面。

集群模式

我們可以通過(guò)創(chuàng)建多個(gè)Eureka Server實(shí)例,并讓他們相互注冊(cè)來(lái)實(shí)現(xiàn)集群。相互注冊(cè)就是我們前面提到的fetch-registryregister-with-eureka,
它們默認(rèn)都是true,所以不需要配置,我們需要制定其他節(jié)點(diǎn)的url就可以了,我們以3個(gè)節(jié)點(diǎn)為例:

spring:
  application:
    name: eureka-server

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    service-url:
      defaultZone: http://peer2:9200/eureka/,http://peer3:9300/eureka/
server:
  port: 9100
---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://peer1:9100/eureka/,http://peer3:9300/eureka/
server:
  port: 9200
---

spring:
  profiles: peer3
eureka:
  instance:
    hostname: peer3
  client:
    service-url:
      defaultZone: http://peer1:9100/eureka/,http://peer2:9200/eureka/
server:
  port: 9300

我們?cè)谝慌_(tái)機(jī)器上起3個(gè)實(shí)例,peer1、peer2、peer3,端口分別為:9100、9200、9300。
這里我們還是要注意一下instance.hostnameservice-url

  • 3個(gè)實(shí)例的instance.hostname不能重復(fù),否則集群搭建失敗
  • service-url不能使用ip+端口直接訪問(wèn),否則也會(huì)失敗

在單機(jī)情況下,我們需要配置hosts來(lái)解析peer1、peer2、peer3

127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3

集群搭建成功:

管理頁(yè)面

DS Replicas顯示兩個(gè)副本節(jié)點(diǎn),available-replicas顯示兩個(gè)可用的副本節(jié)點(diǎn)。

如果在真實(shí)的物理機(jī)上,我們可以不通過(guò)配置hosts的方式也是可以的,記住這是 真實(shí)的物理機(jī)的情況下,
每臺(tái)機(jī)器的ip都不一樣
。配置如下:

spring:
  application:
    name: eureka-server

---
spring:
  profiles: peer1
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://ip2:9200/eureka/,http://ip3:9300/eureka/
server:
  port: 9100
---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://ip1:9100/eureka/,http://ip3:9300/eureka/
server:
  port: 9200
---

spring:
  profiles: peer3
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://ip1:9100/eureka/,http://ip2:9200/eureka/
server:
  port: 9300

實(shí)例的名稱可以使用ip去注冊(cè),當(dāng)然每個(gè)ip不一樣,也不會(huì)重復(fù),不會(huì)導(dǎo)致失敗。

至此,我們的Eureka Server就搭建完了,具體參照GitHub地址:https://github.com/liubo-tech/spring-cloud-eureka

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容