0407-服务注册与发现-Eureka深入理解-元数据、高可用HA

一、Eureka元数据

参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_eureka_metadata_for_instances_and_clients

  了解Eureka元数据的工作原理,以便您可以在平台中使用它。有主机名,IP地址,端口号,状态页和运行状况检查等标准元数据。这些信息发布在服务注册中心,并由客户用于直接联系服务。额外的元数据可以添加到eureka.instance.metadataMap中的实例注册中,并且可以在远程客户端访问,但通常不会改变客户端的行为,除非它意识到元数据的含义。Spring Cloud已经为元数据映射赋予了含义,下面介绍了一些特殊情况。

1.1、改变Eureka 实例Id

application.yml. 

eureka:
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

或者个人使用:    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

1.2、自定元数据  

在providor中提供:microservice-provider-user

eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
    metadata-map:
    zone: ABC # zone默认带的 eureka可以理解的数据
    lihongxu: BBC # 自定义的 不会影响客户端行为

可在eureka中查看:http://localhost:8761/eureka/apps/microservice-provider-user

可以看到其中manager.port已经影响实际接口,但自定义无影响

二、为甚么注册一个服务比较慢

  作为一个实例还涉及到注册表的周期性心跳(通过客户端的serviceUrl),默认持续时间为30秒。服务不可用于客户端发现,直到实例,服务器和客户端在其本地缓存中都具有相同的元数据(因此可能需要3次检测信号)。您可以使用eureka.instance.leaseRenewalIntervalInSeconds更改期限,这将加快获取客户端连接到其他服务的过程。在生产中,坚持使用默认值可能会更好,因为在服务器内部有一些计算可以对租期更新进行假设。

三、Eureka高可用,zones,regions

  Eureka服务器没有后端存储,但注册表中的服务实例必须发送心跳信号以保持其注册是最新的(所以这可以在内存中完成)。客户端还拥有一个eureka注册的内存缓存(因此,他们不必为每个服务请求都去注册表)。默认情况下,每个Eureka服务器也是Eureka客户端,并且需要(至少一个)服务URL来定位对等端。如果您不提供该服务,该服务将运行并正常工作,但它会给您的日志带来很多无法注册的噪音。

3.1、单机模式

前几节说的就是单机模式

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.2、高可用

  通过运行多个实例并要求它们相互注册,Eureka可以变得更加灵活和可用。事实上,这是默认的行为,所以你需要做的只是为对等体添加一个有效的serviceUrl。

application.yml (Two Peer Aware Eureka Servers). 

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/

示例:

配置启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaHaApplication {
    public static void main(String[] args) {
//        SpringApplication.run(EurekaApplication.class, args);
        // 读取控制台输入,决定使用哪个profiles
        System.out.println("输入配置:");
        Scanner scan = new Scanner(System.in);
        String profiles = scan.nextLine();
        new SpringApplicationBuilder(EurekaHaApplication.class).profiles(profiles).run(args);
    }
}

application.yml配置文件:

spring:
  application:
    name: EUREKA-HA
---    
# 启动类配置启动哪个   
server:
  port: 8761
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/
      
---
server:
  port: 8762
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/
      
---
server:
  port: 8763
spring:
  profiles: peer3
eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
     defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

实际启动时,配置文件中用三个”-“分隔两种配置即可,三次分别输入:peer1,peer2,peer3,便可分别按照配置启动。

服务提供者配置其中任何一个集群节点即可,建议多配置两个

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

3.3、实例

示例一、单例不使用security

示例二、单例使用security

示例三、集群HA不使用security

示例四、集群HA使用security

  参看地址:https://github.com/bjlhx15/spring-cloud-base.git

3.4、注意事项

  1、集群时:eureka.client.serviceUrl.defaultZone配置项的地址,不能使用localhost,要使用域名,DNS解析请自行配置【配置host】。 

  2、集群时:spring.application.name 要一致,多个应用

  3、一下配置

# 单点注册中心的时候,将这两个配置项设为false,分布式注册中心true
# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。
#   由于当前这个应用就是Eureka Server,故而设为false
# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。
#   因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
#   默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

  4、实例名

# ha 3、区别配置 同 defaultZone一致
eureka.instance.hostname=peer2

  5、ip设置【集群可用性有问题】

# ha 4、去掉下面这个参数或者改为false
eureka.instance.preferIpAddress=false
原文地址:https://www.cnblogs.com/bjlhx/p/8900454.html