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

相关文章
相关标签/搜索