Spring Cloud学习——Eureka

Spring Cloud学习——Eureka

1.Eureka基础知识

1.1Eureka是什么?
Spring Cloud Eureka是Spring Cloud Netflix 微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能。Spring Cloud 通过为Eureka增加了Spring Boot风格的自动化配置,我们只需通过引入依赖和注解配置就能让Spring Boot构建的微服务应用轻松的与Eureka服务治理体系进行整合。
1.2什么是服务治理?
服务治理可以说是微服务架构中最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册与发现。
使用服务治理的原因:在传统的RPC远程调用框架中,管理每个服务于服务之间依赖关系比较复杂,管理起来比较困难,所以需要使用服务治理,管理服务与服务之间的依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
1.3什么是服务注册与发现?
服务注册:

  • 在服务治理框架中,通常都会有一个服务注册中心。
  • 每一个微服务实例向注册中心登记自己提供的服务,将主机、端口号、版本号、通信协议等一些信息告知注册中心。
  • 注册中心按服务名分类组织服务清单。
  • 服务注册中心需要以心跳的方式监测服务清单中的服务是否可用,如果不可用,需要将不可用的服务实例进行剔除。
    在这里插入图片描述
    在这里插入图片描述

服务发现:
  在服务治理框架的运作下,服务间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。所以,服务调用方在调用服务提供方接口时,并不知道具体的服务实例位置。因此,调用方需要向注册中心咨询服务,并获取所有服务的实例清单,以实现对具体服务实例的访问。比如:有服务C希望调用服务A,服务C就向注册中心发起咨询请求,服务注册中心就会将服务A的位置清单返回给服务C,当服务C要发起调用时,便从该清单中以某种轮询策略取出一个位置来进行服务调用(客户端负载均衡)。
  
1.4 Eureka的两个组件

  • Eureka Server:服务注册中心。各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中会存储所有可用服务节点的信息。
  • Eureka Client:通过注册中心进行访问。是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询负载算法的负载均衡器。在应用启动后,将会向Eureka Server发生心跳(默认周期为30s)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个节点移除(默认90s)。

1.5 Eureka Server的使用
(1)引入pom依赖

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

(2)编写application.yml,配置Eureka

server:
  port: 7001


eureka:
  instance:
    hostname: localhost   #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false   #false表示自己端就是注册中心
    service-url:
      defaultZone: http://localhost:7001/eureka/   #单机

    # 关闭自我保护机制,保证不可用服务被及时剔除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000
spring:
  application:
    name: cloud-eureka-service

(3)编写主启动类,最重要的一个注解@EnableEurekaServer

package com.springcloud;

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

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

(4)启动一把,在浏览器输入http://localhost:7001/ ,出现以下界面说明Eureka配置成功
在这里插入图片描述

1.6 Eureka Client的使用
在另一个微服务模块中:
(1)引入pom依赖

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

(2)修改application.yml

eureka:
  client:
    register-with-eureka: true   #是否将自己注册到注册中心,集群必须设置为true配合ribbon
    fetch-registry: true    #是否从服务端抓取已有的注册信息
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka #,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    prefer-ip-address: true  #访问路径可以显示IP地址
    lease-renewal-interval-in-seconds: 1  #向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-expiration-duration-in-seconds: 2 #收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除

(3)在主启动类中添加注解 @EnableEurekaClient

package com.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class, args);
    }
}

当启动完此微服务名称后,该微服务的名称:

spring:
  application:
    name: cloud-payment-service

会在Eureka中显示。
微服务RPC远程服务调用最核心的是什么呢?
高可用!所以要搭建Eureka注册中心集群,实现负载均衡+故障容错,集群的原理就是互相注册,互相守望。集群的步骤就是修改yml文件的defaultZone为其他注册中心的url。

猜你喜欢

转载自blog.csdn.net/weixin_43395911/article/details/104943512