服务注册与发现——Eureka
微服务框架
微服务是一种架构模式,而Spring Cloud是实现微服务架构模式的一种开源框架。除了使用Spring Cloud来实现微服务外,还有其他选择。
- .Net: surging
- Java:Spring Cloud、Dubbo
Eureka 简介
Eureka是Spring Cloud框架中的其中一个组件,主要负责完成微服务架构中的服务治理功能,他是在Netflix Eureka的基础上进行了二次封装。
Eureka由两部分组成:
- Eureka Server: 提供服务注册的服务端
- Eureka Client: 用来简化与服务端的交互,作为轮询的负载均衡器,并提供故障时的切换支持
所有服务需要通过Eureka Client注册进Eureka Server中,才能被发现。当Eureka Client成功被注册后,Eureka Server会向Eureka Client发起心跳检测,若发现其中某一个Client没有回应,即会认为该客户端出现异常,并释放它的连接。
实战
创建工程
首先创建一个maven项目springcloud
创建 Eureka Server
在springcloud中新建Module
添加Module信息
依赖选择Cloud Discovery->Eureka Server
Spring Boot 版本为2.1.1
建好后,结构如下
POM
生成的pom如下,从pom中,可以看出当前所使用的相关组件版本号为:
- spring boot: 2.1.1.RELEASE
- spring cloud: Greenwich.RC1
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ali.springcloud.eureka</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description></description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC1</spring-cloud.version>
</properties>
<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-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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
修改启动类
修改启动类ServerApplication,添加@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
修改配置文件
将默认的application.properties修改application.yml,并添加配置信息如下:
Eureka Server 自己也是一个Client
eureka:
client:
# 由于 Eureka Server 自身也是一个Client, 设置为false禁止其客户端注册行为
register-with-eureka: false
# 由于 Eureka Serve 的职责就是维护服务实例, 它并不需要去检索服务,所以也设置为false
fetch-registry: false
运行ServerApplication启动服务,并访问localhost:8080,可以看到如下的Eureka的注册中心界面。其中可以看到Application下面没有注册的应用,服务注册地址为默认的localhost:8761
现在我们修改一下配置,将该服务作为客户端注册到自己里面,并将注册地址改为localhost:8080
application.yml 修改为:
eureka:
client:
# 由于 Eureka Server 自身也是一个Client, 设置为false禁止其客户端注册行为
register-with-eureka: true
# 由于 Eureka Serve 的职责就是维护服务实例, 它并不需要去检索服务,所以也设置为false
fetch-registry: false
service-url:
defaultZone: http://localhost:8080/eureka/
重启服务后再次访问localhost:8080, 可以看到Application下面多了一个UNKNOWN的应用,并且注册地址由默认端口8761变更为了8080。
再次修改application.yml,尝试给应用进行命名
eureka:
client:
# 由于 Eureka Server 自身也是一个Client, 设置为false禁止其客户端注册行为
register-with-eureka: true
# 由于 Eureka Serve 的职责就是维护服务实例, 它并不需要去检索服务,所以也设置为false
fetch-registry: false
service-url:
defaultZone: http://localhost:8080/eureka/
instance:
appname: euraka-server
好了,Eureka Server部署完成。再次将配置文件修改,因为8080端口容易被占用,我们将其修改为默认端口8761,并重启服务。
eureka:
client:
# 由于 Eureka Server 自身也是一个Client, 设置为false禁止其客户端注册行为
register-with-eureka: false
# 由于 Eureka Serve 的职责就是维护服务实例, 它并不需要去检索服务,所以也设置为false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
appname: euraka-server
server:
port: 8761
注意,这时服务的访问地址将改为localhost:8761。
下面开始创建Eureka Client,并将其在该注册中心中注册。
创建 Eureka Client
同样,同样的步骤新建一个名为client的module,唯一不同的是,依赖选Cloud Discovery->Eureka Discovery
建好后,项目结构如下:
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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ali.springcloud.eurka</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>client</name>
<description></description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
修改启动类
为启动类,添加@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
修改配置文件
修改默认的application.properties 为application.yml,并添加以下内容后启动客户端应用:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
fetch-registry: false
instance:
hostname: localhost
spring:
application:
name: service-hello
server:
port: 8081
会发现客户端启动不起来,需要在pom中添加web的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
再次尝试启动客户端,发现没有问题。这时访问localhost:8761,会看到,我们新建的应用service-hello,已被成功注册。
上面出现红色的警告,客户端进行了多次重启,因为Server会定时对客户端进行心跳检测,来统计客户端的上线率,当客户端的上线率低于了某个阈值,就会出现该警告,提醒客户端上线率太低。但是Server并不会释放该应用连接,仍会保持该应用。这其实是它的一种保护机制。
由于开发环境,我们会经常重启客户端来进行调试,所以可以通过配置的方式来关闭这种自我保护机制。
开发环境下关闭服务端的自我保护机制
需要修改server端的配置文件:
eureka:
client:
# 由于 Eureka Server 自身也是一个Client, 设置为false禁止其客户端注册行为
register-with-eureka: false
# 由于 Eureka Serve 的职责就是维护服务实例, 它并不需要去检索服务,所以也设置为false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
appname: euraka-server
server:
# 关闭服务端自我保护机制
enable-self-preservation: false
server:
port: 8761
注意,该配置只能在开发环境下设置。
设置好后,再次重启服务,并访问,可以看到提示信息发生改变,提示你自我保护机制被关闭。