SpringCould-eureka

eureka

eureka是一个服务注册和发现模块

  1. 新建springboot工程,作为eureka服务注册中心,勾选如下选项

    • Cloud Discovery->Eureka Server
  2. Application类上添加注解@EnableEurekaServer声明注册中心

  3. Application.yml配置文件添加内容:

    1
    2
    3
    4
    5
    6
    7
    8
    server:
    port: 8091
    eureka:
    instance:
    hostname: 127.0.0.1 #指定该Eureka实例的主机名,本地默认127.0.0.1,部署docker时再试验
    client:
    register-with-eureka: false #Eureka默认也会作为客户端尝试注册,因此需禁用注册行为
    fetch-registry: false
  4. 访问http://127.0.0.1:8091可以进入管理页面查看注册了那些服务

  5. 重复第一步,作为eureka客户端

  6. Application类上添加注解@EnableEurekaClient声明客户端

  7. Application.yml配置文件添加内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    server:
    port: 8092
    spring:
    application:
    name: eureka-client
    eureka:
    client:
    service-url:
    defaultZone: http://127.0.0.1:8091/eureka/ #注意要加eureka,不然找不到
  8. 再进入eureka服务注册中心就可以看到Application名为eureka-client的客户端

eureka高可用(未实践)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#多节点固定模式,文件名application-peer1.yml
spring:
application:
name: ${APPLICATION_NAME:eureka-center}
server:
port: ${EUREKA_PORT:14031}
eureka:
instance:
hostname: ${EUREKA_HOST:eureka-center-peer1} #指定该Eureka实例的主机名,需要host映射
prefer-ip-address: false
client:
serviceUrl: #高可用
defaultZone: ${EUREKA_CENTER_REG:http://eureka-center-peer2:14032/eureka/}
###----------------第二个eureka注册中心互相注册即可-----------------------------------------
#多节点固定模式,文件名application-peer2.yml
spring:
application:
name: ${APPLICATION_NAME:eureka-center}
server:
port: ${EUREKA_PORT:14032}
eureka:
instance:
hostname: ${EUREKA_HOST:eureka-center-peer2} #指定该Eureka实例的主机名,需要host映射
prefer-ip-address: false
client:
serviceUrl: #高可用
defaultZone: ${EUREKA_CENTER_REG:http://eureka-center-peer1:14031/eureka/}

启动:

java -jar app.jar --spring.profiles.active=peer1

java -jar app.jar --spring.profiles.active=peer2

注意:

  1. 高可用启动,application.name一定要一致
  2. 一定要指定端口号
  3. 不能禁用自我注册,注意配置文件加载顺序及覆盖顺序

健康检查

1
2
3
  compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
//上面的包包含下面的依赖,因此springcould不需要添加该依赖
compile("org.springframework.boot:spring-boot-starter-actuator")

常用链接

1
2
3
4
$ curl localhost:8080/actuator/health
{"status":"UP"}
$ curl localhost:8080/actuator
{"_links":{"self":{"href":"http://127.0.0.1:14031/actuator","templated":false},"health":{"href":"http://127.0.0.1:14031/actuator/health","templated":false},"info":{"href":"http://127.0.0.1:14031/actuator/info","templated":false}}}

dockerfile

1
2
3
4
5
6
7
8
HEALTHCHECK  --interval=5m --timeout=3s \
CMD wget --quiet --tries=1 --spider http://127.0.0.1:14031/actuator/health || exit 1
# --quiet 安静模式
# --tries=1 重试次数
# --spider 不下载任何资料

wget --quiet --tries=1 --spider http://127.0.0.1:14031/actuator/health

Wget命令参数及使用

问题:

1
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

原因据说是

注册中心不是高可用的原因

出处:https://www.cnblogs.com/xiaojf/p/7919088.html

额外

1. 修改eureka client的注册ip,在本地有多个网卡时,默认注册的那个ip可能无法访问,解决指定本地服务ip

1
2
3
4
5
eureka:
instance:
prefer-ip-address: true
ip-address: 10.88.123.151
instance-id: 10.88.123.151:9301