Spring Cloud Eureka service registry (II)

Preface

Eureka  is  Netflix  developed a REST-based services, component services of registration and discovery

It mainly consists of two components: Eureka Server and Eureka Client

  • Eureka Client: a Java client to simplify the interaction with Eureka Server (usually the client and server micro-service)
  • Eureka Server: the ability to provide service registry and discovery (usually the micro registry service)

When each micro service starts, it will register themselves by Eureka Client to Eureka Server, Eureka Server stores the service information

In other words, the client and server each micro services will register to Eureka Server, which spawned a micro-service mutual recognition topic

  • Synchronization: Eureka Client also (logically) Eureka Server each
       complete registry between a plurality of services by way of Eureka Server replication synchronization, high availability formed Eureka
  • Recognition: Eureka Client caches the information in the Eureka Server
       even if all Eureka Server nodes shoot down, service consumers can still use the information cache find the service provider (the author has been pro-test)
  • Renewal: micro-services periodically (default 30s) sent to Eureka Server heartbeat to Renew (renewal) information (like heartbeat)
  • Renewal: Eureka Server periodically (default 60s) performs a failure detection service
       that checks over a certain time (default 90s) did not Renew micro-services, service discovery will cancel the micro node

Spring Cloud has been integrated in the Eureka Spring Cloud Netflix inside its subprojects

Best practices for Eureka configuration, refer to: https://github.com/spring-cloud/spring-cloud-netflix/issues/203

More description, refer to: http://cloud.spring.io/spring-cloud-static/Camden.SR4/#spring-cloud-eureka-server

Eureka Server Configuration deploy a single instance

pom.xml 

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注册中心,启动类。

@SpringBootApplication
//创建服务注册中心
@EnableEurekaServer
public class StartMain {
    public static void main(String[] args) {
        SpringApplication.run(StartMain.class, args);
    }
}

注册中心配置文件

## server
server.port=8081

##eureka
#指定环境
eureka.environment=work
# 设置是否将自己作为客户端注册到注册中心(缺省true)
# 这里为不需要(查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient)
eureka.client.register-with-eureka=false
# 设置是否从注册中心获取注册信息(缺省true)
# 因为这是一个单点的EurekaServer,不需要同步其它EurekaServer节点的数据,故设为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#是否开启自我保护模式,默认为true。
eureka.server.enable-self-preservation=true
#续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
eureka.server.eviction-interval-timer-in-ms=10000

zone

上面提到 serviceUrl,那就顺便说下 defaultZone

Eureka 有一个 Region 和 Zone 的概念,你可以理解为现实中的大区(Region)和机房(Zone)

Eureka Client 在启动时需要指定 Zone,它会优先请求自己 Zone 的 Eureka Server 获取注册列表

同样的,Eureka Server 在启动时也需要指定 Zone,如果没有指定的话,其会默认使用 defaultZone

详见源码中的 getEurekaServerServiceUrls() 方法:https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.java

note:

现在启动Eureka Server,没有Eureka Client,过一段时间会出现

     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.

这说明Eureka进入保护模式,可通过上述配置去掉,Eureka的保护模式讲解详见:https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication

Eureka Client配置部署实现

pom文件

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

微服务client,启动类。

@SpringBootApplication
@EnableEurekaClient
public class StartMain {
    public static void main(String[] args) {
        SpringApplication.run(StartMain.class, args);
    }
}

微服务client,配置文件

##eureka
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
eureka.instance.instance-id=${spring.application.name}:${server.port}
# 设置微服务调用地址为IP优先(缺省为false)
eureka.instance.prefer-ip-address=true
# 心跳时间,即服务续约间隔时间(缺省为30s)
eureka.instance.lease-renewal-interval-in-seconds=30
# 发呆时间,即服务续约到期时间(缺省为90s)
eureka.instance.lease-expiration-duration-in-seconds=90

note:

关于续约序言中有讲解,instance-id,是再注册中心页面显示的微服务名。

Eureka 首页显示的微服务名默认为:机器主机名:应用名称:应用端口,也就是:${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}

eureka.client.serviceUrl.defaultZone这个配置可以配置单个注册中心的地址,也可配置多个,逗号隔开

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8000/eureka/,http://127.0.0.1:8081/eureka/

好啦,这样一个注册中心与微服务就完成啦。启动看看吧。

Eureka Server注册中心(高可用)集群部署

Eureka Server 支持运行多实例,并以互相注册的方式(即伙伴机制),来实现高可用的部署

即每一台 Eureka 都在配置中指定另一个 Eureka 或多个 地址作为伙伴,它在启动时会向伙伴节点获取注册列表

详见:http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_peer_awareness

配置demo

与单Server配置稍作改动,如下三个注册中心ABC

A:127.0.0.1:9001

B:127.0.0.1:9002

C:127.0.0.1:9003

A配置: eureka.client.serviceUrl.defaultZone : http://B/eureka/,http://C/eureka/

B配置: eureka.client.serviceUrl.defaultZone : http://A/eureka/,http://C/eureka/

C配置: eureka.client.serviceUrl.defaultZone : http://B/eureka/,http://A/eureka/

ok啦,试试吧。

总结

多看文档,很容易理解的。

Guess you like

Origin www.cnblogs.com/knowledgesea/p/11208000.html