Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Updates to function again.
Browse files Browse the repository at this point in the history
  • Loading branch information
spencergibb committed Jul 12, 2018
1 parent 6838ca0 commit 9df9597
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.loadbalancer.core;

import org.springframework.cloud.client.ServiceInstance;
import reactor.core.publisher.Mono;

/**
* @author Spencer Gibb
*/
class DefaultReactiveResponse implements ReactiveLoadBalancer.Response<ServiceInstance> {

private final ServiceInstance serviceInstance;

public DefaultReactiveResponse(ServiceInstance serviceInstance) {
this.serviceInstance = serviceInstance;
}

@Override
public Mono<ServiceInstance> getServer() {
return Mono.just(this.serviceInstance);
}

@Override
public Mono<Void> onComplete(OnComplete onComplete) {
return Mono.empty(); //TODO: implement
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
/**
* @author Spencer Gibb
*/
class DefaultRequest implements LoadBalancer.Request {
class DefaultRequest implements Request {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
public interface LoadBalancer<T> {

Request REQUEST = new DefaultRequest();

/**
* Response created for each request.
*/
Expand All @@ -36,14 +38,14 @@ interface Response<T> {
void onComplete(OnComplete onComplete);
}

interface Request {
//TODO: define contents
}

/**
* Choose the next server based on the load balancing algorithm
* @param request
* @return
*/
Response<T> choose(Request request);

default Response<T> choose() {
return choose(REQUEST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
public interface ReactiveLoadBalancer<T> {

Request REQUEST = new DefaultRequest();

/**
* Response created for each request.
*/
Expand All @@ -38,14 +40,14 @@ interface Response<T> {
Mono<Void> onComplete(OnComplete onComplete);
}

interface Request {
//TODO: define contents
}

/**
* Choose the next server based on the load balancing algorithm
* @param request
* @return
*/
Mono<Response<T>> choose(Request request);
Mono<Response<T>> select(Request request);

default Mono<Response<T>> select() { //conflicting name
return select(REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.springframework.cloud.loadbalancer.core;

public interface Request {
//TODO: define contents
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public RoundRobinLoadBalancer(LoadBalancerClientFactory clientFactory, Environme
}

@Override
public LoadBalancer.Response<ServiceInstance> choose(LoadBalancer.Request request) {
public LoadBalancer.Response<ServiceInstance> choose(Request request) {
String serviceId = clientFactory.getName(this.environment);
ServiceInstanceSupplier supplier = clientFactory.getInstance(serviceId, ServiceInstanceSupplier.class);
List<ServiceInstance> instances = supplier.get();
Expand All @@ -64,8 +64,22 @@ public LoadBalancer.Response<ServiceInstance> choose(LoadBalancer.Request reques
}

@Override
public Mono<ReactiveLoadBalancer.Response<ServiceInstance>> choose(ReactiveLoadBalancer.Request request) {
return null;
public Mono<ReactiveLoadBalancer.Response<ServiceInstance>> select(Request request) {
String serviceId = clientFactory.getName(this.environment);
ServiceInstanceSupplier supplier = clientFactory.getInstance(serviceId, ServiceInstanceSupplier.class);
List<ServiceInstance> instances = supplier.get();
//TODO: enforce order?

if (isEmpty(instances)) {
log.warn("No servers available for service: " + serviceId);
return Mono.empty();
}

int nextServerIndex = incrementAndGetModulo(instances.size());

ServiceInstance instance = instances.get(nextServerIndex);

return Mono.just(new DefaultReactiveResponse(instance));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package org.springframework.cloud.loadbalancer.support;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientSpecification;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment;

/**
* A factory that creates client, load balancer and client configuration instances. It
Expand All @@ -37,5 +41,25 @@ public LoadBalancerClientFactory() {
super(LoadBalancerClientConfiguration.class, NAMESPACE, PROPERTY_NAME);
}

public String getName(Environment environment) {
return environment.getProperty(PROPERTY_NAME);
}

@SuppressWarnings("unchecked")
public <T> T getInstance(String name, ResolvableType type) {
AnnotationConfigApplicationContext context = getContext(name);
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
type);
if (beanNames.length > 0) {
for (String beanName : beanNames) {
if (context.isTypeMatch(beanName, type)) {
return (T) context.getBean(beanName);
}
}
return null;
}
return null;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public void roundRobbinLoadbalancerWorks() {

ServiceInstance instance = response.getServer();
assertThat(instance).isNotNull();
assertThat(instance.getHost()).isEqualTo(host).as("instance host is incorrent %s", host);
assertThat(instance.getHost())
.as("instance host is incorrent %s", host)
.isEqualTo(host);

if (host.equals("b.host")) {
assertThat(instance.isSecure()).isTrue();
Expand Down

0 comments on commit 9df9597

Please sign in to comment.