spring cloud 基础实战(二) -单机版微服务注册

在这里插入图片描述

- 问题: 系统会根据业务拆分成了很多的微服务,微服务的信息如何管理?
spring cloud 中提供服务注册中心来管理微服务信息。

- 为什么要用注册中心?
1.微服务数量众多,要进行远程调用就需要知道服务端的ip地址和端口,注册中心帮助我们管理这些服务的ip和端口。
2.微服务会实时上报自己的状态,注册中心统一管理这些微服务的状态,将存在问题的服务踢出服务列表,客户端获取到可用的服务进行调用。

3.8 Eureka 介绍

  • spring cloud Eureka 是对Netflix 公司的第二次封装,它实现了服务治理的功能,Spring Cloud Eureka 提供 Eureka Server 服务端与Eureka Client 客户端,服务端既是Eureka服务注册中心,客户端完成微服务向Eureka服务的注册与发现。

  • 客户端同时也具备一个内置的使用轮询(round-robin)负载均衡器,在微服务启动后,将会向 Eureka Server 发送心跳(默认周期为30秒 s)。如果Eureka Server 在多个心跳周期内没有接收到某个节点的心跳,Eureka Server 将会服务注册表中把这个服务节点移除(默认90秒)。

  • 下图显示了Eureka Server 与 Eureka Client 关系
    在这里插入图片描述

  • Eureka Server 是服务端,负责管理各个微服务注册和发现。

  • 在微服务上添加Eureka Client代码,就会访问到Eureka Server 将此微服务注册在Eureka Server中,从而使服务消费方能够找到。

  • 微服务(服务消费者)需要调用另一个微服务(服务提供者)时,从Eureka Server 中获取服务调用地址,进行远程调用。

3.9 搭建单机版 Eureka Server服务注册中心

3.9.1 创建 studentservice-cloud-05-eureka-6001 工程

3.9.2 配置pom.xml

在这里插入图片描述

3.9.3 配置application.yml

server:
  port: 6001 #服务端端口

eureka:
  instance:
    hostname: localhost #eureka服务端的名称
  client:
    register-with-eureka: false #服务注册,false表示不将自己注册到Eureka服务中
    fetch-registry: false #服务发现,false表示自己不从Eureka服务中获取注册信息
    service-url: # Eureka客户端与Eureka服务端的交互地址,集群版配置对方的地址,单机版配置自己(如果不配置则默认本机8761端口)
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.9.4 创建主启动类

  • 在启动类上添加@EnableEurekaServer注解,表示它是一个Eureka Server 服务注册中心
package com.asyt.springcloud;


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

@EnableEurekaServer   //表识一个Eureka Server 服务注册中心
@SpringBootApplication
public class EurekaServer_6001 {

    public static void main(String[] args){
        SpringApplication.run(EurekaServer_6001.class,args);
    }
}

4.0 服务注册到Eureka Server服务注册中心

  • 将服务提供者(studentservice-cloud-03-provider-product-8001)注册到Eureka Server注册中心(studentservice-cloud-05-eureka-6001)
    在这里插入图片描述
  • 下面对studentservice-cloud-03-provider-product-8001模块做对应的修改

4.0.1配置pom.xml文件

  • 添加配置
<!--导入 Eureka 客户端的依赖,将微服务提供者注册进 Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

4.0.1配置application.yml

  • 添加配置
eureka:
  client:
    register-with-eureka: true # 服务注册开关
    fetch-registry: true  # 服务发现开关
    service-url: # 注册到哪一个Eureka Server 服务注册中心,多个中间用逗号分割
      defaultZone: http://localhost:6001/eureka

4.0.2修改主启动类

  • 在启动类上添加@EnableEurekaClient注解,表示它是一个Eureka客户端,本服务启动后会自动注册进Eureka Server服务列表中
package com.asyt.springcloud;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.stereotype.Repository;

@MapperScan(value = "com.asyt.springcloud.mapper",annotationClass = Repository.class)//扫描包下面所有Mapper接口
@EnableEurekaClient  //eureka 客户端,本服务启动后会自动注册进Eureka中心
@SpringBootApplication
public class StudentProver_8001 {

    public static void main(String[]args) {
        SpringApplication.run(StudentProver_8001.class,args);
    }
}

4.0.3 功能测试

 - 先要启动Eureka Server : studentservice-cloud-05-eureka-6001
 - 再启动Eureka Client : studentservice-cloud-03-provider-product-8001
 - 访问: http://localhost:6001

在这里插入图片描述

4.1 微服务注册信息完善

指定实例ID

  • 当前状态中 含有主机名称
    在这里插入图片描述

  • 解决 修改studentservice-cloud-03-provider-product-8001的application.yml
    在这里插入图片描述
    在这里插入图片描述

4.2 Eureka Server自我保护机制

4.2.1 自我保护现象

如长时间没有访问,检测不到心跳,或者修改实例名称,eureka启动自我保护机制
下图中红色提示:表示已经启动了自我保护机制
在某时某一个服务不可用了,Eureka不会立刻删除,依旧会对该微服务的信息进行保存
在这里插入图片描述

4.2.2 什么是自我保护机制

当Eureka Server 在一定时间内(默认90秒)没有接收到某个微服务的心跳,Eureka Server会从服务列表将此服 务实例注销。但是如果出现网络异常情况(微服务本身是正常的),微服务与Eureka Server之间无法正常通信, 以上行为可能变得非常危险了——因为微服务本身其实是正常的,此时本不应该注销这个微服务。
Eureka Server有一种 “自我保护模式” 来解决这个问题——当Eureka Server在短时间内丢失过多客户端时(可能发 生了网络故障),此时Eureka Server会进入自保护模式,一旦进入该模式,Eureka Server就会保护服务注册表中 的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server会 自动退出自我保护模式。
所以, 自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服 务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的 健壮、稳定。

4.2.3自我保护机制配置

在开发阶段建议禁用自我保护模式,默认开启了

  • 在eureka server中配置:studentservice-cloud-05-eureka-6001
    在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值