Spring Cloud 微服务之Eureka(四)

22 篇文章 1 订阅
18 篇文章 0 订阅

一、什么是Spring Cloud Eureka?

  • Spring Cloud Eureka 是 Spring Cloud Netflix 子项目的核心组件之一,主要用于微服务架构中的服务治理。
  • 在微服务架构中往往会有一个注册中心,每个微服务都会向注册中心去注册自己的地址及端口信息,注册中心维护着服务名称与服务实例的对应关系。
  • 每个微服务都会定时从注册中心获取服务列表,同时汇报自己的运行情况,这样当有的服务需要调用其他服务时,就可以从自己获取到的服务列表中获取实例地址进行调用,Eureka实现了这套服务注册与发现机制。

二、Eureka服务的自我保护机制

  • 默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka之间无法正常通行,以上行为可能变得非常危险了—因为微服务本身其实是健康的,此时本不应该注销这个服务。Eureka通过自我保护机制来解决这个问题—当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该EurekaServer节点会自动退出自我保护模式。
  • 自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮和稳定。

三、Eureka服务端示例

  • 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
  • 添加配置

    启动类

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

    application.yml文件

    server.port=8081
    server.servlet.context-path=/
    
    spring.application.name=eureka-server
    
    # Eureka服务端的主机名称
    eureka.instance.hostname=localhost
    # 指定是否要从注册中心获取服务
    eureka.client.fetch-registry=false
    # 指定是否要注册到注册中心
    eureka.client.register-with-eureka=true 
    # 设置注册中心的服务地址
    eureka.client.service-url.defaultZone: 
    						http://${eureka.instance.hostname}:${server.port}/eureka/
    # 是否开启保护模式
    eureka.server.enable-self-preservation=false
    
  • 测试使用

    访问:http://localhost:8081/
    

    在这里插入图片描述

四、Eureka客户端示例

  • 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  • 添加配置

    启动类

    @EnableDiscoveryClient // 服务发现
    @EnableEurekaClient // 服务注册
    @SpringBootApplication
    public class EurekaClientApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    }
    

    application.yml文件

    server.port=8082
    server.servlet.context-path=/
    
    spring.application.name=eureka-client
    
    # Eureka客户端的名称
    eureka.instance.instance-id=eureka-client-8082
    # 指定是否显示Eureka客户端的IP地址(默认localhost)
    eureka.instance.prefer-ip-address=true
    # 指定是否要从注册中心获取服务
    eureka.client.fetch-registry=true
    # 指定是否要注册到注册中心
    eureka.client.register-with-eureka=true 
    # 注册中心服务地址
    eureka.client.service-url.defaultZone: http://localhost:8081/eureka/
    
    # Eureka客户端简述信息(需要开启SpringBoot的监控):http://localhost:8081/actuator/info
    info.app.name: myspringcloud
    info.company.name: blog.prochick.com
    info.author.name: prochick
    
  • 服务发现

    @Autowired
    private DiscoveryClient discoveryClient;
    
    @GetMapping("/discovery")
    public Object discovery(){
        //获取微服务列表的清单
        List<String> services = discoveryClient.getServices();
        System.out.println(services);
        //得到一个具体的微服务信息,通过具体的微服务id,ApplicationName
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRING-CLOUD-
                                                                       PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                instance.getHost()+"\t"+ 
                instance.getPort()+"\t"+ 
                instance.getUri()+"\t"+ 
                instance.getServiceId()
            );
        }
                                                                       
        return this.discoveryClient;
    }
    
  • 测试使用

    访问:http://localhost:8081/
    

    在这里插入图片描述

