码迷,mamicode.com
首页 > 编程语言 > 详细

springcloud02- Eureka 服务注册与发现

时间:2020-07-19 23:34:36      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:服务发现   消费   架构   run   enable   网络   操作   ice   进不去   

Eureka 服务注册与发现

什么是Eureka

遵循的是AP原则

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

技术图片

原理讲解

  • Eureka的基本架构
    • SpringCloud封装了NetFlix公司开发的Eureka模块来实现服务注册与发现(对比Zookeeper)
    • Eureka采用了C/S架构设计,使用Eureka的客户端连接到EurekaServer并维持心跳连接,这样系统的维护人员可以通过EurekaServer来监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(比如Zuul)就可以通过EurekaServer来发现系统中的其他微服务,并执行相关逻辑
  • 三大角色
    • Eureka Server:提供服务的注册与发现
    • Service Provider:将自身服务注册到Eureka中,从而使消费方能够找到
    • Service ConSumer: 服务消费方从Eureka Server中获取注册服务列表,从而找到消费服务

服务注册

当我们新添加一个微服务实例的时候,微服务就会将自己的 ip 与 port 发送到注册中心,在注册中心里面记录起来。当 API gateway 需要访问某些微服务的时候,就会去注册中心取到相应的 ip 与 port。从而实现自动化操作。

1.编写Eureka的服务(注册中心)

? 1)导入依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <!--热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

? 2)配置application.yml

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost   #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false  #fetch-registry如果为false,则表示自己为注册中心
    service-url: #监控页面
       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

? 3)主启动类

package com.mjh.springcloud;

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

/**
 * 启动之后,访问http://localhost:7001/
 */

@SpringBootApplication
@EnableEurekaServer   //@EnableEurekaServer  服务端启动类,可以接受别人注册进来(即注册中心)
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

? 4)启动

技术图片

服务发现

微服务架构下服务实例具有动态分配的网络地址,随着服务的自动扩展、故障和发布升级,导致服务实例的网络地址发生动态变更。因此,需要一种机制,支持服务消费者在服务提供者实例地址发生变更时,能够及时感知获取实例最新的地址,即服务发现机制。

2.想办法把上一次的服务提供者8001端口注册进来

? 1)导入依赖

<!--Eureka-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

? 2)在配置文件里把Eureka服务注册进去,有了Eureka服务就可以吧东西注册到注册中心里去了。

   #Eureka服务
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
      

? 3)开启主启动类支持Eureka服务

//启动类
@SpringBootApplication
@EnableEurekaClient   //服务启动后自动注册到Eureka中
public class DeptPrivider_8001 {
    public static void main(String[] args) {
        SpringApplication.run( DeptPrivider_8001.class,args);
    }
}

? 4)分别启动7001端口和8001端口,之后刷新http://localhost:7001/

? 5)可以把status的地址干掉

#Eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-privider-dept8001 #修改Eureka的状态描述

技术图片

当注册中心在开启的时候我们可能会把客户端停掉,这时候Eureka会自动开启自我保护机制

自我保护机制

某时刻某一个微服务不可以用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存!

  • 默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka支架无法正常通行,以上行为=可能变得非常危险了,因为微服务本身其实是健康的。此时不应该注销这个服务。Eureka通过自我保护来解决这个问题,当EurekaServer节点在短时内丢失过多得客户端时(可能发生网络故障),那么这个节点聚会进入自我保护模式,一旦进入该模式,EurekaSever就会保护服务注册表中的信息,不再删除服务注册表中的数据(也不会注销任何微服务)。当网络故障后,该EurekaServer节点会自动退出自我保护模式。(宁可保留错误的服务注册信息,也不盲目注销任何可能监控的服务实例,一句话:好死不如赖活着)能让Eureka集群更加健壮和稳定

技术图片

6)我们会发现,我们点status的地址时候会报出404页面,是因为我们没有导入依赖,所有进不去,我们需要的话也可以自己配置一下。

<!--actuator完善监控信息-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
#info配置
info:
  app.name:  mjh.springcloud
  company.name: blog.mjh.com

技术图片

我们也可以通过controller层获取微服务相关的信息,里面有一个服务发现,你需要告诉别人你写了什么东西(这些都是在企业中联合开发才用的到),然后还要在主启动类添加@EnableDiscoveryClient //服务发现 ,让其生效。

@GetMapping("/dept/discovery")
//注册进来的微服务,获取一些消息
public Object discovery(){
    //获取微服务到表的清单
    List<String> services = client.getServices();
    System.out.println("discovet=>services:"+services);

    //得到一个具体的微服务信息,通过具体的微服务id,applicationName
    List<ServiceInstance> instances= client.getInstances("SPRINGCLOUD-PRIVIDER-DEPT");

    for (ServiceInstance instance : instances) {
        System.out.println(
                        instance.getHost()+"\t"+
                        instance.getPort()+"\t"+
                        instance.getUri()+"\t"+
                        instance.getServiceId()
        );
    }
    return this.client;
}

springcloud02- Eureka 服务注册与发现

标签:服务发现   消费   架构   run   enable   网络   操作   ice   进不去   

原文地址:https://www.cnblogs.com/mjjh/p/13340850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!