Browse Source
Use the new ServiceRegistry interface Creates a new EurekaRegistration object that aggregates classes needed for a single registration.pull/6/head
Spencer Gibb
8 years ago
7 changed files with 406 additions and 62 deletions
@ -0,0 +1,154 @@
@@ -0,0 +1,154 @@
|
||||
/* |
||||
* Copyright 2013-2016 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 |
||||
* |
||||
* http://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.concurrent.atomic.AtomicReference; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.aop.framework.Advised; |
||||
import org.springframework.aop.support.AopUtils; |
||||
import org.springframework.cloud.client.serviceregistry.Registration; |
||||
import org.springframework.context.ApplicationEventPublisher; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import com.netflix.appinfo.ApplicationInfoManager; |
||||
import com.netflix.appinfo.HealthCheckHandler; |
||||
import com.netflix.appinfo.InstanceInfo; |
||||
import com.netflix.discovery.EurekaClient; |
||||
import com.netflix.discovery.EurekaClientConfig; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class EurekaRegistration implements Registration { |
||||
private static final Log log = LogFactory.getLog(EurekaRegistration.class); |
||||
|
||||
private final EurekaClient eurekaClient; |
||||
private final AtomicReference<CloudEurekaClient> cloudEurekaClient = new AtomicReference<>(); |
||||
private final CloudEurekaInstanceConfig instanceConfig; |
||||
private final ApplicationInfoManager applicationInfoManager; |
||||
private HealthCheckHandler healthCheckHandler; |
||||
|
||||
private EurekaRegistration(CloudEurekaInstanceConfig instanceConfig, EurekaClient eurekaClient, ApplicationInfoManager applicationInfoManager, HealthCheckHandler healthCheckHandler) { |
||||
this.eurekaClient = eurekaClient; |
||||
this.instanceConfig = instanceConfig; |
||||
this.applicationInfoManager = applicationInfoManager; |
||||
this.healthCheckHandler = healthCheckHandler; |
||||
} |
||||
|
||||
public static Builder builder(CloudEurekaInstanceConfig instanceConfig) { |
||||
return new Builder(instanceConfig); |
||||
} |
||||
|
||||
public static class Builder { |
||||
private final CloudEurekaInstanceConfig instanceConfig; |
||||
private ApplicationInfoManager applicationInfoManager; |
||||
private EurekaClient eurekaClient; |
||||
private HealthCheckHandler healthCheckHandler; |
||||
|
||||
private EurekaClientConfig clientConfig; |
||||
private ApplicationEventPublisher publisher; |
||||
|
||||
Builder(CloudEurekaInstanceConfig instanceConfig) { |
||||
this.instanceConfig = instanceConfig; |
||||
} |
||||
|
||||
public Builder with(ApplicationInfoManager applicationInfoManager) { |
||||
this.applicationInfoManager = applicationInfoManager; |
||||
return this; |
||||
} |
||||
|
||||
public Builder with(EurekaClient eurekaClient) { |
||||
this.eurekaClient = eurekaClient; |
||||
return this; |
||||
} |
||||
|
||||
public Builder with(HealthCheckHandler healthCheckHandler) { |
||||
this.healthCheckHandler = healthCheckHandler; |
||||
return this; |
||||
} |
||||
|
||||
public Builder with(EurekaClientConfig clientConfig, ApplicationEventPublisher publisher) { |
||||
this.clientConfig = clientConfig; |
||||
this.publisher = publisher; |
||||
return this; |
||||
} |
||||
|
||||
public EurekaRegistration build() { |
||||
Assert.notNull(instanceConfig, "instanceConfig may not be null"); |
||||
|
||||
if (this.applicationInfoManager == null) { |
||||
InstanceInfo instanceInfo = new InstanceInfoFactory().create(this.instanceConfig); |
||||
this.applicationInfoManager = new ApplicationInfoManager(this.instanceConfig, instanceInfo); |
||||
} |
||||
if (this.eurekaClient == null) { |
||||
Assert.notNull(this.clientConfig, "if eurekaClient is null, EurekaClientConfig may not be null"); |
||||
Assert.notNull(this.publisher, "if eurekaClient is null, ApplicationEventPublisher may not be null"); |
||||
|
||||
this.eurekaClient = new CloudEurekaClient(this.applicationInfoManager, this.clientConfig, this.publisher); |
||||
} |
||||
return new EurekaRegistration(instanceConfig, eurekaClient, applicationInfoManager, healthCheckHandler); |
||||
} |
||||
|
||||
} |
||||
|
||||
public CloudEurekaClient getEurekaClient() { |
||||
if (this.cloudEurekaClient.get() == null) { |
||||
try { |
||||
this.cloudEurekaClient.compareAndSet(null, getTargetObject(eurekaClient, CloudEurekaClient.class)); |
||||
} catch (Exception e) { |
||||
log.error("error getting CloudEurekaClient", e); |
||||
} |
||||
} |
||||
return this.cloudEurekaClient.get(); |
||||
} |
||||
|
||||
@SuppressWarnings({"unchecked"}) |
||||
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception { |
||||
if (AopUtils.isJdkDynamicProxy(proxy)) { |
||||
return (T) ((Advised) proxy).getTargetSource().getTarget(); |
||||
} else { |
||||
return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
|
||||
} |
||||
} |
||||
|
||||
public CloudEurekaInstanceConfig getInstanceConfig() { |
||||
return instanceConfig; |
||||
} |
||||
|
||||
public ApplicationInfoManager getApplicationInfoManager() { |
||||
return applicationInfoManager; |
||||
} |
||||
|
||||
public HealthCheckHandler getHealthCheckHandler() { |
||||
return healthCheckHandler; |
||||
} |
||||
|
||||
public void setHealthCheckHandler(HealthCheckHandler healthCheckHandler) { |
||||
this.healthCheckHandler = healthCheckHandler; |
||||
} |
||||
|
||||
public void setNonSecurePort(int port) { |
||||
this.instanceConfig.setNonSecurePort(port); |
||||
} |
||||
|
||||
public int getNonSecurePort() { |
||||
return this.instanceConfig.getNonSecurePort(); |
||||
} |
||||
} |
@ -0,0 +1,105 @@
@@ -0,0 +1,105 @@
|
||||
/* |
||||
* Copyright 2013-2016 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 |
||||
* |
||||
* http://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.HashMap; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration; |
||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; |
||||
|
||||
import com.netflix.appinfo.InstanceInfo; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
public class EurekaServiceRegistry implements ServiceRegistry<EurekaRegistration>, AutoServiceRegistration { |
||||
|
||||
private static final Log log = LogFactory.getLog(EurekaServiceRegistry.class); |
||||
|
||||
@Override |
||||
public void register(EurekaRegistration reg) { |
||||
maybeInitializeClient(reg); |
||||
|
||||
if (log.isInfoEnabled()) { |
||||
log.info("Registering application " + reg.getInstanceConfig().getAppname() |
||||
+ " with eureka with status " |
||||
+ reg.getInstanceConfig().getInitialStatus()); |
||||
} |
||||
|
||||
reg.getApplicationInfoManager() |
||||
.setInstanceStatus(reg.getInstanceConfig().getInitialStatus()); |
||||
|
||||
if (reg.getHealthCheckHandler() != null) { |
||||
reg.getEurekaClient().registerHealthCheck(reg.getHealthCheckHandler()); |
||||
} |
||||
} |
||||
|
||||
private void maybeInitializeClient(EurekaRegistration reg) { |
||||
// force initialization of possibly scoped proxies
|
||||
reg.getApplicationInfoManager().getInfo(); |
||||
reg.getEurekaClient().getApplications(); |
||||
} |
||||
|
||||
@Override |
||||
public void deregister(EurekaRegistration reg) { |
||||
if (reg.getApplicationInfoManager().getInfo() != null) { |
||||
|
||||
if (log.isInfoEnabled()) { |
||||
log.info("Unregistering application " + reg.getInstanceConfig().getAppname() |
||||
+ " with eureka with status DOWN"); |
||||
} |
||||
|
||||
reg.getApplicationInfoManager().setInstanceStatus(InstanceInfo.InstanceStatus.DOWN); |
||||
|
||||
//TODO: on deregister or on context shutdown
|
||||
reg.getEurekaClient().shutdown(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void setStatus(EurekaRegistration registration, String status) { |
||||
InstanceInfo info = registration.getApplicationInfoManager().getInfo(); |
||||
|
||||
//TODO: howto deal with delete properly?
|
||||
if ("RESET_OVERRIDE".equalsIgnoreCase(status)) { |
||||
registration.getEurekaClient().cancelOverrideStatus(info); |
||||
return; |
||||
} |
||||
|
||||
//TODO: howto deal with status types across discovery systems?
|
||||
InstanceInfo.InstanceStatus newStatus = InstanceInfo.InstanceStatus.toEnum(status); |
||||
registration.getEurekaClient().setStatus(newStatus, info); |
||||
} |
||||
|
||||
@Override |
||||
public Object getStatus(EurekaRegistration registration) { |
||||
HashMap<String, Object> status = new HashMap<>(); |
||||
|
||||
InstanceInfo info = registration.getApplicationInfoManager().getInfo(); |
||||
status.put("status", info.getStatus().toString()); |
||||
status.put("overriddenStatus", info.getOverriddenStatus().toString()); |
||||
|
||||
return status; |
||||
} |
||||
|
||||
public void close() { |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue