SpringCloud (三) Eureka 注册中心

Vector illustration of a Archimedes in bath. Thumbs up eureka. ancient greek mathematician, physicist.  stock illustration

写在前面

版本说明

1、SpringCloud 版本:Hoxton.SR6

2、SpringBoot 版本:2.3.1.RELEASE

Eureka 停止维护

Eureka 是 Spring Cloud Netflix 下面的一个子组件,在新版本的 Spring Cloud 中官方已经宣布有多个组件停止维护,其中就包括 Eureka。

既然停更了为什么还有学习 Eureka?

Eureka 是一款成熟的产品,在很多在线上大量使用,虽然停止维护了,并不表示不能使用。Eureka 作为一款成熟的注册中心与 Spring Cloud 完美的整合,后来的注册中心都借鉴了 Eureka 的设计思想。综合以上观点,学习了解 Eureka 还是很有必要的,当然也有新的替代产品出现了,毕竟在新的 Spring Cloud 项目中已经不推荐使用 Eureka 作为注册中心了。

Eureka 注册中心简介

spring-cloud-netflix-eureka 官网

Eureka 注册中心现在已经被SpringCloud停止维护了,但是 Eureka 的设计思想是值得学习的,下面通过几句话对 Eureka 有一个认识。

  • Eureka 是 Spring Cloud Netflix 核心组件之一,基于CAP理论中的 AP 思想设计,会牺牲数据的一致性,从而保证可用性

    说明:这里说的一致性是强一致性,指的是某一个节点数据的改变会被其他节点感知,用户访问任意节点的到的结果是一致的。

    Eureka 重点关注服务的可用性,但也会保证数据的最终一致性。例如12306网站的抢票服务,优先保证的买票服务是可用的,但是最终还是要保证不能一张票不能被多人所有

  • Eureka 基于 C/S 架构,提供两个组件 Eureka Server 和 Eureka Client,Eureka Server 提供服务注册,Eureka Client 是一个Java客户端,需要接入微服务中,启动后注册到注册中心,纳入服务管理

  • Eureka 没有区分中心节点和从节点,集群中每个节点地位相等,节点之间通过相互注册和相互守望,保证集群可用性

  • 如果某个服务实例在一定时间范围内没有心跳了,Eureka 会将其移除;但是如果短时间内丢失大量服务实例 Eureka 会认为发生网络故障,并不会移除这些实例,这就是 自我保护机制

image

Eureka 注册中心单机版

本项目的搭建基于前面 SpringCloud (一) 微服务入门 这篇博客

单机版 Eureka 服务端搭建

创建项目 cloud-eureka-server-8761

修改 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gorun</groupId>
        <artifactId>cloud2020</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-eureka-server-8761</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

修改 application.yml 文件

server:
  port: 8761 # 自己的服务端口号

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在启动类上加入注解 @EnableEurekaServer

测试

启动项目,访问 http://localhost:8761/

image-20200726132328432

搭建Eureka 注册中心客户端

创建服务提供者项目,项目名:cloud-eureka-provider-8001

image-20200614092211105

POM 核心依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

添加 application.yml 配置

server:
  port: 8001  #端口

spring:
  application:
    name: provider-service #服务名称

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/ #注册中心地址

启动类上加入注解 @EnableDiscoveryClient

image-20200614093434026

测试

先不着急写业务代码,启动项目,刷新 http://localhost:8761/ 地址,找到我们注册的微服务

image-20200614093806381

编写业务代码,提供对外接口

package com.runningboy.springcloud.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author Running boy
 * @Description: Provider Controller
 * @date 2020/6/14 9:39
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @RequestMapping("/info")
    public String info(@RequestParam("msg") String msg) {
        return "Eureka provider \t" +msg + " \t" + UUID.randomUUID();
    }
}

重启服务,保证接口正常

image-20200614095409438

Eureka 注册中心集群版

Eureka 集群原理

Eureka 没有区分中心节点和从节点,集群中每个节点地位相等,节点之间通过相互注册和相互守望,保证集群可用性。

假设注册中心有三个 Eureka服务节点 A、B、C,想要搭建 Eureka Server 集群,只需要将 A 节点的信息注册到 B 和 C 节点上,将 B 节点的信息注册到 A 和 C 上,将 C 节点的信息注册到 A 和 B 上,这样就完成了服务端集群搭建。

集群版 Eureka 服务端搭建

正规的集群搭建应该将 Eureka 服务端部署在不同的 ip 机器上,由于本地只有一台机器,所以只能通过区分端口号模拟搭建 Eureka 服务端集群,端口号分别为 8761、8762、8763.

单机版我们已经有了一个 cloud-eureka-server-8761 的服务端,再次创建 cloud-eureka-server-8762 、cloud-eureka-server-8763 两个服务端,构建一个拥有三个节点的eureka 集群

整理集群信息

项目名 节点名称 端口号
cloud-eureka-server-8761 eureka8761.com 8761
cloud-eureka-server-8762 eureka8762.com 8762
cloud-eureka-server-8763 eureka8763.com 8763

修改 host 文件

host 文件地址 C:\Windows\System32\drivers\etc,将以下文件信息添加到 host 文件末尾

127.0.0.1       eureka8761.com
127.0.0.1       eureka8762.com
127.0.0.1       eureka8763.com

改造 cloud-eureka-server-8761 服务端

修改 application.yml 文件

server:
  port: 8761

# 单机版
#eureka:
#  instance:
#    hostname: localhost
#  client:
#    registerWithEureka: false
#    fetchRegistry: false
#    serviceUrl:
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

# 集群版
eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka8761.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    service-url:
      defaultZone: http://eureka8762.com:8762/eureka/,http://eureka8763.com:8763/eureka/

创建 cloud-eureka-server-8762 服务端

修改 application.yml 文件

server:
  port: 8762

# 集群版
eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka8762.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    service-url:
      defaultZone: http://eureka8761.com:8761/eureka/,http://eureka8763.com:8763/eureka/

创建 cloud-eureka-server-8763 服务端

修改 application.yml 文件

server:
  port: 8763

# 集群版
eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka8763.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    service-url:
      defaultZone: http://eureka8762.com:8762/eureka/,http://eureka8761.com:8761/eureka/

测试

分别启动三个服务节点

访问 http://localhost:8761/ ,发现 8762 和 8763 两个节点已经注册上

image-20200726232858872

访问 http://localhost:8762/ ,发现 8761 和 8763 两个节点已经注册上

image-20200726232929098

访问 http://localhost:8763/ ,发现 8761 和 8762 两个节点已经注册上

image-20200726232951775

猜你喜欢

转载自www.cnblogs.com/dtdx/p/13382974.html