Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
Eureka组件
Eureka Server 和Eureka Client
Eureka Server
- Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
- Eureka Server本身也是一个服务,默认情况下会自动注册到Eureka注册中心。
如果搭建单机版的Eureka Server注册中心,则需要配置取消Eureka Server的自动注册逻辑。毕竟当前服务注册到当前服务代表的注册中心中是一个说不通的逻辑。 - Eureka Server通过Register、Get、Renew等接口提供服务的注册、发现和心跳检测等服务。
Eureka Client
- Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。
三大角色
- Eureka Server
- Service Provider:服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到。
- Service Consumer:服务消费方从Eureka获取注册服务列表,从而能够消费服务。
搭建单机版Eureka Server注册中心
步骤
1、搭建一个普通的maven项目
2、导入pom依赖:需要web依赖和eureka的server依赖;
3、application.yml配置
4、一个springboot的启动入口类:需要增加一个eureka的服务注解
5、启动,页面访问web页面
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<groupId>org.example</groupId>
<artifactId>Spring-Cloud-Eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml文件
spring:
application:
#设置spring应用命名,同命名的应用会注册到同一个服务集群中
name: cloud-eureka
server:
port: 8761
eureka:
client:
#是否将自己注册到Eureka-Server中
register-with-eureka: false
#是否在Eureka-Server中获取服务注册信息
fetch-registry: false
编写启动类
package com.wangpx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @program: Spring-Cloud-Eureka
* @description: 启动类
* @author: wangpx
* @create: 2020-01-18 18:05
*/
@SpringBootApplication
/**
* @EnableEurekaServer - 启动时初始化Eureka Server注册中心。
*/
@EnableEurekaServer
public class CloudEurekaApp {
public static void main(String[] args) {
SpringApplication.run(CloudEurekaApp.class,args);
}
}
访问Eureka Server Web 服务管理平台
- http://localhost:8761
Eureka Server管理平台介绍
- System Satus 系统状态展示
- DS Replicas 注册中心集群列表
- Instances currently registered with Eureka 已在注册中心注册的服务列表
- General Info 当前注册中心相关信息展示
- Instance Info 当前注册中心实例信息展示