zoukankan      html  css  js  c++  java
  • (一)Eureka 服务的注册与发现

    (一)服务的注册于发现(eureka);

    Eureka Server: 服务注册中心,负责服务列表的注册、维护和查询等功能

    在Idea里,新建项目,选择Spring initializer.

    下面的pom

    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    

    配置properties文件参数;

    server.port=8882
    
    #域名
    eureka.instance.hostname=localhost
    
    #禁用 Eureka的客户端注册行为
    eureka.client.register-with-eureka=false
    
    eureka.client.fetch-registry=false
    
    #eureka注册中心服务地址
    eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    

    在启动类上添加注解@EnableEurekaServer

    // @EnableEurekaServer 代表启动注册服务中心
    @EnableEurekaServer
    @SpringBootApplication
    public class SpringCloundEurekaDemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringCloundEurekaDemoApplication.class, args);
    	}
    }
    

      

    启动项目,打开连接  http://localhost:8882

     二.创建一个服务提供者(Eureka-client)

    服务提供方,同时也是一个Eureka Client,负责将所提供的服务向Eureka Server进行注册、续约和注销等操作。注册时所提供的主要数据包括服务名、机器ip、端口号、域名等,从而能够使服务消费方能够找到

    Eureka服务器我们已经编写好了,接下来我们就可以编写一个Eureka的客户端了。这个客户端可能是一个服务提供者,也可能是一个服务消费者,甚至两者都是。

    我们先编写一个简单的Eureka Client

    在Idea里,新建项目,选择Spring initializ.

    下面的pom

    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    			<version>1.4.4.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    

    配置yml文件参数;(换下配置方式)

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8882/eureka/
    server:
      port: 8883
    spring:
      application:
        name: service-hello
    

      

    在启动类上添加注解@EnableEurekaClient

    // @ EnableEurekaClient   表示申明自己是一个发服务提供者;
    @EnableEurekaClient
    @SpringBootApplication
    public class SpringCloundEurekaClientExampleApplication {
    
       public static void main(String[] args) {
          SpringApplication.run(SpringCloundEurekaClientExampleApplication.class, args);
       }
    }
    

      

      

    创建conroller

    @RestController
    public class HelloController {
        @Value("${server.port}")
        String port;
    
        @Value("${spring.application.name}")
        String name;
    
        @RequestMapping("/index")
        public String index() {
            return "服务提供者client:" + name + "服务端口:" + port;
        }
    }
    

      

      

    启动项目

    然后我们再来看一下服务注册中心;就会看到,已经注册了一个服务提供者;

    为了接下来测试Ribbon负载

    我们将client的配置文件属性注册的端口改为8884;;然后在IDEA把该服务再启动一个实例

    (把single instance only勾选去掉)

    回到服务中心,已经有两个client了

    此外

    Spring Cloud实现心跳监测,在服务注册和停止时,注册中心能得到通知,并更新服务实例列表 

    (一)Spring Cloud注册中心添加配置: 

    eureka.server.enable-self-preservation=false 

    eureka.server.enable-self-preservation

    默认情况下,如果Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。

    Eureka通过“自我保护模式”来解决这个问题——当Eureka Server节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,Eureka Server就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。

    综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。

    eureka.server.eviction-interval-timer-in-ms

    eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒


    (二)Spring Cloud服务提供者添加配置: 

    eureka.instance.lease-renewal-interval-in-seconds=5 
    eureka.instance.lease-expiration-duration-in-seconds=5

    eureka.client.registry-fetch-interval-seconds

    表示eureka client间隔多久去拉取服务注册信息,默认为30秒,对于api-gateway,如果要迅速获取服务注册状态,可以缩小该值,比如5秒

    eureka.instance.lease-expiration-duration-in-seconds

    leaseExpirationDurationInSeconds,表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。

    • 默认为90秒
    • 如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。
    • 如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。
    • 该值至少应该大于leaseRenewalIntervalInSeconds

    eureka.instance.lease-renewal-interval-in-seconds

    leaseRenewalIntervalInSeconds,表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。

    • 默认30秒

    所属文章参考:https://www.jianshu.com/u/8f959a9cbc66。https://blog.csdn.net/qq_41377914/article/category/7719803

  • 相关阅读:
    985的方格难题
    POJ 3264 区间最大最小值Sparse_Table算法
    oracle中to_date详细用法示例(oracle日期格式转换)
    PLSQL基础知识-图片
    oracle-查询-时间条件查询
    oracle基础函数--decode
    PLSQL基础学习-文字
    python3 MD5
    CentOS7关闭防火墙方法
    CentOS 7下源码安装MySQL 5.6
  • 原文地址:https://www.cnblogs.com/skyLogin/p/10002472.html
Copyright © 2011-2022 走看看