學習筆記系列之SpringCloud | 第一章 服務註冊中心(Eureka)

Eureka 簡介

Eureka是Netflix開發的服務發現框架,本身是一個基於REST的服務,主要用於定位運行在AWS域中的中間層服務,以達到負載均衡和中間層服務故障轉移的目的。

SpringCloud將它集成在其子項目spring-cloud-netflix中,來實現SpringCloud的服務發現功能。通過一些簡單的註解,開發者就可以快速的在應用中配置一下常用模塊並構建龐大的分佈式系統。

Netflix主要提供的模塊包括:服務發現(Eureka),斷路器(Hystrix),智能路由(Zuul),客戶端負載均衡(Ribbon)等。

創建註冊中心

1、創建SpringBoot項目,引入Euraka依賴

<!--Eureka服務端依賴-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  • 注:使用Eureka服務,必須集成SpringCloud框架
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2、配置文件-bootstrap.yml

spring:
  application:
    name: registry-server #發佈後的服務名稱
eureka:
  instance:
    prefer-ip-address: true  #不使用主機名來定義註冊中心的地址,而使用IP地址的形式
  client:
    registerWithEureka: false #關閉服務註冊
    fetchRegistry: false # 關閉檢索服務
    serviceUrl:
      defaultZone: http://localhost:8889/eureka/ #指定服務註冊中心地址
    server:
      waitTimeInMsWhenSyncEmpty: 0 #在Eureka服務器獲取不到集羣裏對等服務器上的實例時,需要等待的時間,單位爲毫秒
  server:
    enable-self-preservation: false #關閉自我保護模式
    eviction-interval-timer-in-ms: 20000 #過期實例應該啓動並運行的時間間隔,單位爲毫秒,默認爲60 * 1000
server:
  port: 8889 #發佈後的服務端口號
security:
  user:
    name: user  #安全認證用戶名
    password: 123456   #安全認證密碼
  • 注:其中安全認證需要引入相關依賴
<!--安全認證依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

3、啓動服務註冊中心

需要在啓動類上添加註解:@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {

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

訪問註冊中心

服務啓動後,可通過界面訪問註冊中心,需要驗證用戶名密碼

路徑:http://localhost:8889/
如下圖所示:
這裏寫圖片描述

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