SpringCloud註冊中心-Eureka

一.SpringCloudEureka簡介

Spring-Cloud Euraka是Spring Cloud系列中一個組件,它是對Netflix中的開源框架Euraka的集成,用於服務註冊和發現。類似 Zookeeper、Consul等。

Eureka由多個instance(服務實例)組成,實例大體分爲:Eureka Server和Eureka Client。

在實際項目中Eureka Clienth可細分爲Service Provider(服務提供者)和Service Consumer(服務消費者)。

  • Eureka Server 提供服務註冊和發現
  • Service Provider 服務提供方,將自身服務註冊到Eureka,提供服務。
  • Service Consumer服務消費方,從Eureka獲取註冊服務列表,消費服務。

1.Eureka與zk的區別

在談二者區別前先要了解CAP理論,詳細可參考:http://www.ruanyifeng.com/blog/2018/07/cap.html

這裏簡單介紹下,CAP是一致性(Consistency)、可用性(Availability)、分區容錯性(Partition tolerance 類似於多機備份)

  • zk是CP,突出分佈式一致性,對zk每次的請求都保證一致性的結果,但無法保證可用性,當zk正在選舉時,zk不可提供服務。
  • Eureka 是AP,突出高可用可伸縮服務,可保證服務可提供服務,但無法保證每次請求的數據返回一致。

二.搭建Eureka註冊中心

1.配置pom.xml

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <!--   2.3.8版本boot支持cloud Hoxton.x 版本,更高的boot需要cloud Hoxton 以上纔可  -->
   <version>2.3.8.RELEASE</version>
   <relativePath/>
</parent>

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

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <!--   2.3.8版本boot支持cloud Hoxton.x 版本,更高的boot需要cloud Hoxton 以上纔可 ,詳情參考spring.io -->
      <version>Hoxton.SR11</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<build>
  <!--  jar或war包名稱  -->
  <finalName>springcloud-eureka-server</finalName>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <!--   指定主入口   -->
        <mainClass>com.example.springcloud.eureka.EurekaServerApplication</mainClass>
      </configuration>
    </plugin>
  </plugins>
</build>

2.配置application.properties

# 應用名稱
spring.application.name=eureka-server
# 配置動態切換
spring.profiles.active=dev
# 服務端口
server.port=8901
# eureka的主機名稱
eureka.instance.hostname=eureka1
# 註冊中心實例ID
eureka.instance.instance-id=${spring.application.name}:${eureka.instance.hostname}:${server.port}
# 客戶端配置-註冊中心訪問地址, eureka1是在Hosts配置的映射,替代域名的作用。
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
# 客戶端配置-是否將自身註冊到eureka服務
eureka.client.register-with-eureka=true

3.添加Eureka啓動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer // 啓用Eureka註冊中心
@SpringBootApplication // springboot 核心配置
public class EurekaServerApplication {

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

}

三、啓動測試

瀏覽器訪問:http://eureka1:8901/ 顯示如下:

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