Boot 2.0.0 compile compatibility fixes.

This commit is contained in:
Spencer Gibb
2017-06-08 14:09:59 -06:00
parent 6e76121eb4
commit 0eb889efc6
12 changed files with 19 additions and 334 deletions

View File

@@ -51,8 +51,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>

View File

@@ -65,11 +65,13 @@
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -1,105 +0,0 @@
/*
* Copyright 2013-2015 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.zookeeper.discovery;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;
import org.springframework.util.ReflectionUtils;
/**
* Zookeeper {@link org.springframework.cloud.client.discovery.DiscoveryLifecycle}
* that uses {@link ZookeeperServiceDiscovery} to register and de-register instances.
*
* @author Spencer Gibb
* @since 1.0.0
* @deprecated replaced by {@link org.springframework.cloud.zookeeper.serviceregistry.ZookeeperAutoServiceRegistration} . Remove in Edgware
*/
@Deprecated
public class ZookeeperLifecycle extends AbstractDiscoveryLifecycle {
private static final Log log = LogFactory.getLog(ZookeeperLifecycle.class);
private ZookeeperDiscoveryProperties properties;
private ZookeeperServiceDiscovery serviceDiscovery;
public ZookeeperLifecycle(ZookeeperDiscoveryProperties properties,
ZookeeperServiceDiscovery serviceDiscovery) {
this.properties = properties;
this.serviceDiscovery = serviceDiscovery;
if (this.properties.getInstancePort() != null) {
this.serviceDiscovery.setPort(this.properties.getInstancePort());
this.serviceDiscovery.build();
}
this.serviceDiscovery.buildServiceDiscovery();
}
@Override
protected void register() {
if (!this.properties.isRegister()) {
log.debug("Registration disabled.");
return;
}
try {
this.serviceDiscovery.getServiceDiscoveryRef().get().start();
this.serviceDiscovery.getServiceDiscoveryRef().get().registerService(this.serviceDiscovery.getServiceInstanceRef().get());
}
catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
}
// TODO: implement registerManagement
@Override
protected void deregister() {
if (!this.properties.isRegister()) {
return;
}
try {
this.serviceDiscovery.getServiceDiscoveryRef().get().unregisterService(
this.serviceDiscovery.getServiceInstanceRef().get());
}
catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
}
// TODO: implement deregisterManagement
@Override
protected boolean isEnabled() {
return this.properties.isEnabled();
}
@Override
protected int getConfiguredPort() {
return this.serviceDiscovery.getPort();
}
@Override
protected void setConfiguredPort(int port) {
this.serviceDiscovery.setPort(port);
this.serviceDiscovery.build();
this.serviceDiscovery.buildServiceDiscovery();
}
@Override
protected Object getConfiguration() {
return this.properties;
}
}

View File

@@ -31,7 +31,6 @@ import org.apache.curator.x.discovery.ServiceInstanceBuilder;
import org.apache.curator.x.discovery.UriSpec;
import org.apache.curator.x.discovery.details.InstanceSerializer;
import org.springframework.beans.BeansException;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -101,8 +100,7 @@ public class ZookeeperServiceDiscovery implements ZookeeperRegistration, Applica
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(this.context.getEnvironment());
this.appName = resolver.getProperty("spring.application.name", "application");
this.appName = this.context.getEnvironment().getProperty("spring.application.name", "application");
}
@Override

View File

