什么是Eureka
Eureka 是 Netflix 公司开源的产品,它是一种基于 REST(Representational State Transfer)的服务,主要用于 AWS 云。Eureka 提供了完整的 Service Registry 和 Service Discovery 实现,也是 Spring Cloud 体系中最重要最核心的组件之一。
简单来说 ,Eureka 就是 Netflix 开源的一款提供服务注册和发现的产品,并且提供了 Java 客户端。当然在 Spring Cloud 大力优化后的 Eureka,已经不仅仅只是用于 AWS 云,而是可以应用在任何需要使用注册中心的场景。
Eureka 由两个组件组成:Eureka 服务端和 Eureka 客户端。Eureka 服务端就是注册中心。Eureka 客户端是一个 java 客户端,用来简化与服务端的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
下面是Eureka的使用场景
从上面看Eureka Server担任注册中心的角色,提供了服务的发现和注册功能
Service Provider 服务提供者,将自身的服务注册到Eureka Server,同时通过心跳检查服务的运行状态
Service Consumer 服务调用者,从Eureka Server 得到注册的服务列表,找到对应的服务地址在调用并使用
SpringBoot2.x整合SpringCloud
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka- server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.在properties中自定义Springcloud的版本如下配置
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
4.在SpringBoot启动类上加上如下注解
@EnableEurekaServer
5.在application.yml中配置
spring:
application:
name: eureka server
server:
port: 9000
eureka:
client:
register-with-eureka: false
fetch-registry: false
测试
在浏览器中输入localhost:9000即可访问注册中心
可以发现后台页面被分为了五大部分
System Status 代表的系统状态
DS Replicas 该服务从哪里同步数据
Instances currently registered with Eureka 注册在Eureka的实例列表
General Info 系统运行环境 如cpu、内存等信息
Instance Info 本服务的基础信息 如ip地址 状态等
完整的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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.com.scitc</groupId>
<artifactId>springcloud_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud_01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
总结
搭建Eureka是非常简单的,并且Eureka是可用高可用的,比如可用集群,下一节将会将Eureka的集群。