Removes ribbon support

This commit is contained in:
Spencer Gibb
2020-01-17 18:29:47 -05:00
parent 60e06b4fd0
commit 5f396d3da0
7 changed files with 2 additions and 386 deletions

View File

@@ -1,62 +0,0 @@
/*
* 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.cloudfoundry.discovery;
import javax.annotation.PostConstruct;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
import org.springframework.cloud.netflix.ribbon.RibbonClientName;
import org.springframework.cloud.netflix.ribbon.RibbonUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Josh Long
*/
@Configuration(proxyBeanMethods = false)
public class CloudFoundryRibbonClientConfiguration {
@RibbonClientName
private String serviceId;
public CloudFoundryRibbonClientConfiguration() {
}
public CloudFoundryRibbonClientConfiguration(String svcId) {
this.serviceId = svcId;
}
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(CloudFoundryService svc, IClientConfig config,
CloudFoundryDiscoveryProperties properties) {
CloudFoundryServerList cloudFoundryServerList = new CloudFoundryServerList(svc,
properties);
cloudFoundryServerList.initWithNiwsConfig(config);
return cloudFoundryServerList;
}
@PostConstruct
public void postConstruct() {
RibbonUtils.initializeRibbonDefaults(this.serviceId);
}
}

View File

@@ -1,58 +0,0 @@
/*
* 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.cloudfoundry.discovery;
import com.netflix.loadbalancer.Server;
/**
* @author Josh Long
*/
public class CloudFoundryServer extends Server {
private final MetaInfo metaInfo;
public CloudFoundryServer(String appName, String uri, int port) {
super(uri, port);
this.metaInfo = new MetaInfo() {
@Override
public String getAppName() {
return appName;
}
@Override
public String getServerGroup() {
return null;
}
@Override
public String getServiceIdForDiscovery() {
return appName;
}
@Override
public String getInstanceId() {
return appName;
}
};
}
@Override
public MetaInfo getMetaInfo() {
return this.metaInfo;
}
}

View File

@@ -1,95 +0,0 @@
/*
* 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.cloudfoundry.discovery;
import java.util.ArrayList;
import java.util.List;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList;
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
import org.springframework.cloud.netflix.ribbon.RibbonProperties;
import org.springframework.util.Assert;
/**
* @author Josh Long
*/
public class CloudFoundryServerList extends AbstractServerList<CloudFoundryServer> {
private final CloudFoundryService cloudFoundryService;
private final CloudFoundryDiscoveryProperties properties;
private IClientConfig clientConfig;
private String serviceId;
CloudFoundryServerList(CloudFoundryService svc,
CloudFoundryDiscoveryProperties properties) {
this.cloudFoundryService = svc;
this.properties = properties;
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
this.clientConfig = clientConfig;
this.serviceId = clientConfig.getClientName();
}
@Override
public List<CloudFoundryServer> getInitialListOfServers() {
return cloudFoundryServers();
}
@Override
public List<CloudFoundryServer> getUpdatedListOfServers() {
return cloudFoundryServers();
}
private List<CloudFoundryServer> cloudFoundryServers() {
Assert.notNull(this.clientConfig, "clientConfig may not be null");
RibbonProperties ribbon = RibbonProperties.from(this.clientConfig);
Boolean secure = ribbon.getSecure();
Integer securePort = ribbon.getSecurePort();
Integer nonSecurePort = ribbon.getPort();
final int port;
if (secure != null && secure && securePort != null) {
port = securePort;
}
else if (nonSecurePort != null) {
port = nonSecurePort;
}
else {
port = this.properties.getDefaultServerPort();
}
return this.cloudFoundryService.getApplicationInstances(this.serviceId)
.map(tpl -> new CloudFoundryServer(tpl.getT1().getName(),
tpl.getT1().getUrls().get(0), port))
.collectList().blockOptional().orElse(new ArrayList<>());
}
// for testing
String getServiceId() {
return this.serviceId;
}
}

View File

