Spring Cloud Eureka是Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的微服务治理功能.

服务端

依赖

settings.gradle

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.yml

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.

客户端

依赖

settings.gradle

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

SpringCloud学习笔记-Eureka基础的更多相关文章

  1. 006 SpringCloud 学习笔记2-----SpringCloud基础入门

    1.SpringCloud概述 微服务是一种架构方式,最终肯定需要技术架构去实施. 微服务的实现方式很多,但是最火的莫过于Spring Cloud了.SpringCloud优点: - 后台硬:作为Sp ...

  2. SpringCloud学习笔记(一)——基础

    什么是微服务架构 简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进 ...

  3. 【转】SpringCloud学习笔记(一)——基础

    什么是微服务架构 简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进 ...

  4. SpringCloud学习笔记(2):使用Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...

  5. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  6. SpringCloud学习笔记(4):Hystrix容错机制

    简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...

  7. SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

    简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...

  8. SpringCloud学习笔记(6):使用Zuul构建服务网关

    简介 Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强.服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可 ...

  9. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

随机推荐

  1. jmter脚本运行结果实时监控

    一.背景 我们很多时候在使用JMeter做性能测试,我们很难及时察看压测过程中应用的性能状况,总是需要等到测试完成后去看Report 二.解决方案 JMeter引入Backend Listener,用 ...

  2. SpringCloud2.0 Hystrix Dashboard 断路器指标看板

    原文:https://www.cnblogs.com/songlu/p/9973856.html 1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-ser ...

  3. datagrid 溢出文本显示省略号 转载

    http://www.jeasyuicn.com/?sort=3 .datagrid-cell, .datagrid-cell-group, .datagrid-header-rownumber, . ...

  4. 接口自动化测试框架【windows版】:jmeter + ant + jenkins

    为了提高回归效率及保证版本质量,很多公司都在做自动化测试,特别是接口自动化.接口自动化测试框架很多,有写代码的,也有不写代码的,我觉得没有谁比谁好,谁比谁高级之说,只要适用就好. 今天给大家分享一个不 ...

  5. class struct Equals

    { class clsA { private int _i; public int I { set { _i = value; } get { return _i; } } } struct strc ...

  6. Proxy监听对象的数据变化,处理绑定数据很有用

    Proxy可以监听对象身上发生了什么事情,并在这些事情发生后执行一些相应的操作.一下子让我们对一个对象有了很强的追踪能力,同时在数据绑定方面也很有用处. }; //interceptor 拦截 var ...

  7. LwIP应用开发笔记之八:LwIP无操作系统HTTP客户端

    前面我们实现了TCP服务器和客户端的简单应用,接下来我们实现一个基于TCP协议的应用协议,那就是HTTP超文本传输协议 1.HTTP协议简介 超文本传输协议(Hyper Text Transfer P ...

  8. jdk是什么

    jdk是对java基础环境和相应开发平台标准和工具包的封装(zip) 开发平台 j2se j2ee j2me; 基础环境: 虚拟机.运行环境 JDK是整个JAVA的核心,包括了Java运行环境JRE( ...

  9. 关于maven导入工程pom文件报错问题及解决

    pom文件头报错 1.导入maven文件,经常遇到表头出错问题.报错:Failure to transfer org.apache.maven.shared:maven-filtering:pom:1 ...

  10. Linux yum 包 下载地址

    yum包网址: http://www.rpmfind.net/linux/rpm2html/search.php?query=yum