【SpringCloud总结】4. Eureka服务注册与发现

本文的实验案例

https://blog.csdn.net/FullStackDeveloper0/article/details/90473559

先看微服务案例,再看本文

 

一、简介

Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了。功能类似于dubbo的注册中心,比如Zookeeper。

作为服务注册中心,Eureka比Zookeeper好在哪里?
    著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A(可用性)和P(分区容错性)。由于分区容错性P是在分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡。
    因此  Zookeeper保证的是CP,Eureka则是AP。
          
Zookeeper保证CP

因网络问题使得zk集群失去master节点是较大概率会发生的事,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行leader选举。问题在于,选举leader的时间太长,30 ~ 120s, 且选举期间整个zk集群都是不可用的,这就导致在选举期间注册服务瘫痪。虽然服务能够最终恢复,但是漫长的选举时间导致的注册长期不可用是不能容忍的。

Eureka保证AP
Eureka各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。
而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。

Eureka作为单纯的服务注册中心来说要比zookeeper更加“专业”,因为注册服务更重要的是可用性,我们可以接受短期内达不到一致性的状况。
 

 

二、原理

1. 基本架构

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现。
Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。

Eureka包含两个组件:Eureka Server 和 Eureka Client
(1)Eureka Server提供服务注册服务。各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到
 
(2)EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)

 

2. 三大角色

Eureka Server 提供服务注册和发现

Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到

Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务

 

3. 盘点下目前我们的工程情况

    总父工程

    通用模块api

    服务提供者Provider

    服务消费者Consumer

    具体参考:https://blog.csdn.net/FullStackDeveloper0/article/details/90473559

 

三、构建步骤

1. microservicecloud - eureka-7001 Eureka Server

(1) 点击父工程,右键创建Maven moudle,packaging是jar

(2)pom.xml 文件

<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>

  <!-- 父项目坐标信息 -->
  <parent>
   <groupId>com.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>

  <!-- 本项目的坐标描述 -->
  <artifactId>microservicecloud-eureka-7001</artifactId>


  <dependencies>
   <!--eureka-server服务端 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
   </dependency>
  </dependencies>

</project>

(3)yml文件

server: 
  port: 7001
 
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己。
    fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/        
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
 

(4)EurekaServer7001_App主启动类

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer//EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(EurekaServer7001_App.class, args);
  }
}

(5)测试

         http://localhost:7001/

 

2. 服务注册

(1)修改microservicecloud-provider-dept-8001

(2)pom.xml文件 (修改的部分)

   <!-- 将微服务provider侧注册进eureka -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>

(3)yml文件(修改部分)

eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://localhost:7001/eureka

(4)DeptProvider8001_App主启动类

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
public class DeptProvider8001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptProvider8001_App.class, args);
  }
}

(5)测试

       先启动server,在启动微服务! 我们可以在server的注册列表中看到我们注册的微服务!

 

3. 服务的发现(了解)

   对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息

(1)修改microservicecloud-provider-dept-8001工程(提供者)的DeptController

package com.atguigu.springcloud.controller;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptService;


@RestController
public class DeptController
{
  
  @Autowired
  private DiscoveryClient client;
  
  @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
  public Object discovery()
  {
    //获取所有的微服务
    List<String> list = client.getServices();
    System.out.println("**********" + list);
    /*
      注意: list集合里面的字符串就是注册列表里面微服务的名字。每一个微服务下面
             有很多微服务实例。
     for(String s:list){
        List<ServiceInstance> srvList = client.getInstances(s);
        for (ServiceInstance element : srvList) {
           System.out.println(
                  element.getServiceId() + "\t" + 
                  element.getHost() + "\t" + 
                  element.getPort() + "\t"+ 
                  element.getUri());
         }
     }
    */

    //获取指定名称的微服务
    List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
    //遍历微服务实例
    for (ServiceInstance element : srvList) {
     System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
         + element.getUri());
    }
    return this.client;
  }


}

【提示】上面的微服务名 MICROSERVICECLOUD-DEPT 是来自yml中的配置

 

(2)DeptProvider8001_App主启动类

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
public class DeptProvider8001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptProvider8001_App.class, args);
  }
}

(3)测试

   先要启动EurekaServer,再启动DeptProvider8001_App主启动类,需要稍等一会儿!

   http://localhost:8001/dept/discovery   发送请求,查看返回信息

   当然,我们可以在microservicecloud-consumer-dept-80(消费者)工程中编写Controller来查看服务信息

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值