@@ -1,45 +0,0 @@
/*
* 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.cloudfoundry.discovery;
import com.netflix.client.IClient;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.Configuration;
/**
* Auto configuration for Ribbon.
*
* @author Josh Long
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass(IClient.class)
@ConditionalOnBean(SpringClientFactory.class)
@ConditionalOnProperty(value = "ribbon.cloudfoundry.enabled", matchIfMissing = true)
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = CloudFoundryRibbonClientConfiguration.class)
public class RibbonCloudFoundryAutoConfiguration {
}

View File

@@ -1,5 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.cloudfoundry.discovery.RibbonCloudFoundryAutoConfiguration
#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
# Discovery Client Configuration
org.springframework.cloud.client.discovery.EnableDiscoveryClient=\
org.springframework.cloud.cloudfoundry.discovery.CloudFoundryDiscoveryClientConfiguration, \

View File

@@ -27,7 +27,7 @@ import org.junit.runners.Suite.SuiteClasses;
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ CloudFoundryServerListTest.class })
@SuiteClasses({})
@Ignore
public class AdhocTestSuite {

View File

@@ -1,123 +0,0 @@
/*
* 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.cloudfoundry.discovery;
import java.util.List;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import org.cloudfoundry.operations.applications.ApplicationDetail;
import org.cloudfoundry.operations.applications.InstanceDetail;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Josh Long
*/
public class CloudFoundryServerListTest {
private CloudFoundryServerList cloudFoundryServerList;
private String serviceId = "foo-service";
@Before
public void setUp() {
IClientConfig iClientConfig = IClientConfig.Builder.newBuilder(this.serviceId)
.withSecure(true).build();
iClientConfig.set(CommonClientConfigKey.SecurePort, 443);
Tuple2<ApplicationDetail, InstanceDetail> tuple2 = getInstanceDetail();
CloudFoundryService cfs = mock(CloudFoundryService.class);
when(cfs.getApplicationInstances(this.serviceId)).thenReturn(Flux.just(tuple2));
this.cloudFoundryServerList = new CloudFoundryServerList(cfs,
new CloudFoundryDiscoveryProperties());
this.cloudFoundryServerList.initWithNiwsConfig(iClientConfig);
}
private Tuple2<ApplicationDetail, InstanceDetail> getInstanceDetail() {
// @formatter:off
ApplicationDetail applicationDetail = ApplicationDetail
.builder()
.instances(2)
.name("my-app")
.stack("stack")
.memoryLimit(1024)
.id("id")
.requestedState("requestedState")
.runningInstances(2)
.url("https://my-app.cfapps.io")
.diskQuota(20)
.build();
InstanceDetail instanceDetail = InstanceDetail
.builder()
.index("0")
.build();
// @formatter:on
return Tuples.of(applicationDetail, instanceDetail);
}
@Test
public void testListOfServers() {
List<CloudFoundryServer> initialListOfServers = this.cloudFoundryServerList
.getInitialListOfServers();
List<CloudFoundryServer> updatedListOfServers = this.cloudFoundryServerList
.getUpdatedListOfServers();
assertThat(initialListOfServers)
.containsExactly(updatedListOfServers.toArray(new CloudFoundryServer[0]))
.hasSize(1);
CloudFoundryServer server = initialListOfServers.get(0);
assertThat(server.getPort()).isEqualTo(443);
}
@Test
public void testDefaultServerPort() {
IClientConfig iClientConfig = mock(IClientConfig.class);
when(iClientConfig.getClientName()).thenReturn(this.serviceId);
Tuple2<ApplicationDetail, InstanceDetail> tuple2 = getInstanceDetail();
CloudFoundryService cfs = mock(CloudFoundryService.class);
when(cfs.getApplicationInstances(this.serviceId)).thenReturn(Flux.just(tuple2));
CloudFoundryServerList serverList = new CloudFoundryServerList(cfs,
new CloudFoundryDiscoveryProperties());
serverList.initWithNiwsConfig(iClientConfig);
CloudFoundryServer server = serverList.getInitialListOfServers().get(0);
assertThat(server.getPort()).isEqualTo(80);
}
@Test
public void testInit() {
assertThat(this.cloudFoundryServerList.getServiceId()).isEqualTo(this.serviceId);
}
}