一、Eureka 简介
Eureka 是 Netflix 开源的服务发现组件,本身是一个基于 REST 的服务。它包含 Server 和 Client 两部分。Spring Cloud 将它集成在子项目 Spring Cloud Netflix 中,从而实现微服务的注册与发现。
二、Eureka 原理
在分析 Eureka 的原理之前,先来看下架构图,如下图所示:
图中组件非常多,概念也比较抽象,博主在这里解释一下:
- Application Service 相当于服务提供者
- Application Client 相当于服务消费者
- Make Remote Call 可以理解为调用 RESTful API 的行为
- us-east-1c、us-east-1d 等都是 zone,它们都属于 us-east-1 这个 region
由架构图可知,Eureka 包含两个组件:Eureka Server 和 Eureka Client,它们作用如下:
- Eureka Server 提供服务发现能力,各个微服务启动时,会向 Eureka Server 注册自己的信息(例如 IP、端口、微服务名称等),Eureka Server 会存储这些信息。
- Eureka Client 是一个 Java 客户端,用于简化与 Eureka Server 的交互。
- 微服务启动后,会周期性(默认30秒)地向 Eureka Server 发送心跳以续约自己的“租期”。
- 如果 Eureka Server 在一定时间内没有接收到某个微服务实例的心跳,Eureka Server 将会注销该实例(默认90秒)。
- 默认情况下,Eureka Server 同时也是 Eureka Client。多个 Eureka Server 实例,互相之间通过复制的方式,来实现服务注册表中数据的同步。
- Eureka Client 会缓存服务注册表中的信息。使微服务无须每次请求都查询 Eureka Server,从而降低 Eureka Server 的压力;其次,即使 Eureka Server 所有节点都宕掉,服务消费者依然可以从缓存中的信息找到服务提供者并完成调用。
综上,Eureka 通过心跳检查、客户端缓存等机制,提高了系统的灵活性、可伸缩性和可用性。
三、创建服务注册中心(Eureka Server)
1、首先创建一个maven主工程,这个pom文件为父文件,起到依赖版本的作用,其他module工程继承该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>
<groupId>com.riemann</groupId>
<artifactId>microservice-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>microservice-eureka</name>
<description>Example project for Spring Cloud</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>microservice-eureka-server</module>
<module>microservice-provider-service-hi</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
2、然后创建两个moudle工程,一个moudle工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client。
Eureka Server
2.1、右键工程->创建module-> 选择spring initialir 如下图:
2.2、填好自己的项目名
2.3、选择cloud discovery->eureka server ,然后一直下一步就行了。
2.4、创建完后的工程,其pom.xml继承了父pom文件,并引入spring-cloud-starter-netflix-eureka-server的依赖,代码如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.riemann</groupId>
<artifactId>microservice-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.riemann</groupId>
<artifactId>microservice-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-eureka-server</name>
<description>Eureka Server</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2.5、启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
package com.riemann.microserviceeurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
}
}
2.6、eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: microservice-eureka-server
通过eureka.client.registerWithEureka:false
和fetchRegistry:false
来表明自己是一个eureka server
.
2.7、eureka server 是有界面的,启动工程,打开浏览器访问:http://localhost:8761 ,界面如下:
No application available 没有服务被发现 ……
因为没有注册服务当然不可能有服务被发现了。
四、创建一个服务提供者 (Eureka Client)
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
Eureka Client
其他跟 Eureka Server 类似,只有这个不同:
创建过程同server类似,创建完pom.xml如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.riemann</groupId>
<artifactId>microservice-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.riemann</groupId>
<artifactId>microservice-provider-service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microservice-provider-service-hi</name>
<description>Eureka Server</description>
<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-web</artifactId>
</dependency>
</dependencies>
</project>
通过注解@EnableEurekaClient 表明自己是一个eureka client.
package com.riemann.microserviceproviderservicehi;
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;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class MicroserviceProviderServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceProviderServiceHiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String index(@RequestParam(value = "name", defaultValue = "riemann") String name) {
return "hi " + name + " ,i am from port: " + port;
}
}
仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:
server:
port: 8762
spring:
application:
name: microservice-provider-service-hi
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动工程,打开 http://localhost:8761 ,即 eureka server 的网址:
你会发现一个服务已经注册在服务中了,服务名为microservice-provider-service-hi
,端口为8762
这时打开 http://localhost:8762/hi?name=riemann,你会在浏览器上看到 :
hi riemann,i am from port:8762