序
要把一个SpringBoot应用注册到Eureka Server或者是从Eureka Server上获取服务列表,主要做了一下两件事:
1.在应用主类中配置@EnableDiscoveryClient注解。
2.在application.yml中配置eureka.client.service-url.defaultZone指定注册中心的位置。
那么@EnableDiscoveryClient背后封装了那些逻辑呢?
@EnableDiscoveryClient源码分析
先进入@EnableDiscoveryClient注解看一下它的源码和注释。
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.client.discovery;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* Annotation to enable a DiscoveryClient implementation.
* @author Spencer Gibb
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public @interface EnableDiscoveryClient {
/**
* If true, the ServiceRegistry will automatically register the local server.
* @return - {@code true} if you want to automatically register.
*/
boolean autoRegister() default true;
}
注释写的很清楚:Annotation to enable a DiscoveryClient implementation.这个注解使用来开启DiscoveryClient这个接口的实现的。
全局搜索一下DiscoveryClient,发现除了接口,还有一个类也叫这个名字。这个类很重要,但是先不看这个类,先看DiscoveryClient接口。DiscoveryClient接口和@EnableDiscoveryClient在一个包里。
DiscoveryClient接口.png
查看DiscoveryClient接口的实现,发现有EurekaDiscoveryClient这个类。看一下EurekaDiscoveryClient这个类的源码。
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.eureka;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.Ordered;
/**
* A {@link DiscoveryClient} implementation for Eureka.
*
* @author Spencer Gibb
* @author Tim Ysewyn
*/
public class EurekaDiscoveryClient implements DiscoveryClient {
/**
* Client description {@link String}.
*/
public static final String DESCRIPTION = "Spring Cloud Eureka Discovery Client";
private final EurekaClient eurekaClient;
private final EurekaClientConfig clientConfig;
@Deprecated
public EurekaDiscoveryClient(EurekaInstanceConfig config, EurekaClient eurekaClient) {
this(eurekaClient, eurekaClient.getEurekaClientConfig());
}
public EurekaDiscoveryClient(EurekaClient eurekaClient,
EurekaClientConfig clientConfig) {
this.clientConfig = clientConfig;
this.eurekaClient = eurekaClient;
}
@Override
public String description() {
return DESCRIPTION;
}
@Override
public List<ServiceInstance> getInstances(String serviceId) {
List<InstanceInfo> infos = this.eurekaClient.getInstancesByVipAddress(serviceId,
false);
List<ServiceInstance> instances = new ArrayList<>();
for (InstanceInfo info : infos) {
instances.add(new EurekaServiceInstance(info));
}
return instances;
}
@Override
public List<String> getServices() {
Applications applications = this.eurekaClient.getApplications();
if (applications == null) {
return Collections.emptyList();
}
List<Application> registered = applications.getRegisteredApplications();
List<String> names = new ArrayList<>();
for (Application app : registered) {
if (app.getInstances().isEmpty()) {
continue;
}
names.add(app.getName().toLowerCase());
}
return names;
}
@Override
public int getOrder() {
return clientConfig instanceof Ordered ? ((Ordered) clientConfig).getOrder()
: DiscoveryClient.DEFAULT_ORDER;
}
/**
* An Eureka-specific {@link ServiceInstance} implementation. Extends
* {@link org.springframework.cloud.netflix.eureka.EurekaServiceInstance} for
* backwards compatibility.
*
* @deprecated In favor of
* {@link org.springframework.cloud.netflix.eureka.EurekaServiceInstance}.
*/
@Deprecated
public static class EurekaServiceInstance
extends org.springframework.cloud.netflix.eureka.EurekaServiceInstance {
public EurekaServiceInstance(InstanceInfo instance) {
super(instance);
}
}
}
EurekaDiscoveryClient这个类自己好像也没干什么,所有的操作几乎都是它的成员变量EurekaClient来实现的。那就再去EurekaClient的源码里看一看。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.netflix.discovery;
import com.google.inject.ImplementedBy;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.HealthCheckCallback;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.discovery.shared.Applications;
import com.netflix.discovery.shared.LookupService;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
@ImplementedBy(DiscoveryClient.class)
public interface EurekaClient extends LookupService {
Applications getApplicationsForARegion(@Nullable String var1);
Applications getApplications(String var1);
List<InstanceInfo> getInstancesByVipAddress(String var1, boolean var2);
List<InstanceInfo> getInstancesByVipAddress(String var1, boolean var2, @Nullable String var3);
List<InstanceInfo> getInstancesByVipAddressAndAppName(String var1, String var2, boolean var3);
Set<String> getAllKnownRegions();
InstanceStatus getInstanceRemoteStatus();
/** @deprecated */
@Deprecated
List<String> getDiscoveryServiceUrls(String var1);
/** @deprecated */
@Deprecated
List<String> getServiceUrlsFromConfig(String var1, boolean var2);
/** @deprecated */
@Deprecated
List<String> getServiceUrlsFromDNS(String var1, boolean var2);
/** @deprecated */
@Deprecated
void registerHealthCheckCallback(HealthCheckCallback var1);
void registerHealthCheck(HealthCheckHandler var1);
void registerEventListener(EurekaEventListener var1);
boolean unregisterEventListener(EurekaEventListener var1);
HealthCheckHandler getHealthCheckHandler();
void shutdown();
EurekaClientConfig getEurekaClientConfig();
ApplicationInfoManager getApplicationInfoManager();
}
我们发现EurekaClient是一个接口,所有操作都是由它的实现类来完成的。那么它的实现类是什么呢?还记得前面说叫DiscoveryClient的除了有一个接口还有一个类么?是的,那个DiscoveryClient就是EurekaClient的实现类。看一下这个类的注解。
/**
* The class that is instrumental for interactions with <tt>Eureka Server</tt>.
*
* <p>
* <tt>Eureka Client</tt> is responsible for a) <em>Registering</em> the
* instance with <tt>Eureka Server</tt> b) <em>Renewal</em>of the lease with
* <tt>Eureka Server</tt> c) <em>Cancellation</em> of the lease from
* <tt>Eureka Server</tt> during shutdown
* <p>
* d) <em>Querying</em> the list of services/instances registered with
* <tt>Eureka Server</tt>
* <p>
*
* <p>
* <tt>Eureka Client</tt> needs a configured list of <tt>Eureka Server</tt>
* {@link java.net.URL}s to talk to.These {@link java.net.URL}s are typically amazon elastic eips
* which do not change. All of the functions defined above fail-over to other
* {@link java.net.URL}s specified in the list in the case of failure.
* </p>
*
* @author Karthik Ranganathan, Greg Kim
* @author Spencer Gibb
*
*/
意思是这个类用于和Eureka Server合作,负责向Eureka Server注册服务实例;向Eureka Server服务续约;当服务关闭时向Eureka Server取消租约;查询Eureka Server中的服务实例列表。最后一句“Eureka Client还需要配置一个Eureka Server列表”,嗯,正好和开头说的“在application.yml中配置eureka.client.service-url.defaultZone指定注册中心的位置”呼应上了。
至于DiscoveryClient这个类里的方法都是怎么实现的,我们下次再看。
网友评论