转载

Spring Cloud 学习---Eureka服务注册与发现

在上一文: 3W法(what,why,how)入门 Spring Cloud 学习后,对 Spring Cloud 有了基本的认识,本文主要学习 Spring Cloud Netflix 之 Eureka ,还是老套路---3W法(what,why,how)!

本次学习最终实现 效果

Spring Cloud 学习---Eureka服务注册与发现

版本信息

spring.io/projects/sp…

  • Spring Boot 版本:2.1.11.RELEASE
  • Spring Cloud 版本:Greenwich.SR4

What --- 定义

来自官网的定义:

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.
Eureka是一种基于REST(具象状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移。

通俗说:

Eureka 是 Netflix 开源的一款提供服务注册和发现的产品,它提供了完整的 Service Registry 和 Service Discovery 实现。也是 Spring Cloud 体系中最重要最核心的组件之一。

架构图[来自 官网 ]

Spring Cloud 学习---Eureka服务注册与发现

上图描述了 Eureka 的架构,主要由3个角色组成:

1、Eureka Server

  • 提供服务注册和发现

2、Eureka Client (Application Service)

  • 服务提供方
  • 将自身服务注册到Eureka,从而使服务消费方能够找到

3、Eureka Client (Application Client)

  • 服务消费方
  • 从Eureka获取注册服务列表,从而能够消费服务

WHY --- 特点

In AWS cloud, because of its inherent nature, servers come and go. Unlike the traditional load balancers which work with servers with well known IP addresses and host names, in AWS, load balancing requires much more sophistication in registering and de-registering servers with load balancer on the fly. Since AWS does not yet provide a middle tier load balancer, Eureka fills a big gap in the area of mid-tier load balancing.

在AWS云计算中,由于其固有的特性,服务器来来去去。与使用众所周知的IP地址和主机名的服务器的传统负载平衡器不同,在AWS中,负载平衡器需要更加复杂地动态注册和注销负载平衡器服务器。由于AWS还没有提供中间层负载平衡器,Eureka填补了中间层负载平衡领域的一个巨大空白。

Eureka Server:注册中心服务端

注册中心服务端主要对外提供了三个功能:

服务注册服务提供者启动时,会通过 Eureka Client 向 Eureka Server 注册信息,Eureka Server 会存储该服务的信息,Eureka Server 内部有二层缓存机制来维护整个注册表

提供注册表服务消费者在调用服务时,如果 Eureka Client 没有缓存注册表的话,会从 Eureka Server 获取最新的注册表

同步状态Eureka Client 通过注册、心跳机制和 Eureka Server 同步当前客户端的状态。

Eureka Client:注册中心客户端

Eureka Client 是一个 Java 客户端,用于简化与 Eureka Server 的交互。Eureka Client 会拉取、更新和缓存 Eureka Server 中的信息。因此当所有的 Eureka Server 节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者,但是当服务有更改的时候会出现信息不一致。

Register: 服务注册服务的提供者,将自身注册到注册中心,服务提供者也是一个 Eureka Client。当 Eureka Client 向 Eureka Server 注册时,它提供自身的元数据,比如 IP 地址、端口,运行状况指示符 URL,主页等。

Renew: 服务续约Eureka Client 会每隔 30 秒发送一次心跳来续约。 通过续约来告知 Eureka Server 该 Eureka Client 运行正常,没有出现问题。 默认情况下,如果 Eureka Server 在 90 秒内没有收到 Eureka Client 的续约,Server 端会将实例从其注册表中删除,此时间可配置,一般情况不建议更改。

自我保护机制

默认情况下,如果 Eureka Server 在一定的 90s 内没有接收到某个微服务实例的心跳,会注销该实例。但是在微服务架构下服务之间通常都是跨进程调用,网络通信往往会面临着各种问题,比如微服务状态正常,网络分区故障,导致此实例被注销。

Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 即会进入自我保护机制。

Eureka Server 进入自我保护机制,会出现以下几种情况:

  • Eureka 不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
  • Eureka 仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用)
  • 当网络稳定时,当前实例新的注册信息会被同步到其它节点中

Eureka 自我保护机制是为了防止误杀服务而提供的一个机制。当个别客户端出现心跳失联时,则认为是客户端的问题,剔除掉客户端;当 Eureka 捕获到大量的心跳失败时,则认为可能是网络问题,进入自我保护机制;当客户端心跳恢复时,Eureka 会自动退出自我保护机制。

如果在保护期内刚好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,即会调用失败。对于这个问题需要服务消费者端要有一些容错机制,如重试,断路器等。

通过在 Eureka Server 配置如下参数,开启或者关闭保护机制,生产环境建议打开:

eureka.server.enable-self-preservation=true
复制代码

HOW --- 使用

Eureka Server

1、pom中添加依赖

参见: pom.xml

2、启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
复制代码

3、配置文件

server:
  port: 8888
  
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#eureka.client.registerWithEureka 表示是否将自己注册到Eureka Server, 默认为true。 由于当前应用就是Eureka Server, 因此设为 false;
#eureka.client.fetchRegistry 表示是否从Eureka Server获取注册信息,默认为true。 如果这是一个单点的 Eureka Server,不需要同步其他节点的数据,可以设为false。
复制代码

Eureka Client

1、pom中添加依赖

参见: pom.xml

2、启动类

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientProviderApplication.class, args);
    }
}
复制代码

3、配置文件

server:
  port: 8880

spring:
  application:
    name: eureka-client-provider

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
复制代码

Eureka Server 开启密码

1、添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码

2、添加配置信息

spring:  
	application:    
		name: eureka-server  
	security:    
		user:      
			name: tyron      
			password: 123456
复制代码

3、服务端开启basic认证

package com.tyron.eureka.server.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

/**
 * @Description: security开启basic认证
 * @Author: tyron
 * @Date: Created in 2019/12/19
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * @Description: 高版本的丢弃了
     * security:
     *   basic:
     *     enabled: true 配置,应该使用以下方式开启
     * @Param: [http]
     * @Return: void
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
        // 如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}
复制代码

4、客户端注册地址修改

eureka:
  client:
    serviceUrl:
      defaultZone: http://tyron:123456@localhost:8888/eureka/
      #形如:http://${eureka.instance.hostname}:${server.port}/eureka/

复制代码

参考

  • GitHub--eureka
  • Spring Cloud(二):注册中心Eureka
  • Spring Cloud Eureka 服务注册中心(二)
  • Eureka工作原理
  • tyronczt/Spring-Cloud-Learning
原文  https://juejin.im/post/5e13457e6fb9a0481f0a2b7a
正文到此结束
Loading...