《Spirng Cloud 学习系列 -Eureka 搭建》

213 阅读2分钟

简介

  • Spring Boot版本 2.0.8.RELEASE

  • Spring Cloud 版本 Finchley.SR2

  • Server端

<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>

​ 2: @EnableEurekaServer 添加注解在启动类上.

​ 3: 配置文件:

eureka:
  client:
    service-url:
    	#作为客户端时向其他Eureka注册自己 以达到集群共享的目的
      defaultZone: http://localhost:8761/eureka
        #EnableEurekaServer注解默认包括Client
        #所以这里要进行设置 
      # 设置不注册自身
    register-with-eureka: false
    #读取配置中心数据 默认未tue
    fetch-registry: false
    #配置当前应用的名称
    server:
    	#Eureka的自我保护模式关闭 
    	#当一个服务90s内没有发送心跳 就剔除掉 开启自我保护的话就会进入到自我保护模式  不注销 开发环境可以选择关闭 不太理解啥意思
    	enable-self-preservation: false
spring:
  application:
    name: eureka_server
#当前应用端口
server:
  port: 8761

4: 客户端使用

​ 1:导入依赖 说明 原因是Finchley.SR1版本spring-cloud-starter-netflix-eureka-client里面不在包含spring-boot-starter-web依赖

旧版只有web一种模式,默认使用web。新版还包含webflux,需新增依赖spring-boot-starter-web 或者``spring-boot-starter-webflux

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>

​ 2:添加注解

​ @EnableDiscoveryClient

​ 3配置:

eureka:
  client:
    service-url:
    #集群的情况下 已","隔开 客户端往多个Eureka注册 多个Eureka
    #之间两两相互注册
      defualtZone: http://localhost:8761/eureka
#  instance:配置在EurekaServer上点击连接访问的地址
#    hostname:  helloword
spring:
  application:
    name: client
server:
  port: 9701
  eureka:
  client:
    service-url:
      defaultZone: http://eureka1:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

​ Eureka集群注意事项:

  • ​ 如果使用了Eureka1,eureka2 配置Host这种方式设置集群 时一定要刷新 ipconfig /flushdns 否则回注册不上
  • 列表显示IP 使用IP注册

Eureka的高可用 集群配置

  • ​ 两台Eureka相互注册 以达到资源共享

  • ​ 注意事项:

    eureka集群available-replicas不为空需满足如下条件:

    • 1.不能用localhost比如: eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ 要采用:

      • eureka.instance.hostname=eureka1

      • eureka.client.serviceUrl.defaultZone=http://eureka2:8762/eureka/

      • `eureka.instance.hostname=eureka2

    • eureka.client.serviceUrl.defaultZone=http://eureka1:8761/eureka/`

    这里要注意hostname也要一致

  • spring.application.name 要一直

  • 3.相互注册要开启: eureka.client.register-with-eureka=true eureka.client.fetch-registry=true

  • Instance Status列显示Ip

    • eureka.instance.prefer-ip-address=true 这个可以不开启 这里开启了IP注册 但是使用的hostname 会出现在 unavailable-replicas 这里有坑
    • eureka.instance.instance-id={spring.cloud.client.ip-address}:${server.port}

    spring2.0已经将 spring.cloud.client.ipaddress 修改为spring.cloud.client.ip-address

    相信很多小伙伴在实验的时候都会出现这样一个问题:节点出现在 unavailable-replicas 下。下面我们来说一下可能的几种原因:

    原因一:prefer-ip-address 配置项设置错误

    比如,8761服务器设置了prefer-ip-address: true,那么它注册到 8761和 8762服务器时应该使用 defaultZone:http://yourIP:8761/eureka/ ,但此时可以发现使用的仍然是 hostname 名,导致错误发生。

    另一种原因是,三个8761、8762 都设置了prefer-ip-address: true,导致最后解析出来的 hostname 都是相同的IP,使副本不可用。

    原因二:register-with-eureka 配置项设置错误

    看网上很多博客和资料都把此项设置成了 false,此时 eureka 不会注册到其他服务器上,所以出现错误。

    原因三:其他原因

    还有一些其他原因大家可以参考这里:Eureka高可用,节点均出现在unavailable-replicas下 blog.csdn.net/liupeifeng3…

更改Eureka实例ID

Netflix Eureka实例注册了与其主机名相同的ID(即每个主机只有一个服务)。Spring Cloud Eureka提供了一个明智的默认,如下所示:${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}。例如myhost:myappname:8080

使用Spring Cloud,您可以通过在eureka.instance.instanceId中提供唯一的标识符来覆盖此。例如:

application.yml

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