spring cloud 2.x版本 Eureka Server服务注册中心教程
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
1.创建服务注册中心
1.1 新建Spring boot工程:eureka-server
1.2 pom.xml所需依赖jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.3 EurekaServerApplication添加注解@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
@EnableEurekaServer:启用eureka server相关配置
1.4 添加配置文件内容:application.yml
spring:
application:
name: eureka-server
server:
port: 8701
#无参数启动
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false #默认为true,设置为false,仅作为服务中心,不作为服务客户端
fetch-registry: false #默认为true,设置为false, 不从服务中心检索注册的服务
server:
eviction-interval-timer-in-ms: 5000 #清理间隔(单位毫秒, 默认是60*1000)
enable-self-preservation: true #默认为true,设置为false,关闭自我保护
#eureka server: 在运行期间会去统计心跳失败比例在15分钟之内是否低于85%
renewal-percent-threshold: 0.49 #默认0.85
单机模式下:
register-with-eureka和fetch-registry应为false,否则启动会报错:Cannot execute request on any known server。原因,在默认设置下,eureka服务注册中心会将自己作为客户端来尝试注册自己。
1.5 启动服务
打开浏览器输入:http://localhost:8701, 显示如下:
红框内容代表还没有实例注册
结语
至此,一个简单的单机服务注册中心就搭建完成了。
搭建服务注册中心集群
为了保证服务的高可用,我们需要把单机应用改成集群应用,接下来,我们创建一个简单的eureka server集群.
1.1 修改本地host
- 127.0.0.1 eureka1.server.com
- 127.0.0.1 eureka2.server.com
- 127.0.0.1 eureka3.server.com
1.2 增加application.yml配置文件
增加application-server1.yml和application-server2.yml文件,同时修改原来的application.yml文件,文件内容如下:
- application.yml
spring:
application:
name: eureka-server
server:
port: 8701
#无参数启动
eureka:
instance:
hostname: eureka1.server.com
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
register-with-eureka: false
fetch-registry: true
server:
eviction-interval-timer-in-ms: 5000
enable-self-preservation: true
renewal-percent-threshold: 0.49
- application-server1.yml
spring:
application:
name: eureka-server
server:
port: 8702
#无参数启动
eureka:
instance:
hostname: eureka2.server.com
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
register-with-eureka: false
fetch-registry: true
server:
eviction-interval-timer-in-ms: 5000
enable-self-preservation: true
renewal-percent-threshold: 0.49
- application-server2.yml
spring:
application:
name: eureka-server
server:
port: 8703
#无参数启动
eureka:
instance:
hostname: eureka3.server.com
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
register-with-eureka: false
fetch-registry: true
server:
eviction-interval-timer-in-ms: 5000
enable-self-preservation: true
renewal-percent-threshold: 0.49 #默认0.85
1.3 在idea中添加启动服务
设置启动服务,按照截图中1234顺序依次添加
分别创建eureka-server2和eureka-server3.(注:eureka-server1用原来的启动就可以)
1.4 按照顺序分别启动3个eureka server服务
启动服务后分别访问 http://eureka1.server.com:8701,http://eureka1.server.com:8702,http://eureka1.server.com:8703 如图显示:
三个页面如上图显示就代表服务全部启动成功。至此,一个简单的服务中心集群就搭建完成。
总结
可以将application-server1.yml和application-server2.yml的配置信息都放到原application.yml配置中,通过‘---’ 三横杠模加spring.profiles模式来启动,同时增加启动参数: --spring.profiles.active=config-server1。本文采用过个application.yml的方式,方便以后的维护。
本文只是简单的搭建了服务注册中心的单机和集群应用,对eureka做为服务注册中心有一个简单对认识,后续会更新eureka的其他内容。
代码仓库
《Srping Cloud 2.X小白教程》目录
- spring cloud 2.x版本 Eureka Server服务注册中心教程
- spring cloud 2.x版本 Eureka Client服务提供者教程
- spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)
- spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)
- spring cloud 2.x版本 Zuul路由网关教程
- spring cloud 2.x版本 config分布式配置中心教程
- spring cloud 2.x版本 Hystrix Dashboard断路器教程
- spring cloud 2.x版本 Gateway路由网关教程
- spring cloud 2.x版本 Gateway自定义过滤器教程
- spring cloud 2.x版本 Gateway熔断、限流教程
- 写作不易,转载请注明出处,喜欢的小伙伴可以关注公众号查看更多喜欢的文章。
- 联系方式:4272231@163.com
spring cloud 2.x版本 Eureka Server服务注册中心教程的更多相关文章
- Spring Cloud系列(一):服务注册中心
一.Spring Cloud简介 Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线).分布式系统的协调导致了样 ...
- spring cloud 2.x版本 Eureka Client服务提供者教程
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...
- spring cloud 2.x版本 Gateway熔断、限流教程
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...
- Springcloud-alibaba学习实践(2)- nacos&Eureka Server服务注册实践
前言:上一篇已搭建好了springcloud服务注册中心(Nacos&Eureka Server),本篇继续代码实践,注册服务到服务中心,本篇只是演示了两种注册中心,后续我们以Nacos注册中 ...
- Spring Cloud之服务注册中心搭建Eureka Server服务注册中⼼
Spring Cloud并不与Spring MVC类似是一个开源框架,而是一组解决问题的规范(个人理解).解决哪些问题呢?如下: 1)服务管理:⾃动注册与发现.状态监管 2)服务负载均衡 3)熔断 4 ...
- spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)
绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...
- SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载)
场景 SpringCloud学习之运行第一个Eureka程序: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/90611451 S ...
- spring cloud 创建一个简单Eureka Server
在Spring Cloud实现一个Eureka Server是一件非常简单的事情.下面我们来写一个Eureka Server DEMO. 编码 父项目pom.xml <?xml version= ...
- spring cloud 使用Eureka作为服务注册中心
什么是Eureka? Eureka是在AWS上定位服务的REST服务. Eureka简单示例,仅作为学习参考 在pom文件引入相关的starter(起步依赖) /*定义使用的spring cloud ...
随机推荐
- 使用Docker安装FastDFS(分布式文件系统)
1. 获取镜像 可以利用已有的FastDFS Docker镜像来运行FastDFS. 获取镜像可以通过下载 docker image pull delron/fastdfs 也可是直接使用提前下载的镜 ...
- java几个常见的基础错误
1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...
- .Net Core 微服务容器系列基础目录篇
1.开场白 HI,各位老铁,大家端午好,之前写了些关于.net core商城系列的文章,有点乱,今天心血来潮想着整理一下(今天只是先把目录列出来,后面的每篇文章这两天会进行重新修改的,目前先将就看下) ...
- VR应用评测 - Luna
Luna http://store.steampowered.com/app/605770/Luna/ Steam VR 2017年10月发布 | 开发者:Funomena | 好评率92% 一款制作 ...
- python openpyxl内存不主动释放 ——关闭Excel工作簿后内存依旧(MemoryError)
在openpyxl对Excel读写操作过程中,发现内存没有马上释放,如果得多次读取大文件,内存爪机,后续代码就无法运行. 尝试:各种wb.save()或者with open等途径无法解决. 发现:因为 ...
- 【集群监控】Prometheus+AlertManager实现邮件报警
AlertManager下载 https://prometheus.io/download/ 解压 添加配置文件test.yml,配置收发邮件邮箱 Prometheus下载配置参考我的另一篇: htt ...
- Android系统介绍与框架
一.Andriod是什么? Android系统是Google开发的一款开源移动OS,Android中文名被国内用户俗称“安卓”.Android操作系统基于Linux内核设计,使用了Google公司自己 ...
- Redis之数据类型及命令
Redis(REmote DIctionary Server) 是一个遵守BSD协议.支持网络.可基于内存亦可持久化的日志型key-value存储系统. KEY 常用指令: 指令 注释 备注 exit ...
- GUI tkinter (Entry) 输入框篇
"""1.其他函数不常用,这里只说get函数,get函数使用的时候不需要任何参数,它的返回值就是该输入框的内容.""" from tkint ...
- RF分层封装
1.如何管理用例? (1).在ride工具中分层管理用例(案例层.元素层.流程层),提高效率 (2).偶尔运行下,保证脚本能正常跑动 2.用例分层操作 案例层:需要加载流程层.txt资源和Seleni ...