五、Eureka的服务集群

  • 服务端添加配置

    • 第一个
    server.port=9091
    server.servlet.context-path=/
    
    spring.application.name=eureka-server1
    
    # Eureka服务端的主机名称
    eureka.instance.hostname=eureka9091.com
    # 指定是否要从注册中心获取服务
    eureka.client.fetch-registry=false
    # 指定是否要注册到注册中心
    eureka.client.register-with-eureka=true 
    # 设置注册中心的服务地址
    eureka.client.service-url.defaultZone: http://eureka9091.com:9091/eureka
    
    • 第二个
    server.port=9092
    server.servlet.context-path=/
    
    spring.application.name=eureka-server2
    
    # Eureka服务端的主机名称
    eureka.instance.hostname=eureka9092.com
    # 指定是否要从注册中心获取服务
    eureka.client.fetch-registry=false
    # 指定是否要注册到注册中心
    eureka.client.register-with-eureka=true 
    # 设置注册中心的服务地址
    eureka.client.service-url.defaultZone: http://eureka9092.com:9092/eureka
    
  • 客户端添加配置

    server.port=8081
    server.servlet.context-path=/
    
    spring.application.name=eureka-client
    
    # Eureka客户端的名称
    eureka.instance.instance-id=eureka-client-8081
    # 指定是否要从注册中心获取服务
    eureka.client.fetch-registry=true
    # 指定是否要注册到注册中心
    eureka.client.register-with-eureka=true 
    # 注册中心服务地址
    eureka.client.service-url.defaultZone: 
    			http://eureka9091.com:9091/eureka,http://eureka9092.com:9092/eureka
    
  • 测试使用

    访问:http://eureka9092.com:9092
    访问:http://eureka9091.com:9091
    

    在这里插入图片描述

六、Eureka的服务认证

  • 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
  • 服务端添加配置

    启动类

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

    application.yml文件

    server.port=8081
    server.servlet.context-path=/
    spring.application.name=eureka-server
    
    # 配置访问用户名和密码
    spring.security.user.name=eureka
    spring.security.user.password=123456
    
    # Eureka服务端的主机名称
    eureka.instance.hostname=localhost
    # 指定是否要从注册中心获取服务
    eureka.client.fetch-registry=false
    # 指定是否要注册到注册中心
    eureka.client.register-with-eureka=true 
    

    添加CSRF配置

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().ignoringAntMatchers("/eureka/**");
            super.configure(http);
        }
    }
    
  • 客户端添加配置

    application.yml文件

    server.port=8080
    server.servlet.context-path=/
    spring.application.name=eureka-client
    
    # Eureka
    eureka.instance.instance-id=eureka-client-8081
    eureka.client.fetch-registry=true
    eureka.client.register-with-eureka=true 
    eureka.client.service-url.defaultZone: http://eureka:123456@localhost:8081/eureka/
    
  • 测试使用

    访问:http://localhost:8081/
    

七、Eureka与Zookeeper对比

著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A (可用性)、P(容错性)。而由于分区容错性P在分布式系统中是必须要保证的,因此我们只能在A和C之间进行权衡。

  • Zookeeper 保证的是CP原则

    • 当向注册中心查询服务列表时,我们可以容忍注册中心返回的是几分钟以前的注册信息,但不能接受服务直接宕机不可用。
    • 也就是说,服务注册功能对可用性的要求要高于一致性。但是zk会出现这样一种情况,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行Leader选举。
    • 在云部署的环境下,因为选举时长可能在30~120s,且选举期间整个Zookeeper集群都是不可用的,这就导致在选举期间注册服务瘫痪。
    • 在云部署的环境下,因为网络问题使得Zookeeper集群失去master节点是较大概率会发生的事件,虽然服务最终能够恢复,但是漫长的选举时间导致的注册长期不可用是不能容忍的。
  • Eureka 保证的是AP原则

    • Eureka 各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。

    • 而Eureka的客户端在向某个Eureka注册时,如果发现连接失败,则会自动切换至其他节点,只要有一台Eureka服务节点还在,就能保住注册服务的可用性,只不过查到的信息可能不是最新的。

    • 除此之外,Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没

      有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现以下几种情况:

      • Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
      • Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其他节点上
      • 当网络稳定时,当前实例新的注册信息会被同步到其他节点中
    • Eureka 可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪。


【源码地址】:GitHub

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小吉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值