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 ...
随机推荐
- linux mint安装ffmpge
sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ffmpeg
- javaweb技术入门
JavaWeb巩固和进阶 1.如何配置外部应用? 方法一: server.xml 在<Host>中添加如下配置 <Context path="/xxx" docB ...
- Spring只定义接口自动代理接口实现类
能够扫描到包 @ComponentScan("org.zxp.esclientrhl") ESCRegistrar类主要实现ImportBeanDefinitionRegistra ...
- VisualStudio自定义调试工具(GIS)
闲言 偶尔分享技术,对,这次就是偶尔,几年一次(技术自卑).上周末竟然有人催更,也是受宠...若惊.以后会主动定期更的,可能. 前言 Visual Studio 调试器自带很多调试工具,调 ...
- css 添加手状样式,鼠标移上去变小手
cursor:pointer, 简单实用. 前端工作一年多,竟然没有博客.说出来别人都要笑话,这是一个新的开始.
- SpringBoot注入Service失败
Description: The bean 'userService' could not be injected as a 'com.phy.hemanresoruce.service.UserSe ...
- 关于SpringBoot 1.x和2.x版本差别
有点小差别 基本上基于SpringBoot的代码不需要改动,但有些配置属性和配置类,可能要改动,改动原因是 配置和类的更新或者是改名一般正常的MVC,数据库访问这些都不需要改动,下面按照本书章节说明区 ...
- Java入门之File类和IO流
1.File类 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作 . 构造方法: public File(String pathname) :通过将 ...
- mysql引号与esc键下方键
navicat导出数据表发现建表语句如下: create table `product_category` ( `category_id` int not null auto_increment, ` ...
- CSS 选择符有哪些?哪些属性可以继承?优先级算法如何计算?
CSS 选择符有哪些? 1.id选择器(#id) 2.类选择器(.class) 3.标签选择器(div,h1,p) 4.相邻选择器(h1 + p) 5.子选择器(ul > li) 6.后代选择器 ...