Eureka 服务注册与发现一

Eureke服务注册与发现

什么是Eureke?

  • Eureka:英 /juˈriːkə/ 美 /juˈriːkə/ 尤里卡 优瑞卡
  • NetFilx在设计Eureka时,遵循的就是AP原则
  • Eureka是NetFilx的一个子模块,也是核心模块之一,Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了,功能类似于Dubbo的注册中心,比如Zookeeper

原理讲解

  • Eureka的基本架构

    • Springcloud封装了NetFIlx公司开发的Eureka模块来实现服务注册与发现
    • Eureka采用了c-s的架构设计,EurekaServer作为服务注册功能的服务器,他是服务注册中心
    • 而系统中其他微服务,使用Eureka的客户端连接到EurekaServer并维持心跳连接,这样系统的维护人员就可以通过EurekaServer来监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(如Zuul)就可以通过EurekaServer来发现系统中的其他微服务,并执行相关的逻辑
    • Eureka包含两个组件:Eureka Server 和 Eureka Client
    • Eureka Server 提供服务注册服务,各个节点启动后,会在Eureka Server中注册,这样Eureka Server中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
    • Eureka Client 是一个java客户端,用于简化Eureka Server 的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器。在应用启动后,将会向Eurake Server发送心跳(默认周期为30秒).如果Eureka Server 将会从服务器中把这个服务节点移除掉(默认周期90秒)
  • 三大角色

    • Eureka Server :提供服务的注册与发现,类似 Zookeeper
    • Service Provider: 将自身注册到Eureka中,从而使消费者能够找到
    • Service Consumer: 服务消费者从Eurake中获取服务注册列表,从而找到服务进行消耗。

编写Eureka注册中心

spring cloud 好像就4步

    1. 导入依赖
    1. 编写配置
    1. 编写配置类
    1. 开启注解
  1. 导入依赖
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>springcloud</artifactId>
        <groupId>cn.lzm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>sprIngcloud-eureka-7001</artifactId>

     <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 编写配置信息
  • application.yml
server:
  port: 7001

#Eureka 注册中心配置,基本上都这样配置
eureka:
  instance:
    hostname: localhost # Eurake 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向Eureka 注册中心注册自己
    fetch-registry: false # fatch-registry 为 false表示 自己为注册中心
    service-url: # 监控页面
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  1. 编写启动类,开启注解
package cn.lzm.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //就是这个注解
public class EurakeServer_7001 {

    public static void main(String[] args) {
        SpringApplication.run(EurakeServer_7001.class,args);
    }
}
  1. 测试,查看监控页面(#.#)

开启服务注册,用的是之前写的服务提供者代码

https://www.cnblogs.com/xiaominaaaa/p/14044471.html

  1. 在pom.xml中加入Eureka客户端
  <!--Eureka 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  1. 为启动类添加@EnableEurekaClient注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class PersonProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(PersonProviderApplication.class,args);
    }
}
  1. 客户端配置
# Eureka 客户端配置
eureka:
  instance:
    appname: app
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/ #指向注册中心
  1. 成功开启后可以在Eureka注册中心看到注册的信息

配置Eureka 监控信息

  • Eureka 是能够点进去查看信息的,我们现在做的就是完善它
  1. pom.xml添加
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 配置
#info配置
info:
  app.name: laosi-springcloud # app名字
  company.name: cn.lzm #公司名称
  1. 查看,点击页面可以看到json数据,就是刚刚配置的

自我保护机制


一句话总结:某时刻某一个微服务不可以用了,Eureka不会立即清理掉,依旧会对微服务的信息进行保存!

  • 默认情况下,如果EurekaServer在一定时间内没有收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认时间90s)。但是当网络分区故障发生时,微服务与Eureka之间无法正常通行,以上行为可能变得非常危险了-因为微服务本身其实是健康的,此时不应该注销这个服务。Eureka通过自我保护机制来解决这个问题,当EurekaServer节点在短时间内丢失过多客户端是(可能发生了网络分区故障),那么这个节点就会进入自我保护模式,一旦进入模式,EurakeServer就会保护服务注册表中的信息,不在删除服务注册表中的数据(也就是不会注销任何微服务).但当罗故障恢复后,该EurekaServer节点会自动退出自我保护模式。
  • 在自我保护模式中,EurekaServer会保护服务注册中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该EurekaServer节点就会自动退出自我保护模式。他的设计哲学就是宁保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话,好死不如赖活着。
  • 综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不是盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮和稳定
  • 在SpringCloud中,可以使用eureka.server.enable-self-preservation = false禁用自我保护模式[不推荐关闭揍我保护模式]

服务发现

  • 让别的服务来访问的时候可以获取一些服务的信息
  1. 提供接口
import cn.lzm.springcloud.pojo.Person;
import cn.lzm.springcloud.service.PersonService;
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.*;
import java.util.List;
/**
 * 关键,提供 Restful 服务
 */
@RestController
public class PersonController {
    @Autowired
    PersonService personService;
    //获得一些信息,得到具体的微服务
    @Autowired
    DiscoveryClient discoveryClient;
    @GetMapping("/person/discover")
    public Object discover(){
        //获取微服务列表
        List<String> services = discoveryClient.getServices();
        System.out.println("discover=>services:  "+services);
        //得到一个具体的微服务信息
        List<ServiceInstance> list = discoveryClient.getInstances("app");
        for (ServiceInstance serviceInstance : list) {
            System.out.println(serviceInstance.getHost()+"\t"+
                    serviceInstance.getInstanceId()+"\t"+
                    serviceInstance.getScheme()+"\t"+
                    serviceInstance.getServiceId()+"\t"+
                    serviceInstance.getPort()+"\t");
        }
        return discoveryClient;
    }

    @GetMapping("/person/{id}")
    public Person getPersonById(@PathVariable("id") int id) {
        return personService.getPersonById(id);
    }

    @PostMapping("/person/add")
    public String addPerson(Person person) {
        personService.addPerson(person);
        return"1";
    }
    @GetMapping("/person/getall")
    public List<Person> getAllPerson() {
        return personService.getAllPerson();
    }
}
  1. 开启服务发现
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //允许服务发现
public class PersonProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(PersonProviderApplication.class,args);
    }
}
  1. 测试一下
posted @ 2020-12-11 12:16  阿肆啊  阅读(156)  评论(0编辑  收藏  举报