SpringCloud(2) 服务注册和发现Eureka Server
一、简介
EureKa在Spring Cloud全家桶中担任着服务的注册与发现的落地实现。Netflix在设计EureKa时遵循着AP原则,它基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,功能类似于Dubbo的注册中心Zookeeper。
官方文档:http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#spring-cloud-eureka-server
二、实现原理

EureKa采用C-S的设计架构,即包括了Eureka Server(服务端),EureKa client(客户端)。
1.EureKa Server 提供服务注册,各个节点启动后,在EureKa server中进行注册;
2 EureKa Client 是一个Java客户端,用于和服务端进行交互,同时客户端也是一个内置的默认使用轮询负载均衡算法的负载均衡器。在应用启动后,会向Eueka Server发送心跳(默认30秒)。如果EureKa Server在多个心跳周期内没有接受到某个节点的心跳,EureKa Server将会从服务注册表中将这个服务移出(默认90秒)。
三、SpringCloud Eureka的使用步骤
1、Eureka Server端(服务端)
1.1 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2 application.yml
server:
port: 8761 eureka:
instance:
#单机hostname: localhost
hostname: localhost
client:
registerWithEureka: false #false表示不向注册中心注册自己
fetchRegistry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
serviceUrl:
#Eureka高复用时设置其他的Eureka之间通信
#defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7004.com:7004/eureka/
#单机设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1.3 程序类主类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
} }
添加注解 @EnableEurekaServer
1.4 观察结果 http://127.0.0.1:8761/

2、Eureka Client端
使用eureka客户端 官方文档:http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#netflix-eureka-client-starter
2.1 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2 application.yml
server:
port: 8771 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ spring:
application:
name: product-service
2.3 架构及程序
2.3.1 项目架构:

2.3.2 主程序类
@SpringBootApplication
public class ProductServiceApplication { public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
} }
2.3.3 controller
@RestController
@RequestMapping("/api/v1/product")
public class ProductController { @Value("${server.port}")
private String port; @Autowired
private ProductService productService; /**
* 获取所有商品列表
* @return
*/
@RequestMapping("list")
public Object list(){
return productService.listProduct();
} /**
* 根据id查找商品详情
* @param id
* @return
*/
@RequestMapping("find")
public Object findById(@RequestParam("id") int id){
Product product = productService.findById(id);
Product result = new Product();
BeanUtils.copyProperties(product, result);
result.setName(result.getName() + " data from port=" + port);
return result;
} }
2.3.4 domain
public class Product implements Serializable {
public Product(){}
public Product(int id, String name, int price, int store){
this.id = id;
this.name = name;
this.price = price;
this.store = store;
}
private int id;
/**
* 商品名称
*/
private String name;
/**
* 价格
*/
private int price;
/**
* 库存
*/
private int store;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStore() {
return store;
}
public void setStore(int store) {
this.store = store;
}
}
2.3.5 service
public interface ProductService {
List<Product> listProduct();
Product findById(int id);
}
@Service
public class ProductServiceImpl implements ProductService { private static final Map<Integer, Product> daoMap = new HashMap<Integer, Product>(); static {
Product p1 = new Product(1, "iphone1", 1999, 10);
Product p2 = new Product(2, "iphone2", 2999, 10);
Product p3 = new Product(3, "iphone3", 3999, 109);
Product p4 = new Product(4, "iphone4", 4999, 190);
Product p5 = new Product(5, "iphone5", 5999, 210);
Product p6 = new Product(6, "iphone6", 6999, 120);
Product p7 = new Product(7, "iphone7", 7999, 10); daoMap.put(p1.getId(), p1);
daoMap.put(p2.getId(), p2);
daoMap.put(p3.getId(), p3);
daoMap.put(p4.getId(), p4);
daoMap.put(p5.getId(), p5);
daoMap.put(p6.getId(), p6);
daoMap.put(p7.getId(), p7); } @Override
public List<Product> listProduct() {
Collection<Product> collection = daoMap.values();
List<Product> list = new ArrayList<>(collection);
return list;
} @Override
public Product findById(int id) {
return daoMap.get(id);
}
}
2.4 开启多个Eureka Client


