Java 随笔Spring-BootJava学习笔记

Spring Cloud(二):Eureka

2017-12-16  本文已影响534人  聪明的奇瑞

服务发现概述

Eureka

Eureka 架构

快速入门

编写 Eureka Server (Demo源码 eureka)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
server:
  port: 8761                  #配置端口
eureka:
  client:
    register-with-eureka: false   #是否向服务端注册自己,它本身就是Eureka Server,所以为false
    fetch-registry: false         #表示是否从Eureka Server获取信息,因为这是一个单节点,不需要同步其它Eureka Server的数据,所以为 false
    service-url:
      defaultZone: http://localhost:8761/eureka/    #设置 Eureka Client 与 Eureka Server 同步的地址,注册、查询服务都要使用该地址,多个地址可用逗号分隔

将微服务注册到 Eureka Server

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.1.5.RELEASE</version>
</dependency>
@SpringBootApplication
@EnableEurekaClient
public class FlimUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(FlimUserApplication.class, args);
    }
}
spring:
  application:
    name: flim-user   #注册到Eureka的名字
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/    #设置EurekaServer的服务地址
  instance:
    prefer-ip-address: true   #true代表将自己的IP注册到EurekaServer上,false代表注册主机名到EurekaServer
image

Eureka高可用

构建双结点的 Eureka Server 集群

spring:
  application:
    name: eureka
---
spring:
  profiles: eureka1
eureka:
  instance:
    hostname: eureka1
  client:
    service-url:
      default-zone: http://localhost:8762/eureka
---
spring:
  profiles: eureka2
eureka:
  instance:
    hostname: eureka2
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
java -jar eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka1 --server.port=8761
java -jar eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka2 --server.port=8762
Eureka 高可用

将应用注册到 Eureka Server 集群上

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

Eureka Server 安全

为 Eureka Server 添加用户认证

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
spring:
    security:
      user:
        name: user
        password: 123456

将微服务注册到需要认证的 Eureka Server

eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123456@localhost:8761/eureka/

Eureka 元数据

自定义元数据-服务提供者

eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123456@localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    metadata-map:
      my-metadata: 自定义的元数据

自定义元数据-服务消费者

@RestController
public class MovieController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/user-instance")
    public List<ServiceInstance> showInfo(){
        return this.discoveryClient.getInstances("flim-user");      //获取Flim-User微服务实例的信息
    }
}
[
    {
        "host": "192.168.43.43",
        "port": 8080,
        "metadata": {
            "management.port": "8080",
            "my-metadata": "自定义的元数据"
        },
        "secure": false,
        "serviceId": "FLIM-USER",
        "uri": "http://192.168.43.43:8080",
        "instanceInfo": {
            "instanceId": "linyuandembp:flim-user",
            "app": "FLIM-USER",
            "appGroupName": null,
            "ipAddr": "192.168.43.43",
            "sid": "na",
            "homePageUrl": "http://192.168.43.43:8080/",
            "statusPageUrl": "http://192.168.43.43:8080/info",
            "healthCheckUrl": "http://192.168.43.43:8080/health",
            "secureHealthCheckUrl": null,
            "vipAddress": "flim-user",
            "secureVipAddress": "flim-user",
            "countryId": 1,
            "dataCenterInfo": {
                "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
                "name": "MyOwn"
            },
            "hostName": "192.168.43.43",
            "status": "UP",
            "leaseInfo": {
                "renewalIntervalInSecs": 30,
                "durationInSecs": 90,
                "registrationTimestamp": 1513418181530,
                "lastRenewalTimestamp": 1513418181530,
                "evictionTimestamp": 0,
                "serviceUpTimestamp": 1513418181530
            },
            "isCoordinatingDiscoveryServer": false,
            "metadata": {
                "management.port": "8080",
                "my-metadata": "自定义的元数据"
            },
            "lastUpdatedTimestamp": 1513418181530,
            "lastDirtyTimestamp": 1513418181485,
            "actionType": "ADDED",
            "asgName": null,
            "overriddenStatus": "UNKNOWN"
        }
    }
]

Eureka Server 的 REST 端点

REST 端点集合

Eureka 自我保护模式

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.

多网环境下的IP选择

spring:
  cloud:
    inetutils:
      ignored-interfaces:
        - docker0     #忽略docker0网卡
        - veth.*      #忽略所有veth开头的网卡
spring:
  cloud:
    inetutils:
      preferred-networks:
        - 192.168
        - 10.0      

Eureka 健康检查

Eureka 常见问题

Eureka 注册服务慢

已停止的微服务节点注销慢或不注销

如何自定义微服务的 InstanceID

${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
eureka:
    instance:
        instance-id: ${spring.cloud.client.ipAddress}:${server.port}

Eureka 的UNKNOWN 问题

上一篇下一篇

猜你喜欢

热点阅读