From d3c2f8904973b7e11312339b8edc2ac0ff6e5254 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 2 Jan 2015 07:26:46 +0000 Subject: [PATCH] Add NoopDiscoveryClient and configuration This is so that @EnableDiscoveryClient can be used (e.g. accidentally or transitively via @EnableZuulProxy) without any actual discovery provider on the classpath. Fixes gh-3 --- .../client/discovery/NoopDiscoveryClient.java | 60 +++++++++++ .../NoopDiscoveryClientConfiguration.java | 99 +++++++++++++++++++ src/main/resources/META-INF/spring.factories | 3 +- 3 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java create mode 100644 src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java diff --git a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java new file mode 100644 index 00000000..6bcf8a2a --- /dev/null +++ b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClient.java @@ -0,0 +1,60 @@ +/* + * Copyright 2013-2014 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.client.discovery; + +import java.util.Collections; +import java.util.List; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * @author Dave Syer + * + */ +public class NoopDiscoveryClient implements DiscoveryClient { + + private final ServiceInstance instance; + + public NoopDiscoveryClient(ServiceInstance instance) { + this.instance = instance; + } + + @Override + public String description() { + return "Spring Cloud No-op DiscoveryClient"; + } + + @Override + public ServiceInstance getLocalServiceInstance() { + return instance; + } + + @Override + public List getInstances(String serviceId) { + return Collections.emptyList(); + } + + @Override + public List getAllInstances() { + return Collections.emptyList(); + } + + @Override + public List getServices() { + return Collections.emptyList(); + } + +} diff --git a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java new file mode 100644 index 00000000..88786c6b --- /dev/null +++ b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java @@ -0,0 +1,99 @@ +/* + * Copyright 2013-2014 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.client.discovery; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.context.embedded.EmbeddedServletContainer; +import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.core.env.Environment; + +/** + * @author Dave Syer + * + */ +@Configuration +@EnableConfigurationProperties +@ConditionalOnMissingClass(name = "com.netflix.discovery.EurekaClientConfig") +@ConditionalOnExpression("!${eureka.client.enabled:false}") +public class NoopDiscoveryClientConfiguration implements ApplicationListener { + + private static final Logger logger = LoggerFactory + .getLogger(NoopDiscoveryClientConfiguration.class); + + @Autowired(required = false) + private ServerProperties server; + + @Autowired + private ApplicationContext context; + + @Autowired + private Environment environment; + + private DefaultServiceInstance serviceInstance; + + @PostConstruct + public void init() { + String host = "localhost"; + try { + host = InetAddress.getLocalHost().getHostName(); + } + catch (UnknownHostException e) { + logger.error("Cannot get host info", e); + } + int port = 0; + if (server != null && server.getPort() != null) { + port = server.getPort(); + } + if (context instanceof EmbeddedWebApplicationContext) { + EmbeddedServletContainer container = ((EmbeddedWebApplicationContext) context) + .getEmbeddedServletContainer(); + if (container != null) { + // TODO: why is it null + port = container.getPort(); + } + } + serviceInstance = new DefaultServiceInstance(environment.getProperty( + "spring.application.name", "application"), host, port); + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + context.publishEvent(new InstanceRegisteredEvent(this, environment)); + } + + @Bean + public DiscoveryClient discoveryClient() { + return new NoopDiscoveryClient(serviceInstance); + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 9b8a902a..9ea05d30 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,3 +1,4 @@ # Bootstrap Configuration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.springframework.cloud.client.CommonsClientAutoConfiguration +org.springframework.cloud.client.CommonsClientAutoConfiguration,\ +org.springframework.cloud.client.discovery.NoopDiscoveryClientConfiguration