• 【SpringCloud微服务】- Eureka服务注册与服务发现Discovery


    Eureka简介

    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。

    SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

    Eureka包含两个组件:Eureka ServerEureka Client

    • Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
    • Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

    在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除。
    在这里插入图片描述

    服务注册

    现在存在两个微服务,一个服务提供者,一个服务消费者,现在我们就来把两个微服务注册到Eureka中。

    创建Eureka注册中心

    Eureka本身也是以一个服务的形式存在的,所以接下来直接创建一个Module,并引入Eureka服务端的依赖包。

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    Eureka的主启动类添加注解@EnableEurekaServer
    在这里插入图片描述

    配置Eureka端口号及地址

    在application.yml中添加如下配置:

    server:
      port: 7001
    
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false   # 表示不向注册中心注册自己
        fetch-registry: false
        service-url:
          defaultZone: http://localhost:7001/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    启动Eureka服务并访问

    启动Eureka服务并访问:http://localhost:7001/进入Eureka主页
    在这里插入图片描述

    引入Eureka Client包并开启注解

    在两个微服务的pom.xml中引入Eureka的客户端依赖包

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    然后在两个微服务的主启动类上添加注解 @EnableEurekaClient
    在这里插入图片描述

    添加注册配置

    在提供者和消费者微服务的配置文件中添加配置:

    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
           defaultZone: http://localhost:7001/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    启动微服务并访问Eureka主页查看

    启动提供者和消费者微服务,并重新访问http://localhost:7001/
    在这里插入图片描述
    注册成功!!!

    服务发现

    对于注册进Eureka里面的服务,可以通过Discovery来获得这些服务的信息。

    获取DiscoveryClient

    @Autowired
    private DiscoveryClient discoveryClient;
    
    • 1
    • 2

    获取服务列表

    List<String> services = discoveryClient.getServices();
    for(String service:services){
        System.out.println(service);
    }
    
    • 1
    • 2
    • 3
    • 4

    根据服务获取实例列表

    List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    for (ServiceInstance instance : instances) {
        System.out.println(instance.getInstanceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
    }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    Zookeeper服务注册

    添加依赖包

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
            exclusion>
        exclusions>
    dependency>
    <dependency>
        <groupId>org.apache.zookeepergroupId>
        <artifactId>zookeeperartifactId>
        <version>3.4.9version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    添加配置

    spring:
      cloud:
        zookeeper:
          connect-string: 192.168.83.129:2181
    
    • 1
    • 2
    • 3
    • 4

    主启动类注解

    在这里插入图片描述

    @SpringBootApplication
    @EnableDiscoveryClient
    public class PaymentMain8004 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8004.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Consul服务注册

    添加依赖包

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-consul-discoveryartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    添加配置

    spring:
      application:
        name: consul-provider-payment
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    主启动类注解

    在这里插入图片描述

    @SpringBootApplication
    @EnableDiscoveryClient
    public class PaymentMain8006 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8006.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3个注册中心的比较

    实现语言CAP服务健康检查接口
    EurekaJavaAP可配支持HTTP
    ConsulGoCP支持HTTP/DNS
    ZookeeperJavaCP支持客户端
  • 相关阅读:
    Linux 权限管理
    缓冲区溢出漏洞预防
    30岁了,还能转行做测试/开发程序员吗?下定决心开始干......
    一文精通C++ -- 继承
    乳制品行业如何通过APS智能排产进行生产管理的优化?
    本机Consul注册为服务并开机自启动
    Centos 8 stream x64设置交换空间
    数字全息干涉重建算法研究
    DBSCAN聚类算法实用案例
    实现领域驱动设计 - 使用ABP框架 - 存储库
  • 原文地址:https://blog.csdn.net/weixin_43598687/article/details/125777028