• SpringCloud学习笔记(eureka注册中心)


    Spring Cloud Eureka是Spring Cloud Netflix 子项目的核心组件之一,主要用于微服务架构中的服务治理。在微服务架构中往往会有一个注册中心,每个微服务都会向注册中心去注册自己的地址及端口信息,注册中心维护着服务名称与服务实例的对应关系。每个微服务都会定时从注册中心获取服务列表,同时汇报自己的运行情况,这样当有的服务需要调用其他服务时,就可以从自己获取到的服务列表中获取实例地址进行调用,Eureka实现了这套服务注册与发现机制

    • 使用的工具是idea,创建的是mavne项目手动添加依赖,或者通过Spring Initializr去创建都可以
      在这里插入图片描述

    • 如果是父子工程的话,需要在父工程pom文件中添加springboot的父依赖和springcloud依赖

       
       <parent>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-parentartifactId>
           <version>2.2.1.RELEASEversion>
       parent>
       
       
       <dependency>
           <groupId>org.springframework.cloudgroupId>
           <artifactId>spring-cloud-dependenciesartifactId>
           <version>Hoxton.SR1version>
           <type>pomtype>
           <scope>importscope>
       dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 在子项目中添加eureka服务端依赖
    
    <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">
        <parent>
            <artifactId>SpringCloudartifactId>
            <groupId>org.examplegroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>eurekaartifactId>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 编写启动类
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaRegistryApplication01 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaRegistryApplication01.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • application.yml配置文件
    server:
      port: 8761  #端口号
    spring:
      application:
        name: eureka-server #应用名称,集群情况下该名称相同
    eureka:
      instance:
        hostname: eurekaServer #主机名,不配置时根据操作系统主机名称取获取
      client:
        register-with-eureka: false #默认值是true,是否将自己注册到注册中心
        fetch-registry: false # 默认值是true,是否从注册中心拉取服务
        service-url:
          defaultZone: http://localhost:8761/eureka/ #注册中信对外暴露的注册中心地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    启动启动类,浏览器输入http://localhost:8761/,看到下面页面说明注册中心启动成功
    在这里插入图片描述

    高可用eureka注册中心

    在上面的基础之上再创建一个eureka子项目,以达到集群目的,pom文件引入的依赖都和上面一样,区别在于service-url指向的是另外一个eureka注册中心并且是否将自己注册到注册中的配置需要配置为true

    • 修改配置文件
    server:
      port: 8761  #端口号
    spring:
      application:
        name: eureka-server #应用名称,集群情况下该名称相同
    eureka:
      instance:
        hostname: eurekaServer01 #主机名,不配置时根据操作系统主机名称取获取
      client:
        register-with-eureka: true #默认值是true,是否将自己注册到注册中心
        fetch-registry: false # 默认值是true,是否从注册中心拉取服务
        service-url:
          defaultZone: http://localhost:8762/eureka/ #注册中信对外暴露的注册中心地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    server:
      port: 8762  #端口号
    
    spring:
      application:
        name: eureka-server #应用名称,集群情况下该名称相同
    
    eureka:
      instance:
        hostname: eurekaServer02 #主机名,不配置时根据操作系统主机名称取获取
      client:
        register-with-eureka: true #默认值是true,是否将自己注册到注册中心
        fetch-registry: false # 默认值是true,是否从注册中心拉取服务
        service-url: #注册中信对外暴露的注册中心地址
          defaultZone: http://localhost:8761/eureka/ #注册中心对外暴露的地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 启动启动类,浏览器输入地址就能看到eureka注册服务的界面

    在这里插入图片描述

    在这里插入图片描述

    eureka自我保护机制

    eureka本身作为一个服务运行,提供注册中心功能。将一个个服务提供者注册到eureka中,eureka通过服务提供者的心跳(每30s发送一次心跳包)来判断该服务是否活着,同时会定期删除90s未发送心跳包的服务。当我们的服务提供者因为断电,网络等原因导致服务停掉。eureka就会启动自我保护机制(启动自我保护阈值,15分钟内心跳失败比例是否低于85%,如果低于就会启动自我保护机制),不再删除注册表中的服务实例,当服务重启或网路恢复后,就会退出保护机制。自我保护是对网路异常的安全保护措施。
    触发eureka自我保护机制

    代码地址链接

  • 相关阅读:
    Java笔试复盘
    05.数据解析之正则表达式
    智能采购管理系统有哪些应用优势?如何高效提升医药制造业采购管理效率?
    已解决:oracle新增列字段报错,ORA-00972:identifier is too long
    dotnet平台Http消息处理者工厂
    用检索做时间序列预测是一种怎样的体验
    【redis持久化模式】两种持久化方式、概念、原理、如何使用、优劣势_Redis05
    程序员注意!35岁前,别靠死工资过日子
    Unity初学者肯定能用得上的50个小技巧
    上机实验四 图的最小生成树算法设计 西安石油大学数据结构
  • 原文地址:https://blog.csdn.net/qq_42494654/article/details/119479373