前言
随着互联网技术的发展,系统用户量的增长,很多系统都采用了分布式的方式进行部署。这个固然大大提高了系统的性能和可用性,但是分布式部署导致各种服务数量大增,这给我们进行服务治理和运维带来了困扰。
一、Eureka是什么?
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
二、Eureka是干什么的?
Eureka分为Eureka客户端和Eureka服务端,Eureka客户端提供服务注册与注销、服务发现的功能,Eureka服务端提供服务治理的功能。
三、Eureka原理解析
1.服务治理流程

流程说明:
(1)服务提供者集成Eureka客户端,启动时,Eureka客户端发送注册请求到Eureka服务端;
(2)Eureka客户端每30秒发送一次续约请求到Eureka服务端;
(3)若Eureka服务端超过90秒未收到服务提供者的续约请求,则Eureka服务端会将其从服务列表中踢除;
(4)服务消费者集成Eureka客户端,启动时,Eureka客户端同样会发送注册请求到Eureka服务端;
(5)服务消费者中,Eureka客户端每隔30秒,会从Eureka服务端获取服务实例列表,其中,首次是全量查询,后续为增量查询;获取到实例列表后,Eureka客户端会将其写入到本地缓存中;
(6)服务消费者调用服务提供者时,Eureka会总本地缓存获取对应的实例,并进行接口调用。
2.Eureka高可用架构

Eureka支持多机房部署的高可用架构,其中各个机房的Eureka客户端会请求本机房的Eureka服务端,而各机房的Eureka服务端则会将服务实例信息进行互相同步,保证各个Eureka服务端的实例信息都是最新的。
3.Eureka服务端缓存架构

为避免读写冲突,Eureka采用多层缓存的架构。
服务注册时,Eureka服务端会将服务实例更新到注册实例列表缓存(register)和读写缓存(readWriteCacheMap)中,然后Eureka服务端每隔30秒会将读写缓存(readWriteCacheMap)的数据更新到只读缓存(readOnlyCacheMap)中。
消费者从Eureka服务端获取实例列表时,是直接从只读缓存(readOnlyCacheMap)中获取。
4.Eureka健康检查
5.Eureka分区
Eureka的分区概念分为区域(region)和机房(zone)。如上图,区域(region)是北京,机房分为zone-1和zone-2。消费者(Consumer-1)调用服务时,会优先调用zone-1里面的服务提供者(Service-1),只有zone-1里面的服务提供者(Service-1)不可用,才会去调用zone-2里面的服务提供者(Service-2)。
四、Eureka实战
1.创建Eureka服务端
(1)创建名为eureka-server的maven工程
(2)引入Eureka-server依赖
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0
modelVersion> <parent> <groupId>com.hxq
groupId> <artifactId>spring-cloud-hxq
artifactId> <version>0.0.1-SNAPSHOT
version>
parent> <artifactId>eureka-server
artifactId> <name>eureka-server
name> <properties> <project.build.sourceEncoding>UTF-8
project.build.sourceEncoding>
properties> <dependencies> <dependency> <groupId>org.springframework.cloud
groupId> <artifactId>spring-cloud-starter-netflix-eureka-server
artifactId>
dependency>
dependencies>
project>
Eureka-server依赖:spring-cloud-starter-netflix-eureka-server。
(3)创建启动类
package com.hxq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; / * 注册中心 * @author Administrator * */ @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args); } }
启动类要加上@EnableEurekaServer注解。
(4)创建application.yml配置文件
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/ spring: application: name: eurka-server
2.创建Eureka客户端
(1)创建名为service-hi的maven工程
(2)引入Eureka-client依赖
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0
modelVersion> <parent> <groupId>com.hxq
groupId> <artifactId>spring-cloud-hxq
artifactId> <version>0.0.1-SNAPSHOT
version>
parent> <artifactId>service-hi
artifactId> <name>service-hi
name> <dependencies> <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-client
artifactId>
dependency>
dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot
groupId> <artifactId>spring-boot-maven-plugin
artifactId>
plugin>
plugins>
build>
project>
(3)创建启动类
package com.hxq; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; / * 服务提供者 * * @author Administrator * */ @SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args); } / * 服务端口 */ @Value("${server.port}") private String port; @RequestMapping("/hi") public String home(@RequestParam(value = "name", defaultValue = "hxq") String name) {
return "hi " + name + " ,i am from port:" + port; } }
启动类要加上@EnableEurekaClient注解。
(4)创建application.yml配置文件
server: port: 9100 spring: application: name: service-hi eureka: serviceUrl: defaultZone: http://localhost:8761/eureka/
3.服务验证
(1)启动eureka-server服务

(2)启动service-hi服务

(3)打开Eureka服务列表
五、微服务精通系列文章
- 微服务精通之Eureka原理解析
- 微服务精通之Ribbon原理解析
- 微服务精通之Hystrix原理解析
- 微服务精通之Feign原理解析
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178675.html原文链接:https://javaforall.net
