Spring Cloud學習筆記【二】Eureka服務註冊與發現

                     Spring Cloud學習筆記【二】Eureka服務註冊與發現

一、Eureka簡介

 

       Spring Cloud Eureka 是 Spring Cloud Netflix 微服務套件的一部分,基於 Netflix Eureka 做了二次封裝,主要負責完成微服務架構中的服務治理功能,服務治理可以說是微服務架構中最爲核心和基礎的模塊,他主要用來實現各個微服務實例的自動化註冊與發現。功能類似於dubbo的註冊中心,比如 Zookeeper。

服務註冊:在服務治理框架中,通常都會構建一個註冊中心,每個服務單元向註冊中心登記自己提供的服務,將主機與端口號、版本號、通信協議等一些附加信息告知註冊中心,註冊中心按照服務名分類組織服務清單,服務註冊中心還需要以心跳的方式去監控清單中的服務是否可用,若不可用需要從服務清單中剔除,達到排除故障服務的效果。

服務發現:由於在服務治理框架下運行,服務間的調用不再通過指定具體的實例地址來實現,而是通過向服務名發起請求調用實現。

Spring Cloud Eureka 使用 Netflix Eureka 來實現服務註冊與發現,即包括了服務端組件,也包含了客戶端組件,並且服務端和客戶端均採用 Java 編寫,所以 Eureka 主要適用與通過 Java 實現的分佈式系統,或是與 JVM 兼容語言構建的系統,但是,由於 Eureka 服務端的服務治理機制提供了完備的 RESTful API,所以他也支持將非 Java 語言構建的微服務納入 Eureka 的服務治理體系中來。

二、創建Eureka註冊中心

依賴管理配置:指定spring cloud的版本,和添加eureka的server依賴

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR1")
}
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
test {
    useJUnitPlatform()
}

 application.properties配置文件配置

spring.application.name=eureka
#eureka默認端口
server.port=8761
#禁止自己當做服務去註冊
eureka.client.register-with-eureka=false
#自我保護模式關閉
eureka.server.enable-self-preservation=false
# 清理無效節點的時間間隔(單位毫秒,默認是60*1000)
# 生產環境,不會頻繁重啓,所以,一定要把自我保護機制打開,否則網絡一旦終端,就無法恢復。
eureka.server.eviction-interval-timer-in-ms=10000    

啓動類配置 :添加@EnableEurekaServer註解即可

@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {

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

}

啓動訪問http://localhost:8761/

三、創建客戶端項目

依賴管理配置:指定spring cloud的版本,和添加eureka的client依賴

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR1")
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
test {
    useJUnitPlatform()
}

application.properties配置文件配置

spring.application.name=client
#往配置中心添加服務的地址
eureka.client.service-url.defaultZone:http://localhost:8761/eureka/

啓動類配置:添加@EnableEurekaClient註解即可

@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {

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

}

註冊成功 

 

 

 

 

 

 

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