@@ -15,11 +15,13 @@
*/
package org.springframework.cloud.zookeeper.discovery.dependency;
import java.util.Collections;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
@@ -32,11 +34,14 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
*/
public class DependenciesPassedCondition extends SpringBootCondition {
private static final Bindable<Map<String, String>> STRING_STRING_MAP = Bindable
.mapOf(String.class, String.class);
private static final String ZOOKEEPER_DEPENDENCIES_PROP = "spring.cloud.zookeeper.dependencies";
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> subProperties = new RelaxedPropertyResolver(context.getEnvironment()).getSubProperties(ZOOKEEPER_DEPENDENCIES_PROP);
Map<String, String> subProperties = Binder.get(context.getEnvironment())
.bind(ZOOKEEPER_DEPENDENCIES_PROP, STRING_STRING_MAP).orElseGet(Collections::emptyMap);
if (!subProperties.isEmpty()) {
return ConditionOutcome.match("Dependencies are defined in configuration");
}

View File

@@ -19,7 +19,6 @@ package org.springframework.cloud.zookeeper.discovery.dependency;
import java.util.Collections;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
@@ -48,10 +47,9 @@ public class DependencyEnvironmentPostProcessor
@Override public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment);
String appName = resolver.getProperty("spring.application.name");
String appName = environment.getProperty("spring.application.name");
if (StringUtils.hasText(appName) && !appName.contains("/")) {
String prefix = resolver.getProperty("spring.cloud.zookeeper.prefix");
String prefix = environment.getProperty("spring.cloud.zookeeper.prefix");
if (StringUtils.hasText(prefix)) {
StringBuilder prefixedName = new StringBuilder();
if (!prefix.startsWith("/")) {

View File

@@ -78,16 +78,6 @@ public class ZookeeperAutoServiceRegistration extends AbstractAutoServiceRegistr
return this.properties.isEnabled();
}
@Override
protected int getConfiguredPort() {
return this.registration.getPort();
}
@Override
protected void setConfiguredPort(int port) {
this.registration.setPort(port);
}
@Override
protected Object getConfiguration() {
return this.properties;

View File

@@ -1,133 +0,0 @@
/*
* Copyright 2013-2017 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.zookeeper.discovery;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.discovery.details.InstanceSerializer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.discovery.test.TestRibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import com.toomuchcoding.jsonassert.JsonPath;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* Test for backwards compatibility
* @author Marcin Grzejszczak
* @author Spencer Gibb
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZookeeperDiscoveryWithZookeeperLifecycleTests.Config.class,
properties = "spring.application.name=testzkwithzookeeperlifecycle",
webEnvironment = RANDOM_PORT)
@ActiveProfiles("ribbon")
public class ZookeeperDiscoveryWithZookeeperLifecycleTests {
@Autowired TestRibbonClient testRibbonClient;
@Autowired DiscoveryClient discoveryClient;
@Autowired ZookeeperServiceDiscovery serviceDiscovery;
@Value("${spring.application.name}") String springAppName;
@Test public void should_find_the_app_by_its_name_via_Ribbon() {
//expect:
then(registeredServiceStatusViaServiceName()).isEqualTo("UP");
}
@Test public void should_find_a_collaborator_via_discovery_client() {
//given:
List<ServiceInstance> instances = this.discoveryClient.getInstances(this.springAppName);
ServiceInstance instance = instances.get(0);
//expect:
then(registeredServiceStatus(instance)).isEqualTo("UP");
then(instance.getMetadata().get("testMetadataKey")).isEqualTo("testMetadataValue");
}
@Test public void should_present_application_name_as_id_of_the_service_instance() {
//given:
ServiceInstance instance = this.discoveryClient.getLocalServiceInstance();
//expect:
then(this.springAppName).isEqualTo(instance.getServiceId());
}
private String registeredServiceStatusViaServiceName() {
return JsonPath.builder(this.testRibbonClient.thisHealthCheck()).field("status").read(String.class);
}
private String registeredServiceStatus(ServiceInstance instance) {
return JsonPath.builder(this.testRibbonClient.callOnUrl(instance.getHost()+":"+instance.getPort(), "health")).field("status").read(String.class);
}
@Test public void should_properly_find_local_instance() {
//expect:
then(this.serviceDiscovery.getServiceInstanceRef().get().getAddress()).isEqualTo(this.discoveryClient.getLocalServiceInstance().getHost());
}
@Configuration
@EnableAutoConfiguration
@Import(CommonTestConfig.class)
@EnableDiscoveryClient
@Profile("ribbon")
static class Config {
@Bean
public ZookeeperServiceDiscovery zookeeperServiceDiscovery(ZookeeperDiscoveryProperties properties, CuratorFramework curator,
InstanceSerializer<ZookeeperInstance> instanceSerializer) {
return new ZookeeperServiceDiscovery(curator, properties, instanceSerializer);
}
@Bean
public ZookeeperLifecycle zookeeperLifecycle(ZookeeperDiscoveryProperties properties,
ZookeeperServiceDiscovery serviceDiscovery) {
return new ZookeeperLifecycle(properties, serviceDiscovery);
}
@Bean TestRibbonClient testRibbonClient(@LoadBalanced RestTemplate restTemplate,
@Value("${spring.application.name}") String springAppName) {
return new TestRibbonClient(restTemplate, springAppName);
}
}
@Controller
@Profile("ribbon")
class PingController {
@RequestMapping("/ping") String ping() {
return "pong";
}
}
}

View File

@@ -1,63 +0,0 @@
/*
* 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.zookeeper.discovery;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.zookeeper.ZookeeperAutoConfiguration;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperAutoServiceRegistration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertTrue;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* @author Spencer Gibb
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZookeeperLifecycleRegistrationDisabledTests.TestPropsConfig.class,
properties = { "spring.application.name=myTestNotRegisteredService",
"spring.cloud.zookeeper.discovery.register=false", "spring.cloud.zookeeper.dependency.enabled=false"},
webEnvironment = RANDOM_PORT)
public class ZookeeperLifecycleRegistrationDisabledTests {
@Autowired
private ZookeeperDiscoveryClient client;
@Test
public void contextLoads() {
List<ServiceInstance> instances = this.client.getInstances("myTestNotRegisteredService");
assertTrue("service was registered", instances.isEmpty());
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@Import({ CommonTestConfig.class })
static class TestPropsConfig { }
}

View File

@@ -4,7 +4,7 @@ import java.util.Collection;
import org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@@ -47,13 +47,13 @@ public class DependencyConfig {
}
class PortListener implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
class PortListener implements ApplicationListener<WebServerInitializedEvent> {
private int port;
@Override
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
this.port = event.getEmbeddedServletContainer().getPort();
public void onApplicationEvent(WebServerInitializedEvent event) {
this.port = event.getWebServer().getPort();
}
public int getPort() {

View File

@@ -15,8 +15,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.zookeeper.discovery.CustomZookeeperServiceDiscovery;
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties;
import org.springframework.cloud.zookeeper.discovery.ZookeeperLifecycle;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServiceDiscovery;
import org.springframework.cloud.zookeeper.discovery.watcher.presence.DependencyPresenceOnStartupVerifier;
import org.springframework.cloud.zookeeper.discovery.watcher.presence.LogMissingDependencyChecker;
@@ -71,10 +69,6 @@ public class DefaultDependencyWatcherSpringTests {
@EnableAutoConfiguration
@Profile("watcher")
static class Config {
@Bean
public ZookeeperLifecycle zookeeperLifecycle(ZookeeperDiscoveryProperties properties, ZookeeperServiceDiscovery serviceDiscovery) {
return new ZookeeperLifecycle(properties, serviceDiscovery);
}
@Bean
@LoadBalanced RestTemplate loadBalancedRestTemplate() {

View File

@@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -79,7 +78,7 @@ public class SampleZookeeperApplication {
@RequestMapping("/myenv")
public String env(@RequestParam("prop") String prop) {
return new RelaxedPropertyResolver(this.env).getProperty(prop, "Not Found");
return this.env.getProperty(prop, "Not Found");
}
@FeignClient("testZookeeperApp")