SpringCloud——Eureka服务注册和发现
一、SpringCloud和Dubbo
SpringCloud整合了一套较为完整的微服务解决方案框架,而Dubbo只是解决了微服务的几个方面的问题。
| content | Dubbo | SpringCloud |
|---|---|---|
| 服务注册中心 | zookeeper | Spring Cloud Netflix Eureka |
| 服务调用方式 | RPC | REST API |
| 服务网关 | 无 | Spring Cloud Netflix Zuul |
| 断路器 | 不完善 | Spring Cloud Netflix Hystrix |
| 分布式配置 | 无 | Spring Cloud Config |
| 服务跟踪 | 无 | Spring Cloud Sleuth |
| 消息总线 | 无 | Spring Cloud Bus |
| 数据流 | 无 | Spring Cloud Stream |
| 批量任务 | 无 | Spring Cloud Task |
当然,虽然dubbo没有提供很多解决方案,但他也可以整合第三方的项目来实现。
二、Demo
今天介绍的服务发现是在SpringCloud的子项目Netflix中,除此之外,他还提供了熔断器、负载均衡、智能路由等,之后会介绍到。
和往常一样,我们先来实现这个实例,然后再分析。这里,我们需要一个服务注册中心(即下面例子中的eureka-server)和一个服务的提供方(eureka-provider)。
1、服务注册中心
1)、引入pom文件
<!--基于Springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!--设置字符编码及java版本-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--增加eureka-server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--用于测试的,本例可省略-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2)、配置
新建application.properties,注意名称只能是这个,不然不会被识别。
server.port=8761
#注册中心默认端口就是8761,也可通过下面的方式定义其他端口
#eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
在application.properties配置文件中使用如下配置:
server.port=8761
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
其中server.port配置eureka服务器端口号。Eureka的配置属性都在开源项目spring-cloud-netflix-master中定义(spring boot连文档都没有,只能看源码了),
在这个项目中有两个类EurekaInstanceConfigBean 和EurekaClientConfigBean,分别含有eureka.instance和eureka.client相关属性的解释和定义。
从中可以看到,registerWithEureka表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。
fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要。defaultZone就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。
3)、启动类
@EnableEurekaServer //启动一个服务注册中心提供给其他应用进行对话
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
//下面两行代码都可以用来启动
SpringApplication.run(ServerApplication.class, args);
//new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4)、测试
在浏览器中输入http://localhost:8761 就会显示:
2、服务提供者
1)、pom文件跟注册中心类似,只有eureka有区别:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2)、配置文件
#应用(服务)名称
spring.application.name=compute-service
server.port=8762
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
3)、Java Code
Controller中,通过DiscoveryClient发现服务。
启动类
@EnableDiscoveryClient //激活eureka中的DiscoveryClient实现
@SpringBootApplication
public class ComputeServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
}
}
4)、测试
在浏览器中输入http://localhost:8762/hello?name=Sherry或http://yfywyangsx.hiersun.com:hello-service:8762/hello?name=Sherry
##
Spring Cloud中,Eureka常见问题总结。
指定Eureka的Environment
|
1
|
eureka.environment: 指定环境
|
参考文档:https://github.com/Netflix/eureka/wiki/Configuring-Eureka
指定Eureka的DataCenter
|
1
|
eureka.datacenter: 指定数据中心
|
参考文档:https://github.com/Netflix/eureka/wiki/Configuring-Eureka
文中指出,配置-Deureka.datacenter=cloud,这样eureka将会知道是在AWS云上。
如何解决Eureka注册服务慢的问题
使用配置项:
|
1
|
eureka.instance.leaseRenewalIntervalInSeconds
|
参考文档:
http://cloud.spring.io/spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service
原文:
|
1
2
3
|
Why is it so Slow to Register a Service?
Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick with the default because there are some computations internally in the server that make assumptions about the lease renewal period.
|
翻译:
|
1
|
作为实例还涉及到与注册中心的周期性心跳,默认持续时间为30秒(通过serviceUrl)。在实例、服务器、客户端都在本地缓存中具有相同的元数据之前,服务不可用于客户端发现(所以可能需要3次心跳)。你可以使用eureka.instance.leaseRenewalIntervalInSeconds 配置,这将加快客户端连接到其他服务的过程。在生产中,最好坚持使用默认值,因为在服务器内部有一些计算,他们对续约做出假设。
|
Eureka的自我保护模式
如果在Eureka Server的首页看到以下这段提示,则说明Eureka已经进入了保护模式。
|
1
|
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
|
保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。
详见:https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication
如何解决Eureka Server不踢出已关停的节点的问题
在开发过程中,我们常常希望Eureka Server能够迅速有效地踢出已关停的节点,但是新手由于Eureka自我保护模式,以及心跳周期长的原因,常常会遇到Eureka Server不踢出已关停的节点的问题。解决方法如下:
(1) Eureka Server端:配置关闭自我保护,并按需配置Eureka Server清理无效节点的时间间隔。
|
1
2
|
eureka.server.enable-self-preservation # 设为false,关闭自我保护
eureka.server.eviction-interval-timer-in-ms # 清理间隔(单位毫秒,默认是60*1000)
|
(2) Eureka Client端:配置开启健康检查,并按需配置续约更新时间和到期时间。
|
1
2
3
|
eureka.client.healthcheck.enabled # 开启健康检查(需要spring-boot-starter-actuator依赖)
eureka.instance.lease-renewal-interval-in-seconds # 续约更新时间间隔(默认30秒)
eureka.instance.lease-expiration-duration-in-seconds # 续约到期时间(默认90秒)
|
示例:
服务器端配置:
|
1
2
3
4
|
eureka:
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 4000
|
客户端配置:
|
1
2
3
4
5
6
7
|
eureka:
client:
healthcheck:
enabled: true
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
|
注意:
更改Eureka更新频率将打破服务器的自我保护功能,生产环境下不建议自定义这些配置。
详见:https://github.com/spring-cloud/spring-cloud-netflix/issues/373
自定义Eureka的Instance ID
在Spring Cloud中,服务的Instance ID的默认值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}} ,也就是机器主机名:应用名称:应用端口 。因此在Eureka Server首页中看到的服务的信息类似如下:itmuch:microservice-provider-user:8000 。如果想要自定义这部分的信息怎么办?
示例:
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
- instance:
- preferIpAddress: true
- instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 将Instance ID设置成IP:端口的形式
Eureka配置最佳实践参考
https://github.com/spring-cloud/spring-cloud-netflix/issues/203
注意点:eureka.client.healthcheck.enabled=true配置项必须设置在application.yml中
eureka.client.healthcheck.enabled=true 只应该在application.yml中设置。如果设置在bootstrap.yml中将会导致一些不良的副作用,例如在Eureka中注册的应用名称是UNKNOWN等。
SpringCloud——Eureka服务注册和发现的更多相关文章
- 将SpringCloud Eureka 服务注册与发现部署到docker
一.前言 最近在学习docker,顺便把之前学习的spring cloud 部署到Docker 中.至于什么是SpringCloud的服务注册与发现,什么是docker,我这里就不作赘述了.可以先去学 ...
- SpringCloud Eureka服务注册及发现——服务端/客户端/消费者搭建
Eureka 是 Netflix 出品的用于实现服务注册和发现的工具. Spring Cloud 集成了 Eureka,并提供了开箱即用的支持.其中, Eureka 又可细分为 Eureka Serv ...
- springcloud(第三篇)springcloud eureka 服务注册与发现 *****
http://blog.csdn.net/liaokailin/article/details/51314001 ******************************************* ...
- SpringCloud(3)---Eureka服务注册与发现
Eureka服务注册与发现 一.Eureka概述 1.Eureka特点 (1) Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. (2) Eureka 主管服务 ...
- SpringCloud 进阶之Eureka(服务注册和发现)
1. Eureka 服务注册与发现 Eureka 是一个基于REST的服务,用于服务的的注册与发现; Eureka采用C-S的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册 ...
- SpringCloud的入门学习之概念理解、Eureka服务注册与发现入门
1.微服务与微服务架构.微服务概念如下所示: 答:微服务强调的是服务的大小,它关注的是某一个点,是具体解决某一个问题.提供落地对应服务的一个服务应用,狭意的看,可以看作Eclipse里面的一个个微服务 ...
- SpringCloud - 2. 服务注册 和 发现
SpringCloud 的服务注册和发现是由Eureka来完成. 1.eureka server 1.1 依赖 <dependency> <groupId>org.spring ...
- Spring Cloud学习(一):Eureka服务注册与发现
1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...
- Eureka服务注册与发现
一.服务注册 注册Eureka的服务非常的简单,只需要引入spring-cloud-starter-netflix-eureka-client的jar包即可. <dependency> & ...
随机推荐
- ALGO-7_蓝桥杯_算法训练_逆序对
出处:http://blog.csdn.net/enjoying_science/article/details/44114035 (有难度,以后回来填坑) 阅读代码中: #include<st ...
- 学习笔记之.NET Core
source code https://github.com/haotang923/dotnet/tree/master/src .NET Documentation | Microsoft Docs ...
- Linux 期中架构 inotify
全网备份数据同步方案 备份网站内部人员信息 不能解决外部(人员)上传数据的备份 定时任务最短执行的周期为一分钟,采用定时任务方式,有时可能造成一分钟内的数据丢失 因此对于重要数据需要采用实时同步的方 ...
- CA双向认证的时候,如果一开始下载的证书就有问题的,怎么保证以后的交易没有问题?
研究HTTPS协议的时候,发现网站的CA认证,比如建行,比如支付宝,需要首先下载数字证书, 当然有些其他的双向认证,比如之前做过的港航和JP MORGAN进行交互的时候,证书是私下发送的,不需要去公网 ...
- 第3章 文件I/O(7)_高级文件操作:存储映射
8. 高级文件操作:存储映射 (1)概念: 存储映射是一个磁盘文件与存储空间的一个缓存相映射,对缓存数据的读写就相应的完成了文件的读写. (2)mmap和munmap函数 头文件 #include&l ...
- sqoop操作之ORACLE导入到HIVE
导入表的所有字段 sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \ --username SCOTT --passw ...
- python判断变量是否为int、字符串、列表、元组、字典等方法
在实际写程序中,经常要对变量类型进行判断,除了用type(变量)这种方法外,还可以用isinstance方法判断: #!/usr/bin/env pythona = 1b = [1,2,3,4]c = ...
- WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)------->>>任意文件上传检测POC
前言: Oracle官方发布了7月份的关键补丁更新CPU(Critical Patch Update),其中针对可造成远程代码执行的高危漏洞 CVE-2018-2894 进行修复: http://ww ...
- linux上安装vsftpd
介绍:在前几篇博客中博主介绍了,怎么用java语言搭建一个简单的网站.如果有些小伙伴想把自己做的网站发布到服务器上让别人访问的话,不妨可以关注博主的博客,博客会在接下来的几篇博客中介绍怎么把一个网站发 ...
- C关键字typedef及argc,argv,env参数含义
C关键字typedef--为C中各种数据类型定义别名. 在此插一点C知识 int main(int argc,const char *argv[],const char *envp[])主函数的红色部 ...