Skip to content

Commit

Permalink
ServerLoadBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
nytta committed Jan 20, 2019
1 parent 25de155 commit da53220
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public T getRef() {
Objects.requireNonNull(interfaceClass, "interfaceClass is null");
boolean oldValue = refInited.get();
if (!oldValue && refInited.compareAndSet(oldValue, true)) {
ref = (T) ServiceFactory.takeDefaultInstance(Reference.class).refer(interfaceClass, params);
ref = ServiceFactory.takeDefaultInstance(Reference.class).refer((Class<T>)interfaceClass, this);
}
return ref;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package lam.cobia.config.spring;

import java.util.Map;

import lam.cobia.proxy.ConsumerProxyFactory;
import lam.cobia.rpc.support.Consumer;
import lam.cobia.rpc.support.Protocol;
Expand All @@ -22,8 +20,8 @@ public class CobiaReference implements Reference{
private ConsumerProxyFactory consumerProxyFactory = ServiceFactory.takeDefaultInstance(ConsumerProxyFactory.class);

@Override
public <T> T refer(Class<T> clazz, Map<String, Object> params) {
Consumer<T> consumer = protocol.refer(clazz, params);
public <T> T refer(Class<T> clazz, CRefrenceBean<T> refrenceBean) {
Consumer<T> consumer = protocol.refer(clazz, refrenceBean);

T t = consumerProxyFactory.getConsumerProxy(consumer);
return t;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package lam.cobia.config.spring;

import java.util.Map;

import lam.cobia.spi.Spiable;

/**
Expand All @@ -15,6 +13,6 @@
@Spiable("cobia")
public interface Reference {

public <T> T refer(Class<T> clazz, Map<String, Object> params);
public <T> T refer(Class<T> clazz, CRefrenceBean<T> refrenceBean);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import lam.cobia.loadbalance.AbstractLoadBalance;
import lam.cobia.rpc.support.Consumer;
import lam.cobia.rpc.support.Invocation;
import org.apache.commons.lang3.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.List;

Expand All @@ -13,8 +17,21 @@
* @version: 1.0
*/
public class ServerLoadBalance extends AbstractLoadBalance {

private static final Logger LOGGER = LoggerFactory.getLogger(ServerLoadBalance.class);

@Override
public <T> Consumer<T> doSelect(List<Consumer<T>> consumers, Invocation invocation) {
if (CollectionUtils.isEmpty(consumers)) {
LOGGER.warn("[doSelect] invocation:{}, consumers is null or empty:{}", invocation, consumers);
return null;
}
boolean serverBalanced = BooleanUtils.toBooleanDefaultIfNull(consumers.get(0).getRegistryData().getServiceBalanced(), false);
for(Consumer<T> consumer : consumers) {
if (serverBalanced) {

}
}
// TODO finish the work here.
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions cobia-core/src/main/java/lam/cobia/rpc/DefaultProtocol.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lam.cobia.rpc;

import lam.cobia.cluster.AbstractCluster;
import lam.cobia.config.spring.CRefrenceBean;
import lam.cobia.config.spring.CRegistryBean;
import lam.cobia.config.spring.CServiceBean;
import lam.cobia.core.model.RegistryData;
Expand Down Expand Up @@ -80,7 +81,7 @@ public <T> Exporter<T> export(Provider<T> provider, CServiceBean<T> serviceBean)
}

@Override
public <T> Consumer<T> refer(Class<T> clazz, Map<String, Object> params) {
public <T> Consumer<T> refer(Class<T> clazz, CRefrenceBean<T> refrenceBean) {

// TODO take LoadBalance according to the config, not takeDefaultInstance(..)
LoadBalance loadBalance = ServiceFactory.takeDefaultInstance(LoadBalance.class);
Expand All @@ -91,8 +92,8 @@ public <T> Consumer<T> refer(Class<T> clazz, Map<String, Object> params) {

List<Consumer<T>> consumers = new ArrayList<Consumer<T>>();
for (RegistryData registryData : list) {
params.put(ParamConstant.WEIGHT, registryData.getWeight());
Consumer<T> consumer = new DefaultConsumer<T>(clazz, params, getClient(registryData), registryData);
refrenceBean.getParams().put(ParamConstant.WEIGHT, registryData.getWeight());
Consumer<T> consumer = new DefaultConsumer<T>(clazz, refrenceBean.getParams(), getClient(registryData), registryData);
consumerMap.put(consumer, sharedObject);
consumers.add(consumer);
}
Expand Down
3 changes: 2 additions & 1 deletion cobia-core/src/main/java/lam/cobia/rpc/support/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Closeable;
import java.util.Map;

import lam.cobia.config.spring.CRefrenceBean;
import lam.cobia.config.spring.CServiceBean;
import lam.cobia.spi.Spiable;

Expand All @@ -19,7 +20,7 @@ public interface Protocol extends Closeable{

public <T> Exporter<T> export(Provider<T> provider, CServiceBean<T> serviceBean);

public <T> Consumer<T> refer(Class<T> clazz, Map<String, Object> params);
public <T> Consumer<T> refer(Class<T> clazz, CRefrenceBean<T> refrenceBean);

public void close();
}

0 comments on commit da53220

Please sign in to comment.