SpringCloud學習筆記-Eureka基礎

Spring Cloud Eureka是Spring Cloud Netflix微服務套件中的一部分,它基於Netflix Eureka作了二次封裝,主要負責完成微服務架構中的微服務治理功能.html

服務端

依賴

settings.gradlejava

pluginManagement {
    resolutionStrategy {
    }
    repositories {
        maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
        gradlePluginPortal()
    }
}
rootProject.name = 'swb-infra-eureka'

build.gradle正則表達式

buildscript {
    ext {
        //定義一個變量,統一規定springboot的版本
        springBootVersion = '2.0.1.RELEASE'
    }

}
plugins {
    id "idea"
    id "java"
    id 'org.springframework.boot' version "2.0.1.RELEASE"
    id 'io.spring.dependency-management' version "1.0.8.RELEASE"
}

group = 'com.swb'
version = '0.0.1-SNAPSHOT'

description = """swb-infra-eureka"""

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}
repositories {
    maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR1'
    }
}

dependencies {
    compile "org.springframework.cloud:spring-cloud-starter-netflix-eureka-server"
}

配置文件

application.ymlspring

spring:
  application:
    name: swb-infra-eureka
  profiles:
    active: ${ACTIVE_PROFILE:default}
  cloud:
    inetutils:
      # 首選的網絡地址,支持JAVA正則表達式
      preferred-networks: ${CLOUD_INETUTILS_PREFERRED_NETWORKS:127.0.0.1}
      # 忽略的網卡名,支持JAVA正則表達式,這在使用docker啓動時頗有用,解決多網卡註冊問題.
      ignored-interfaces: docker0, veth.*
server:
  port: 19100
  servlet:
    context-path: /
eureka:
  # lease-expiration-duration-in-seconds: 20
  # 生產環境中官方是不建議修改默認配置,由於那樣會破壞 eureka server 的保護模式
  server:
    # 關閉保護模式(生產環境不建議修改)
    enable-self-preservation: false
    # 清理間隔(默認是60 * 1000 毫秒)(生產環境不建議修改)
    eviction-interval-timer-in-ms: 10000
    # Eureka 拉取服務列表時間(默認:30秒)(生產環境不建議修改)
    remote-region-registry-fetch-interval: 5
  client:
    # eureka server 不必本身把本身註冊上去,因此能夠設置成 false
    register-with-eureka: false
    # 是否從Eureka Server上獲取註冊信息,默認爲true,此處建議修改爲 false (單機設置的意義不大,若是設置成 true 啓動會去抓取一次註冊表,獲取不到更新緩存就會出錯(該錯誤不影響 eureka 正常使用))
    fetch-registry: false
    service-url:
      # 默認註冊地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
      # 劃重點:此處的 defaultZone 千萬別寫成 default-zone
      defaultZone: http://${EUREKA_IP:127.0.0.1}:${server.port}/eureka/
      # 從 Eureka 服務器端獲取註冊信息的間隔時間(默認:30秒)
    registry-fetch-interval-seconds: 5

開啓註冊服務

在啓動類上添加註解@EnableEurekaServer.docker

客戶端

依賴

settings.gradle緩存

springboot

build.gradle服務器

buildscript {
    ext {
        //定義一個變量,統一規定springboot的版本
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
    }
}

plugins {
    id "org.springframework.boot" version "2.0.1.RELEASE"
    id "io.spring.dependency-management" version "1.0.8.RELEASE"
    id "idea"
    id "java"
}

group = 'com.XXX'
version = '0.0.1-SNAPSHOT'

description = """XXX"""

sourceCompatibility = 1.8
targetCompatibility = 1.8

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}
// 實時刷新依賴
configurations.all {
    resolutionStrategy {
        cacheChangingModulesFor 0, 'seconds'
        cacheDynamicVersionsFor 0, 'seconds'
    }
}

repositories {
    maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR1'
    }
}

dependencies {
    compile "org.springframework.cloud:spring-cloud-starter-netflix-eureka-client"
}

配置

application.yml網絡

只展現與eureka相關配置架構

eureka:
  instance:
    ip-address: ${EUREKA_INSTANCE_IP:${spring.cloud.client.ip-address:127.0.0.1}} #❶
    prefer-ip-address: true
    instance-id: ${eureka.instance.ip-address}:${server.port}:${spring.application.name} #❷
  client:
    serviceUrl:
      defaultZone: http://${EUREKA_IP:${spring.cloud.client.ip-address:127.0.0.1}}:19100/eureka/

啓動

啓動類添加註解@EnableDiscoveryClient@EnableEurekaClient

Notes

❶ 1.5.X版本能夠將此設置爲${spring.cloud.client.ipAddress},2.X對應的是${spring.cloud.client.ip-address},此處設置默認值127.0.0.1是爲了兼容版本.它們對應的源碼類全路徑是org.springframework.cloud.client.HostInfoEnvironmentPostProcessor

eureka.instance.instance-id是在eureka上展現的數據,真實訪問的IP爲eureka.instance.ip-address,此處爲了保持一致,所以直接引用了${eureka.instance.ip-address}

Tips

  • Spring Cloud 是套件,不是單獨的一個項目,所以版本號採用命名的方式,這也是爲何gradle中使用插件dependency-management的緣由.能夠參考SpringBoot及SpringCloud版本管理(Gradle版本)
  • 通常狀況下不用配置spring.cloud.inetutils,這個主要是解決在使用docker啓動時將服務註冊在docker0網卡上致使服務間通訊阻塞問題.

參考

一塊兒來學Spring Cloud(F版) | 第一篇:認識Eureka

SpringCloud的版本

https://plugins.gradle.org/plugin/io.spring.dependency-management

https://stackoverflow.com/questions/38221227/gradle-configuration-of-pluginrepository

https://www.coder4.com/archives/5884

相關文章
相關標籤/搜索