注:eureka管理后台出现一串红色字体:是警告,说明有服务上线率低
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:
enable-self-preservation: false
默认情况下,如果EurekaServer在一 定时间内没有接收到某 个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了,因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过 “自我保护模式”来解决这个问题一当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进 入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。综上,自我保护模式是一 种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留) ,也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false禁用自我保护模式。在生产环境中,自我保护模式一般不会关闭,且默认是开启状态true。
SpringCloud(2) 服务注册和发现Eureka Server的更多相关文章
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战
笔记 5.服务注册和发现Eureka Server搭建实战 简介:使用IDEA搭建Eureka服务中心Server端并启动,项目基本骨架介绍 官方文档:http://clou ...
- SpringCloud之服务注册与发现Eureka(一)
Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来.Eureka包含了服务器端和客户端组件.服务器端,也被称作是服务注册 ...
- SpringCloud(二) 服务注册与发现Eureka
1.eureka是干什么的? 上篇说了,微服务之间需要互相之间通信,那么通信就需要各种网络信息,我们可以通过使用硬编码的方式来进行通信,但是这种方式显然不合适,不可能说一个微服务的地址发生变动,那么整 ...
- SpringCloud之服务注册与发现Eureka+客户端Feign
前言 SpringCloud 是微服务中的翘楚,最佳的落地方案. Eureka 作为注册中心,是 SpringCloud 体系中最重要最核心的组件之一. Feign 使用接口加注解的方式调用服务,配合 ...
- Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
Spring Cloud(二):服务注册与发现 Eureka[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 上一篇主要介绍了相关理论,这一篇开始我们 ...
- SpringCloud - 2. 服务注册 和 发现
SpringCloud 的服务注册和发现是由Eureka来完成. 1.eureka server 1.1 依赖 <dependency> <groupId>org.spring ...
- spring cloud 学习之 服务注册和发现(Eureka)
一:服务注册和发现(Eureka) 1:采用Eureka作为服务注册和发现组件 2:Eureka 项目中 主要在启动类加上 注解@EnableEurekaServer @SpringBootAppli ...
- SpringCloud:(一)服务注册与发现
最近跟着方志明老师学习SpringCloud,博客地址如下: https://blog.csdn.net/forezp/article/details/81040925 自己也跟着撸了一遍,纸上得来终 ...
- springcloud之服务注册与发现(Eureka)
springcloud服务注册与发现 使用Eureka实现服务治理 作用:实现服务治理(服务注册与发现) 简介: Spring Cloud Eureka是Spring Cloud Netflix项目下 ...
随机推荐
- wpa_cli 关联无线网络
fq关联无线网络,不同的无线网络认证方式不同设置: 1)open(开放式认证方式,分为): wpa_cli -iwlan0 set_network 0 ssid '"w ...
- vue 格式化银行卡(信用卡)每4位一个符号隔断
问题 在做银行卡输入框时有一个需求如题,这里举例用-隔断 调查 查看了很多大公司网站的银行卡输入,发现还有有很多缺陷的: 有的是在中间删除,光标会跳到最后: 有的是能删除掉中间隔断符的: 等等,逻辑感 ...
- 如何给网站添加IE浏览器升级提示
1.在代码编辑器中(如Notepad++)打开网站头部模板 2.使用<!––[if IE]>语句添加升级提示,如: 判断是否IE(包含使用IE内核的浏览器) <!––[if IE]& ...
- 推荐一些iOS博客
公司性质的: 公司 地址 美团 http://tech.meituan.com/archives 个人博客: 博主 地址 (斜体的技术文章较少) 王巍(onevcat) https://onevcat ...
- Java之hashCode的作用和equals方法的重构规则
这个是博主对hashcode的初步理解,以后加深了会再来更新: 1.hashcode是什么? hashcode是对象的散列码,不同的对象几乎不一样,说几乎是因为还是可以一样的. 特点:每一个对象都有h ...
- HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
今天整合ssm框架 时 遇到的问题 困扰我好长时间 原因就是 mapper文件 没有被加载进来 但是 为什么没有被加载进来呢 因为中间的配置文件出了一些问题 网上大多数说法是 在pom ...
- yum出现Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile解决方法
yum出现Could not retrieve mirrorlist解决方法 Loaded plugins: fastestmirror, securityLoading mirror speeds ...
- Golang Go Go Go part2:变量及常量声明
三.关键字及内置预声明常量.类型.函数 1.关键字 Go有25个关键字,只能用在语法允许的地方,不能作为名称使用,它们是: break default func ...
- MySQL 数据库字段类型使用说明
简介 MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型. 数值类型 下列用于描述的代码字母中: M表示最大的显示尺寸.最大的合法的显示尺寸是 255 .(注 ...
- [Swift]LeetCode283. 移动零 | Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...