Spring cloud eureka 入门使用及指导说明(单机篇)
2018-03-07 17:47
513 查看
基本概念和方案
Eureka是基于REST(Representational State Transfer,代表性状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移。我们称这个服务为Eureka服务器。Eureka还带有一个基于Java的客户端组件,即Eureka客户端,它使与服务的交互更容易。客户端还有一个内置的负载均衡器,可以进行基本的循环负载均衡。在Netflix,一个更复杂的负载平衡器包装Eureka提供加权负载平衡的基础上的几个因素,如流量,资源使用情况,错误条件等,以提供卓越的弹性。
集群方案逻辑架构图如下:
上面的体系结构描述了Netflix如何部署Eureka,这是通常运行的方式。每个区域一个Eureka集群区域,负责该区域内的服务实例。每个区域至少有一台Eureka服务器来处理区域故障。
服务注册Eureka,然后发送心跳每30秒更新一次租约。如果客户端无法续租几次,则会在大约90秒内将其从服务器注册表中取出。注册信息和更新被复制到群集中的所有Eureka节点。来自任何区域的客户端可以查找注册表信息(每30秒发生一次)以找到他们的服务(可能位于任何区域)并进行远程调用。
Eureka-Server
搭建Server端,有三种方式:第一种,可以直接去到Maven库下载war,放入Tomcat中即可使用,地址:
http://search.maven.org/#search%7Cga%7C1%7Ceureka-server
b7cd
第二种,可以去到Github下载Netflix/eureka最新的源码自己编译,地址:
https://github.com/Netflix/eureka
第三种,也是本章节主要的讲解,通过Spring boot创建一个Eureka Server项目。
[b]1、pom.xml 引入Eureka Server依赖[/b]
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>1.4.2.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>[b]2、配置文件application.properties[/b]server.port=8765
#表示是否注册Eureka服务器,因为自身作为服务注册中心,所以为false
eureka.client.registerWithEureka=false
#是否从eureka上获取注册信息,同上eureka.client.fetchRegistry=false
[b]3、创建一个启动类EurekaServerApplication,并启动[/b]
运行启动类,访问 http://127.0.0.1:8761/,查看系统状态,如下图:
Eureka-Client
[b]1、pom.xml 引入Eureka Client依赖[/b]<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.2.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
[b]2、配置文件application.properties[/b]server.port=18765
# 连接注册中心的服务地址eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka
[b]3、创建一个启动类EurekaServerApplication,并启动[/b]
相关说明
1、pom.xml引入包的选择问题使用最新的 spring-cloud-starter-netflix-eureka-server,而spring-cloud-starter-eureka-server已经过期。2、@EnableEurekaClient和@EnableDiscoveryClient的区别spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等),@EnableDiscoveryClient基于spring-cloud-commons,@EnableEurekaClient基于spring-cloud-netflix。
就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。
相关文章推荐
- Spring cloud config 入门使用及指导说明(单机篇)
- Spring Cloud--Eureka(一)入门使用
- Spring Cloud Eureka 入门 (二)服务提供者详解
- Spring Cloud Eureka 入门 (一)服务注册中心详解 「Spring Cloud Eureka 入门系列」 Spring Cloud Eureka 入门 (一)服务注册中心详解 Spr
- Spring Cloud入门二:eureka集群
- Spring Cloud Eureka 入门 (一)服务注册中心详解
- Spring Cloud Eureka 入门 (二)服务提供者详解
- 《Spring Cloud Netflix》 -- 服务注册和服务发现-Eureka 的使用
- SpringCloud构建微服务入门架构(二)服务注册与发现Eureka集群搭建
- Spring Cloud Eureka 入门 (三)服务消费者详解
- Spring Cloud Edgware新特性之一:解决Eureka中Jersey 1.x版本过旧的问题-不使用Jersey
- spring-cloud入门之eureka-server(服务发现)
- 在spring cloud eureka中使用ip而不是hostname
- spring-cloud中config配置中心使用(基于eureka的高可用)
- Spring Cloud Eureka 入门 (二)服务提供者详解
- 嗡汤圆的Spring Cloud自学(PART.2):eureka服务注册与代理端使用
- Spring Cloud Eureka 常用配置及说明
- (二)SpringBoot+SpringCloud —— 使用Eureka实现服务注册与发现
- Spring Cloud Eureka 入门 (三)服务消费者详解
- Spring Cloud Eureka 入门 (二)服务提供者详解