【SpringCloud】Eureka 服务注册中心入门

在这里插入图片描述

Eureka 服务注册中心

1、什么是注册中心

注册中心相当于微服务架构中的“通讯录”,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到注册中心,当服务需要调用其他服务时,就到这里找到服务的地址,进行调用。

举个例子

  • 服务发现:你先要给张三打电话,于是你在通讯录中寻找张三的电话,并拨打电话

  • 服务注册:李四告诉了你的电话号,你把电话号存在通讯录中以方便以后在通讯中找到他

总结:服务注册中心的作用就是服务的注册服务的发现

2、常见的注册中心

  • Netflix Eureka
  • Alibaba Nacos
  • HashiCorp Consul
  • Apache Zookeeper
  • CoreOS Etcd
  • CNCF CoreDNS
特性EurekaNacosConsulZookeeper
CAPAPCP+APCPCP
健康检查Client BeatTCP/HTTP/MySQL/Client BeatTCP/HTTP/gRPC/CmdKeep Alive
雪崩保护
自动注销实例支持支持不支持支持
访问协议HTTPHTTP/DNSHTTP/DNSTCP
监听支持支持支持支持支持
多数据中心支持支持支持不支持
跨注册中心同步不支持支持支持不支持
SpringCloud集成支持支持支持支持

3、为什么需要注册中心

在分布式系统中,我们不仅仅是需要在注册中心找到服务和服务地址的映射关系这么简单,还需要考虑更多更复杂的问题:

  • 服务注册后,如何被及时的发现
  • 服务宕机后,如何及时下线
  • 服务如何有效的水平扩展
  • 服务发现后,如何进行路由
  • 服务异常时,如何进行降级
  • 注册中心如何实现自身的高可用

这些问题的解决都依赖于注册中心。简单来说,注册中心的功能类似于 DNS 服务器或者负载均衡器,而实际上,注册中心作为微服务的基础组件,可能要更加复杂,也需要更多的灵活性和时效性。所以我们还需要学习更多的 SpringCloud 微服务组件协同完成应用开发。

4、什么是 Eureka 注册中心

Eureka 是 Netflix 开发的服务发现组件,本身是一个基于 REST 的服务。SpringCloud 将它集成在其子项目 SpringCloud Netflix 中,实现 SpringCloud 的服务注册与发现,同时还提供了负载均衡、故障转移等能力。

5、Eureka 注册中心三种角色

image-20220427223520722

  • Eureka Server
    • 通过 Register、Get、Renew 等接口提供服务的注册和发现
  • Application Service(Service Provider)
    • 服务提供方,把自身的服务实例注册到 Euraka Server 中
  • Application Client(Service Consumer)
    • 服务调用方,通过 Eureka Server 获取服务列表,消费服务

image-20220427224350951

  • 0、实例化服务
  • 1、将服务注册到注册中心
  • 2、注册中心收录服务
  • 3、注册中心获取服务列表
    • user-service 127.0.0.1:9091
    • goods-service 127.0.0.1:9092
    • sso-service 127.0.0.1:9093
  • 4、基于负载均衡算法从地址列表选择一个服务地址进行服务调用
  • 5、定期发送心跳
  • 6、检查没有定期发送心跳的服务并在一定时间内剔除服务地址列表

6、Eureka 入门案例

单节点版本!

创建纯 maven 项目,不勾选 Create from archetype,作为父项目
22222222222222
添加依赖 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.fengluo</groupId>
    <artifactId>eureka-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!-- Spring Cloud Hoxton.SR1 依赖 -->
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <!-- 集成 spring-boot-starter-parent 依赖 -->
    <!-- 使用集成方式,实现复用,符合继承的都可以被使用 -->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <!-- 项目依赖管理 父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息)-->
    <dependencyManagement>
        <dependencies>
            <!-- spring cloud 依赖 -->
            <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>

</project>

创建 Eureka 节点,在 Create from archetype 中使用 quickstart 模板

image-20220429172132156

添加 eureka-server01 的依赖 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>eureka-server01</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.fengluo</groupId>
    <artifactId>eureka-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <name>eureka-server01</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- netflix eureka server 依赖 -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

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

    <!-- spring boot test 依赖 -->
    <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>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

image-20220429173116675

EurekaServerApplication 启动类

package org.fengluo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class);
    }
}

application.yml

server:
  port: 8761 # 端口

spring:
  application:
    name: eureka-server # 注册名称

# 配置 Eureka Server 注册中心
eureka:
  instance:
    hostname: localhost # 主机名,不配置的时候将根据操作系统的主机名来获取
  client:
    register-with-eureka: false # 是否将自己注册到注册中心,默认为 true
    fetch-registry: false # 是否从注册中心获取服务注册信息,默认为 true
    service-url: # 注册中心对外暴露的注册地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动项目

fuck

至此,我们的单节点 Eureka Server 注册中心就配置成功了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud是一个基于Spring Boot的开源框架,用于构建分布式系统的微服务架构。它提供了一系列的工具和组件,用于简化微服务架构中的开发、部署、配置和监控等任务。 入门Spring Cloud可以按照以下步骤进行: 1. 了解微服务架构:首先,你需要了解什么是微服务架构以及它的特点和优势。微服务架构是一种将一个大型应用拆分成若干个小而独立的服务的架构风格,每个服务都可以独立开发、部署和扩展。 2. 学习Spring Boot:Spring Cloud是基于Spring Boot构建的,所以你需要先掌握Spring Boot。Spring Boot是一个简化了Spring应用开发的框架,它提供了自动配置、快速开发等特性。 3. 了解Spring Cloud的核心组件:Spring Cloud包含了一系列核心组件,例如服务注册与发现、负载均衡、配置管理等。你需要了解这些组件的作用和使用方式。 4. 搭建开发环境:根据你的喜好,选择合适的IDE(如IntelliJ IDEA、Eclipse)和构建工具(如Maven、Gradle)搭建开发环境。 5. 创建微服务:使用Spring Boot创建一个简单的微服务应用,并运行起来。你可以选择使用Spring Cloud提供的组件,例如Eureka作为服务注册与发现组件。 6. 实现服务之间的通信:学习如何在微服务架构中实现服务之间的通信。Spring Cloud提供了多种通信方式,例如RESTful API、消息队列等。 7. 实现负载均衡和容错:了解如何使用Spring Cloud实现负载均衡和容错。Spring Cloud提供了多种负载均衡和容错的机制,例如Ribbon、Hystrix等。 8. 配置管理:学习如何使用Spring Cloud实现配置管理。Spring Cloud提供了Config Server组件,可以集中管理和动态刷新应用的配置信息。 9. 部署和监控:了解如何将微服务应用部署到生产环境,并学习如何监控微服务的运行状态。 以上是入门Spring Cloud的一些基本步骤,希望对你有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风落_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值