Skip to content

Commit

Permalink
remove ServiceClassHolder and use the ApplicationModel replacedly (#2646
Browse files Browse the repository at this point in the history
)

* [Dubbo] move the classes of model from config to rpc-api for reusing

* remove ServiceClassHolder and use the ApplicationModel replacedly

* [Dubbo] delete the unused imported of ApplicationModel

* Update dubbo-demo-consumer.xml

this commits is no need to commit to remote repo.
  • Loading branch information
Jeff-Lv authored and beiwei30 committed Oct 24, 2018
1 parent f2b6f07 commit 7286e23
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private void init() {
checkStubAndMock(interfaceClass);
Map<String, String> map = new HashMap<String, String>();
resolveAsyncInterface(interfaceClass, map);
Map<Object, Object> attributes = new HashMap<Object, Object>();

map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE);
map.put(Constants.DUBBO_VERSION_KEY, Version.getProtocolVersion());
map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
Expand Down Expand Up @@ -311,6 +311,7 @@ private void init() {
appendParameters(map, this);
String prefix = StringUtils.getServiceKey(map);
if (methods != null && !methods.isEmpty()) {
Map<Object, Object> attributes = new HashMap<Object, Object>();
for (MethodConfig method : methods) {
appendParameters(map, method, method.getName());
String retryKey = method.getName() + ".retry";
Expand All @@ -323,6 +324,8 @@ private void init() {
appendAttributes(attributes, method, prefix + "." + method.getName());
checkAndConvertImplicitConfig(method, map, attributes);
}
//attributes are stored by system context.
StaticContext.getSystemContext().putAll(attributes);
}

String hostToRegistry = ConfigUtils.getSystemProperty(Constants.DUBBO_IP_TO_REGISTRY);
Expand All @@ -333,8 +336,6 @@ private void init() {
}
map.put(Constants.REGISTER_IP_KEY, hostToRegistry);

//attributes are stored by system context.
StaticContext.getSystemContext().putAll(attributes);
ref = createProxy(map);
ConsumerModel consumerModel = new ConsumerModel(getUniqueServiceName(), ref, interfaceClass.getMethods());
ApplicationModel.initConsumerModel(getUniqueServiceName(), consumerModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.ServiceClassHolder;
import org.apache.dubbo.rpc.cluster.ConfiguratorFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderModel;
Expand Down Expand Up @@ -532,7 +531,6 @@ private void exportLocal(URL url) {
.setProtocol(Constants.LOCAL_PROTOCOL)
.setHost(LOCALHOST)
.setPort(0);
ServiceClassHolder.getInstance().pushServiceClass(getServiceClass(ref));
Exporter<?> exporter = protocol.export(
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
exporters.add(exporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>

</beans>
</beans>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,50 @@

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.rpc.Invoker;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static java.util.stream.Collectors.toSet;

// adjust project structure in order to fully utilize the methods introduced here.
public class ApplicationModel {

protected static final Logger logger = LoggerFactory.getLogger(ApplicationModel.class);
protected static final Logger LOGGER = LoggerFactory.getLogger(ApplicationModel.class);

/**
* full qualified class name -> provided service
*/
private static final ConcurrentMap<String, Set<ProviderModel>> providedServices = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, ProviderModel> providedServices = new ConcurrentHashMap<>();
/**
* full qualified class name -> subscribe service
*/
private static final ConcurrentMap<String, Set<ConsumerModel>> consumedServices = new ConcurrentHashMap<>();

private static final ConcurrentMap<String, Set<Invoker>> providedServicesInvoker = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, ConsumerModel> consumedServices = new ConcurrentHashMap<>();

public static Collection<ConsumerModel> allConsumerModels() {
return consumedServices.values().stream().flatMap(Collection::stream).collect(toSet());
return consumedServices.values();
}

public static Collection<ProviderModel> allProviderModels() {
return providedServices.values().stream().flatMap(Collection::stream).collect(toSet());
return providedServices.values();
}

public static Collection<ProviderModel> getProviderModel(String serviceName) {
public static ProviderModel getProviderModel(String serviceName) {
return providedServices.get(serviceName);
}

public static Collection<ConsumerModel> getConsumerModel(String serviceName) {
public static ConsumerModel getConsumerModel(String serviceName) {
return consumedServices.get(serviceName);
}

public static void initConsumerModel(String serviceName, ConsumerModel consumerModel) {
Set<ConsumerModel> consumerModels = consumedServices.computeIfAbsent(serviceName, k -> new HashSet<>());
if (!consumerModels.add(consumerModel)) {
logger.warn("Already register the same consumer:" + serviceName);
if (consumedServices.putIfAbsent(serviceName, consumerModel) != null) {
LOGGER.warn("Already register the same consumer:" + serviceName);
}
}

public static void initProviderModel(String serviceName, ProviderModel providerModel) {
Set<ProviderModel> providerModels = providedServices.computeIfAbsent(serviceName, k -> new HashSet<>());
if (!providerModels.add(providerModel)) {
logger.warn("already register the provider service: " + serviceName);
if (providedServices.putIfAbsent(serviceName, providerModel) != null) {
LOGGER.warn("Already register the same:" + serviceName);
}
}

public static void addProviderInvoker(String serviceName,Invoker invoker){
Set<Invoker> invokers = providedServicesInvoker.get(serviceName);
if (invokers == null){
providedServicesInvoker.putIfAbsent(serviceName,new ConcurrentHashSet<Invoker>());
invokers = providedServicesInvoker.get(serviceName);
}
invokers.add(invoker);
}

public Set<Invoker> getProviderInvoker(String serviceName){
Set<Invoker> invokers = providedServicesInvoker.get(serviceName);
if (invokers == null){
return Collections.emptySet();
}
return invokers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public String getServiceName() {
return serviceName;
}

public Class<?> getServiceInterfaceClass() {
return serviceInterfaceClass;
}

public Object getServiceInstance() {
return serviceInstance;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.dubbo.remoting.http.servlet.BootstrapListener;
import org.apache.dubbo.remoting.http.servlet.ServletManager;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.ServiceClassHolder;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol;

import org.apache.http.HeaderElement;
Expand Down Expand Up @@ -90,7 +90,7 @@ public int getDefaultPort() {
@Override
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
String addr = getAddr(url);
Class implClass = ServiceClassHolder.getInstance().popServiceClass();
Class implClass = ApplicationModel.getProviderModel(url.getServiceKey()).getServiceInterfaceClass();
RestServer server = servers.get(addr);
if (server == null) {
server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.ServiceClassHolder;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Test;
Expand All @@ -48,11 +49,12 @@ public void tearDown() {

@Test
public void testExport() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);

IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

RpcContext.getContext().setAttachment("timeout", "200");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl));
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, exportUrl));

Expand All @@ -64,7 +66,9 @@ public void testExport() {

@Test
public void testNettyServer() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
Expand All @@ -79,19 +83,23 @@ public void testNettyServer() {

@Test(expected = RpcException.class)
public void testServletWithoutWebConfig() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");

protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, servletUrl));
protocol.export(proxy.getInvoker(server, IDemoService.class, servletUrl));
}

@Test(expected = RpcException.class)
public void testErrorHandler() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));

Expand All @@ -100,10 +108,12 @@ public void testErrorHandler() {

@Test
public void testInvoke() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);


Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl));
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, exportUrl));

RpcInvocation rpcInvocation = new RpcInvocation("hello", new Class[]{Integer.class, Integer.class}, new Integer[]{2, 3});

Expand All @@ -113,11 +123,13 @@ public void testInvoke() {

@Test
public void testFilter() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
.addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.support.LoggingFilter");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));

Expand All @@ -130,12 +142,14 @@ public void testFilter() {

@Test
public void testRpcContextFilter() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

// use RpcContextFilter
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
.addParameter(Constants.EXTENSION_KEY, "org.apache.dubbo.rpc.protocol.rest.RpcContextFilter");
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));

IDemoService demoService = this.proxy.getProxy(protocol.refer(IDemoService.class, nettyUrl));

Expand All @@ -151,10 +165,12 @@ public void testRpcContextFilter() {

@Test(expected = RuntimeException.class)
public void testRegFail() {
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
IDemoService server = new DemoService();
ProviderModel providerModel = new ProviderModel(exportUrl.getServiceKey(), server, IDemoService.class);
ApplicationModel.initProviderModel(exportUrl.getServiceKey(), providerModel);

URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter");
protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
protocol.export(proxy.getInvoker(server, IDemoService.class, nettyUrl));
}

@Test
Expand Down
Loading

0 comments on commit 7286e23

Please sign in to comment.