(一)Eureka 服务的注册与发现
(一)服务的注册于发现(eureka);
Eureka Server: 服务注册中心,负责服务列表的注册、维护和查询等功能
在Idea里,新建项目,选择Spring initializer.

下面的pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置properties文件参数;
server.port=8882 #域名
eureka.instance.hostname=localhost #禁用 Eureka的客户端注册行为
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false #eureka注册中心服务地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
在启动类上添加注解@EnableEurekaServer
// @EnableEurekaServer 代表启动注册服务中心
@EnableEurekaServer
@SpringBootApplication
public class SpringCloundEurekaDemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloundEurekaDemoApplication.class, args);
}
}
启动项目,打开连接 http://localhost:8882

二.创建一个服务提供者(Eureka-client)
服务提供方,同时也是一个Eureka Client,负责将所提供的服务向Eureka Server进行注册、续约和注销等操作。注册时所提供的主要数据包括服务名、机器ip、端口号、域名等,从而能够使服务消费方能够找到
Eureka服务器我们已经编写好了,接下来我们就可以编写一个Eureka的客户端了。这个客户端可能是一个服务提供者,也可能是一个服务消费者,甚至两者都是。
我们先编写一个简单的Eureka Client
在Idea里,新建项目,选择Spring initializ.

下面的pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置yml文件参数;(换下配置方式)
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8882/eureka/
server:
port: 8883
spring:
application:
name: service-hello
在启动类上添加注解@EnableEurekaClient
// @ EnableEurekaClient 表示申明自己是一个发服务提供者;
@EnableEurekaClient
@SpringBootApplication
public class SpringCloundEurekaClientExampleApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloundEurekaClientExampleApplication.class, args);
}
}
创建conroller
@RestController
public class HelloController {
@Value("${server.port}")
String port; @Value("${spring.application.name}")
String name; @RequestMapping("/index")
public String index() {
return "服务提供者client:" + name + "服务端口:" + port;
}
}
启动项目
然后我们再来看一下服务注册中心;就会看到,已经注册了一个服务提供者;

为了接下来测试Ribbon负载
我们将client的配置文件属性注册的端口改为8884;;然后在IDEA把该服务再启动一个实例
(把single instance only勾选去掉)

回到服务中心,已经有两个client了

此外
Spring Cloud实现心跳监测,在服务注册和停止时,注册中心能得到通知,并更新服务实例列表
(一)Spring Cloud注册中心添加配置:
eureka.server.enable-self-preservation=false
eureka.server.enable-self-preservation
默认情况下,如果Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。
Eureka通过“自我保护模式”来解决这个问题——当Eureka Server节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,Eureka Server就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
eureka.server.eviction-interval-timer-in-ms
eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
(二)Spring Cloud服务提供者添加配置:
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=5
eureka.client.registry-fetch-interval-seconds
表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒
eureka.instance.lease-expiration-duration-in-seconds
leaseExpirationDurationInSeconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
- 默认为90秒
- 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
- 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
- 该值至少应该大于leaseRenewalIntervalInSeconds
eureka.instance.lease-renewal-interval-in-seconds
leaseRenewalIntervalInSeconds,表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。
- 默认30秒
所属文章参考:https://www.jianshu.com/u/8f959a9cbc66。https://blog.csdn.net/qq_41377914/article/category/7719803
(一)Eureka 服务的注册与发现的更多相关文章
- SpringCloud初体验:一、Eureka 服务的注册与发现
Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...
- Eureka 服务的注册和发现
二.Eureka 服务端 1.新建一个 maven module 子项目 microservicecloud-eureka-server 2.pom.xml <project xmlns=&qu ...
- spring cloud 系列第1篇 —— eureka 服务的注册与发现 (F版本)
源码仓库地址:https://github.com/heibaiying/spring-samples-for-all 一.eureka 简介 Spring Cloud Eureka使用Netflix ...
- 玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)
一.服务的注册与发现(Eureka) spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等 ...
- SpringCloud-微服务的注册与发现Eureka(二)
一.SpringCloud简介 Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- (转) 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...
- 服务的注册与发现Eureka(二)
1.服务治理概念 在传统rpc远程调用中,服务与服务依赖关系,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用.负载均衡.容错等,实现服务发现与注册. 2.服务的注册与 ...
- springCloud学习-服务的注册与发现(Eureka)
1.小记 这段时间有空,把springcloud的知识整理一下,好记性不如烂笔头,也让自己对springcloud有个清晰的认识.此次的整理记录主要借鉴了这位大佬的博客 https://blog.cs ...
- C5. Spring 服务的注册与发现(Spring Cloud Eureka)
[概述] Eureka 作为 Spring Cloud 分布式解决方案中重要的一环,实现了服务的注册与发现等功能.Eureka 包括 Eureka Server 和 Eureka Client,具体的 ...
随机推荐
- 杂项-DB:ETL(数据库仓库技术)
ylbtech-杂项-DB:ETL(数据库仓库技术) ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).交互转换(transfor ...
- Redis学习笔记(九) 命令进阶:Pub/Sub(发布/订阅)操作
原文链接:http://doc.redisfans.com/pub_sub/index.html Redis的Pub/Sub模型可以应对工作中的一些简单应用,涉及到复杂应用还是推荐使用诸如Rabbit ...
- Spark RDD概念学习系列之什么是Pair RDD
不多说,直接上干货! 什么是Pair RDD (1)包含键值对类型的RDD被称作Pair RDD. (2)Pair RDD通常用来进行聚合计算. (3)Pair RDD通常由普通RDD做ETL转换而来 ...
- layer-list
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...
- php基础------将二维数组转三维数组
将二维数组转为三维数组 /** * 二维数组转三维数组(指定键为三维数组的键名) * @param [type] $arr [要排序的数组] * @param [type] $key [指定的键] * ...
- webpack——打包JS
1.在文件中打开命令行,输入code ./ (我的编译器是vs code) 2.然后会弹出编译器,在编译器内新建js文件app,sum app.js import sum from './sum ...
- jq点击按钮打开和关闭弹出层,点击除了当前按钮以外的地方关闭弹出层
1.html <a id="more" onclick="moreFun()">更多</a> <ul id="moreL ...
- Vue学习之路第七篇:跑马灯项目实现
前面六篇讲解了Vue的一些基础知识,正所谓:学以致用,今天我们将用前六篇的基础知识,来实现类似跑马灯的项目. 学前准备: 需要掌握定时器的两个函数:setInterval和clearInterval以 ...
- POJ 3517 And Then There Was One( 约瑟夫环模板 )
链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...
- BZOJ 1176/2683 Mokia (三维偏序CDQ+树状数组)
题目大意: 洛谷传送门 三维偏序裸题.. 每次操作都看成一个三元组$<x,y,t>$,表示$x,y$坐标和操作时间$t $ 询问操作拆成$4$个容斥 接下来就是$CDQ$了,外层按t排序, ...