纵横研究院微服务&容器专题社区SpringCloudSpringCloud

服务注册和发现 Eureka

2019-04-11  本文已影响7人  董二弯

在前两章介绍了微服务简介(https://www.jianshu.com/p/b98f75722eec)
spring-cloud简介(https://www.jianshu.com/p/c996cadfaa66)
这一章学习spring cloud中的第一个组件,Eureka。这是一个用于服务注册和发现的组件。分为Eureka Server和Eureka Client,Eureka Server为Eureka服务注册中心,Eureka Client为Eureka客户端。

基本架构

Eureka主要包括三种角色:
Register Service:服务注册中心,他是一个Eureka Server,提供服务注册和发现的功能。
Provider Service:服务提供者,它是一个Eureka Client,提供服务。
Consumer Service:服务消费者,它是一个Eureka Client,消费服务。

服务消费的基本流程

首先需要启动一个Eureka Server,做为服务注册中心,服务提供者Eureka Client向服务注册中心Eureka Server注册,将自己的服务名和IP地址等信息通过REST API的形式提交给服务注册中心Eureka Server。同样,服务消费者Eureka Client也向服务注册中心Eureka Server注册,同时服务消费者获取一份服务注册列表的信息,该列表包含了所有向服务注册中心Eureka Server注册的服务信息。获取服务注册列表信息之后,服务消费者就知道服务提供者的IP地址,可以通过Http远程调度来消费服务提供者的服务。

编写Eureka Server

第一步:引入Eureka Server的起步依赖spring-cloud-starter-eureka-server,以及spring boot测试的起步依赖spring-boot-starter-test。最后引入spring boot的maven插件spring-boot-maven-plugin,该插件的作用是可以试用maven插件的方式来启动spring boot工程。

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

第二步:在application.yml中做程序的相关配置。Eureka Server会向自己注册,这时需要配置eureka.client.registerWithEureka和eureka.client.fetchRegistry为false,防止自己注册自己。配置如下:

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

最后在工程启用类加上注解@EnableEurekaServer,开启Eureka Server的功能。代码如下:

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

启动项目,访问http://localhost:8761,可以在浏览器查看Eureka Server的主界面。在界面Instance currently registered with Eureka这一项上没有任何注册的实列,接下来编写Eureka Client,并注册到Eureka Server。

编写Euraka Client

引入依赖:

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在工程配置文件application.yml中做相关配置。其中defaultZone为默认的Zone,来源于AWS的概念。区域(Region)和可用区(Availability Zone,AZ)是AWS的另外两个概念。区域是指服务器所在的区域,比如北美洲、欧洲、亚洲等,每个区域一般由多个可用区组成。在配置中defaultZone是指Eureka Server的注册地址。

server:
  port: 8763
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

在项目启动类上加上注解@EnableEurekaClient开启Eureka Client功能。代码如下:

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

启动Client,启动成功后,控制台会打印出如下信息:

 DiscoveryClient_EUREKA-CLIENT/DESKTOP-NB1C2U4:eureka-client:8763 - registration status: 204

说明已经向Eureka Server注册了。在浏览器访问http://localhost:8761,可在主界面看到有一个实列注册,Application为EUREKA-CLIENT,Status为UP,端口为8763,说明注册成功。

eureka-server.png

构建高可用的Eureka Server集群

在实际的项目中,可能由几十个或者几百个的微服务实例,这时Eureka Server承担了非常高的负载。由于Eureka Server在微服务架构中有着举足轻重的作用,所以需要对Eureka Server进行高可用集群。

第一步更改eureka-server的配置文件application.yml,在配置文件中采用多profile的格式,具体代码如下:

spring:
  profiles: peer1
server:
  port: 8761
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/
---
spring:
  profiles: peer2
server:
  port: 8762
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://peer1:8761/eureka/

由于是本地代建Eureka Server集群,需要在本机hosts文件做域名映射。

127.0.0.1 peer1
127.0.0.1 peer2

通过mvn clean package编译项目,成功后在目录target文件夹下生成jar包。通过java -jar命令并指定spring-profiles-active参数。命令如下:

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

修改eureka-client配置文件,注意其中只是向8761端口的服务器注册,配置如下:

server:
  port: 8763
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/

启动eureka-client,访问 http://localhost:8762/,在DS Replicas选项中显示了其它多实例节点。此时发现eureka-client在配置文件中只是向8761端口的server注册,但在8762端口的服务器同样可以看到eureka-client的注册信息,可见peer1的注册列表信息已经同步到了peer2节点。

eureka-server-two.png

Eureka的一些概念

DiscoveryManager.getInstance().shutdownComponent();
eureka:
    server:
        enable-self-preservation: false

源码解析

接下来我们通过debug的形式,学习Eureka Client是如何进行注册的。在工程的Maven的依赖包下,找到eureka-client-1.6.2.jar包,在com.netflix.discovery包下有一个DiscoverClient类,这个类是Eureka Client和Eureka Server交互的核心类。
启动项目后首先会进入DiscoverClient的initScheduledTasks方法。在该方法中初始化并封装了刷新服务注册列表信息和发送心跳的定时任务。


eureka-init.png

在初始化定时任务后进入DiscoverClient的register()方法


eureka-register.png
具体进入EurekaHttpClient的register方法,可以看出通过http请求的Eureka Server路径,以及携带的请求信息。
eureka-register2.png
同时来跟踪Eureka Server端的代码,Eureka server服务端请求入口为

ApplicationResource类,如下所示,可以看出Eureka Client是通过http post的方式去服务注册。

server-rk.png

通过一系列的参数校验。最后以以下方法进行注册。

 registry.register(info, "true".equals(isReplication));

进入register方法,实际执行的是PeerAwareInstanceRegistryImpl的register方法,该方法如下:

 @Override
    public void register(final InstanceInfo info, final boolean isReplication) {
        int leaseDuration = Lease.DEFAULT_DURATION_IN_SECS;
        if (info.getLeaseInfo() != null && info.getLeaseInfo().getDurationInSecs() > 0) {
            leaseDuration = info.getLeaseInfo().getDurationInSecs();
        }
       //调用父类的注册方法
        super.register(info, leaseDuration, isReplication);
       // 注册成功后同步Eureka中的服务注册信息
        replicateToPeers(Action.Register, info.getAppName(), info.getId(), info, null, isReplication);
    }

进入父类的注册方法,在AbstractInstanceRegistry类中可以看到Eureka真正的服务注册实现的代码。

 public void register(InstanceInfo registrant, int leaseDuration, boolean isReplication) {
        try {
            read.lock();
            Map<String, Lease<InstanceInfo>> gMap = registry.get(registrant.getAppName());
            REGISTER.increment(isReplication);
            if (gMap == null) {
                final ConcurrentHashMap<String, Lease<InstanceInfo>> gNewMap = new ConcurrentHashMap<String, Lease<InstanceInfo>>();
                gMap = registry.putIfAbsent(registrant.getAppName(), gNewMap);
                if (gMap == null) {
                    gMap = gNewMap;
                }
            }
            Lease<InstanceInfo> existingLease = gMap.get(registrant.getId());
            // Retain the last dirty timestamp without overwriting it, if there is already a lease
            if (existingLease != null && (existingLease.getHolder() != null)) {
                Long existingLastDirtyTimestamp = existingLease.getHolder().getLastDirtyTimestamp();
                Long registrationLastDirtyTimestamp = registrant.getLastDirtyTimestamp();
                logger.debug("Existing lease found (existing={}, provided={}", existingLastDirtyTimestamp, registrationLastDirtyTimestamp);
                if (existingLastDirtyTimestamp > registrationLastDirtyTimestamp) {
                    logger.warn("There is an existing lease and the existing lease's dirty timestamp {} is greater" +
                            " than the one that is being registered {}", existingLastDirtyTimestamp, registrationLastDirtyTimestamp);
                    logger.warn("Using the existing instanceInfo instead of the new instanceInfo as the registrant");
                    registrant = existingLease.getHolder();
                }
            } else {
                // The lease does not exist and hence it is a new registration
                synchronized (lock) {
                    if (this.expectedNumberOfRenewsPerMin > 0) {
                        // Since the client wants to cancel it, reduce the threshold
                        // (1
                        // for 30 seconds, 2 for a minute)
                        this.expectedNumberOfRenewsPerMin = this.expectedNumberOfRenewsPerMin + 2;
                        this.numberOfRenewsPerMinThreshold =
                                (int) (this.expectedNumberOfRenewsPerMin * serverConfig.getRenewalPercentThreshold());
                    }
                }
                logger.debug("No previous lease information found; it is new registration");
            }
            Lease<InstanceInfo> lease = new Lease<InstanceInfo>(registrant, leaseDuration);
            if (existingLease != null) {
                lease.setServiceUpTimestamp(existingLease.getServiceUpTimestamp());
            }
            gMap.put(registrant.getId(), lease);
            synchronized (recentRegisteredQueue) {
                recentRegisteredQueue.add(new Pair<Long, String>(
                        System.currentTimeMillis(),
                        registrant.getAppName() + "(" + registrant.getId() + ")"));
            }
            // This is where the initial state transfer of overridden status happens
            if (!InstanceStatus.UNKNOWN.equals(registrant.getOverriddenStatus())) {
                logger.debug("Found overridden status {} for instance {}. Checking to see if needs to be add to the "
                                + "overrides", registrant.getOverriddenStatus(), registrant.getId());
                if (!overriddenInstanceStatusMap.containsKey(registrant.getId())) {
                    logger.info("Not found overridden id {} and hence adding it", registrant.getId());
                    overriddenInstanceStatusMap.put(registrant.getId(), registrant.getOverriddenStatus());
                }
            }
            InstanceStatus overriddenStatusFromMap = overriddenInstanceStatusMap.get(registrant.getId());
            if (overriddenStatusFromMap != null) {
                logger.info("Storing overridden status {} from map", overriddenStatusFromMap);
                registrant.setOverriddenStatus(overriddenStatusFromMap);
            }

            // Set the status based on the overridden status rules
            InstanceStatus overriddenInstanceStatus = getOverriddenInstanceStatus(registrant, existingLease, isReplication);
            registrant.setStatusWithoutDirty(overriddenInstanceStatus);

            // If the lease is registered with UP status, set lease service up timestamp
            if (InstanceStatus.UP.equals(registrant.getStatus())) {
                lease.serviceUp();
            }
            registrant.setActionType(ActionType.ADDED);
            recentlyChangedQueue.add(new RecentlyChangedItem(lease));
            registrant.setLastUpdatedTimestamp();
            invalidateCache(registrant.getAppName(), registrant.getVIPAddress(), registrant.getSecureVipAddress());
            logger.info("Registered instance {}/{} with status {} (replication={})",
                    registrant.getAppName(), registrant.getId(), registrant.getStatus(), isReplication);
        } finally {
            read.unlock();
        }
    }

在该类中服务注册信息其实就是存储在一个 ConcurrentHashMap结构中。

 private final ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> registry
            = new ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>();

流程总结:
Eureka Client:在DiscoveryClient先通过initScheduledTasks()方法封装定时任务,然后调用register()方法通过http访问服务器接口进行注册。
Eureka Server:ApplicationResource类接收Http服务请求,调用PeerAwareInstanceRegistryImpl的register方法,PeerAwareInstanceRegistryImpl完成服务注册后,调用replicateToPeers向其它Eureka Server节点(Peer)做状态同步。

在以上只是对服务注册进行了源码跟踪,感兴趣的可以以同样的方式对服务续约、服务剔除等进行源码学习。

总结

在这一章节中,我们学习了Eureka的概念、架构。如何编写Eureka Server和Eureka Client,并进行了Eureka Server高可用的演示。最后以服务注册为列进行了源码学习。在下一章学习如何使用RestTemplate和Ribbon结合作为服务消费者去消费服务。

PS:项目github地址:https://github.com/dzydzydzy/spring-cloud-example.git

上一篇 下一篇

猜你喜欢

热点阅读