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
❶ 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}
dependency-management
的緣由.能夠參考SpringBoot及SpringCloud版本管理(Gradle版本)spring.cloud.inetutils
,這個主要是解決在使用docker啓動時將服務註冊在docker0
網卡上致使服務間通訊阻塞問題.一塊兒來學Spring Cloud(F版) | 第一篇:認識Eureka
https://plugins.gradle.org/plugin/io.spring.dependency-management
https://stackoverflow.com/questions/38221227/gradle-configuration-of-pluginrepository