use of org.apache.dubbo.rpc.ProtocolServer in project dubbo by alibaba.
the class HttpProtocol method doExport.
@Override
protected <T> Runnable doExport(final T impl, Class<T> type, URL url) throws RpcException {
String addr = getAddr(url);
ProtocolServer protocolServer = serverMap.get(addr);
if (protocolServer == null) {
RemotingServer remotingServer = httpBinder.bind(url, new InternalHandler(url.getParameter("cors", false)));
serverMap.put(addr, new ProxyProtocolServer(remotingServer));
}
final String path = url.getAbsolutePath();
final String genericPath = path + "/" + GENERIC_KEY;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonRpcServer skeleton = new JsonRpcServer(mapper, impl, type);
JsonRpcServer genericServer = new JsonRpcServer(mapper, impl, GenericService.class);
skeletonMap.put(path, skeleton);
skeletonMap.put(genericPath, genericServer);
return () -> {
skeletonMap.remove(path);
skeletonMap.remove(genericPath);
};
}
use of org.apache.dubbo.rpc.ProtocolServer in project dubbo by alibaba.
the class GrpcProtocol method doExport.
@Override
protected <T> Runnable doExport(T proxiedImpl, Class<T> type, URL url) throws RpcException {
String key = url.getAddress();
ProtocolServer protocolServer = serverMap.computeIfAbsent(key, k -> {
DubboHandlerRegistry registry = new DubboHandlerRegistry();
NettyServerBuilder builder = NettyServerBuilder.forPort(url.getPort()).fallbackHandlerRegistry(registry);
Server originalServer = GrpcOptionsUtils.buildServerBuilder(url, builder).build();
GrpcRemotingServer remotingServer = new GrpcRemotingServer(originalServer, registry);
return new ProxyProtocolServer(remotingServer);
});
GrpcRemotingServer grpcServer = (GrpcRemotingServer) protocolServer.getRemotingServer();
ServiceRepository serviceRepository = ApplicationModel.getServiceRepository();
ProviderModel providerModel = serviceRepository.lookupExportedService(url.getServiceKey());
if (providerModel == null) {
throw new IllegalStateException("Service " + url.getServiceKey() + "should have already been stored in service repository, " + "but failed to find it.");
}
Object originalImpl = providerModel.getServiceInstance();
Class<?> implClass = originalImpl.getClass();
try {
Method method = implClass.getMethod("setProxiedImpl", type);
method.invoke(originalImpl, proxiedImpl);
} catch (Exception e) {
throw new IllegalStateException("Failed to set dubbo proxied service impl to stub, please make sure your stub " + "was generated by the dubbo-protoc-compiler.", e);
}
grpcServer.getRegistry().addService((BindableService) originalImpl, url.getServiceKey());
if (!grpcServer.isStarted()) {
grpcServer.start();
}
return () -> grpcServer.getRegistry().removeService(url.getServiceKey());
}
use of org.apache.dubbo.rpc.ProtocolServer in project dubbo by alibaba.
the class PortTelnetHandler method telnet.
@Override
public String telnet(Channel channel, String message) {
StringBuilder buf = new StringBuilder();
String port = null;
boolean detail = false;
if (message.length() > 0) {
String[] parts = message.split("\\s+");
for (String part : parts) {
if ("-l".equals(part)) {
detail = true;
} else {
if (!StringUtils.isInteger(part)) {
return "Illegal port " + part + ", must be integer.";
}
port = part;
}
}
}
if (port == null || port.length() == 0) {
for (ProtocolServer server : DubboProtocol.getDubboProtocol().getServers()) {
if (buf.length() > 0) {
buf.append("\r\n");
}
if (detail) {
buf.append(server.getUrl().getProtocol()).append("://").append(server.getUrl().getAddress());
} else {
buf.append(server.getUrl().getPort());
}
}
} else {
int p = Integer.parseInt(port);
ProtocolServer protocolServer = null;
for (ProtocolServer s : DubboProtocol.getDubboProtocol().getServers()) {
if (p == s.getUrl().getPort()) {
protocolServer = s;
break;
}
}
if (protocolServer != null) {
ExchangeServer server = (ExchangeServer) protocolServer.getRemotingServer();
Collection<ExchangeChannel> channels = server.getExchangeChannels();
for (ExchangeChannel c : channels) {
if (buf.length() > 0) {
buf.append("\r\n");
}
if (detail) {
buf.append(c.getRemoteAddress()).append(" -> ").append(c.getLocalAddress());
} else {
buf.append(c.getRemoteAddress());
}
}
} else {
buf.append("No such port ").append(port);
}
}
return buf.toString();
}
use of org.apache.dubbo.rpc.ProtocolServer in project dubbo by alibaba.
the class WebServiceProtocol method doExport.
@Override
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
transportFactory = new SoapTransportFactory();
destinationRegistry = new DestinationRegistryImpl();
String addr = getAddr(url);
ProtocolServer protocolServer = serverMap.get(addr);
if (protocolServer == null) {
RemotingServer remotingServer = httpBinder.bind(url, new WebServiceHandler());
serverMap.put(addr, new ProxyProtocolServer(remotingServer));
}
serverFactoryBean = new ServerFactoryBean();
serverFactoryBean.setAddress(url.getAbsolutePath());
serverFactoryBean.setServiceClass(type);
serverFactoryBean.setServiceBean(impl);
serverFactoryBean.setBus(bus);
serverFactoryBean.setDestinationFactory(transportFactory);
serverFactoryBean.getServiceFactory().getConfigurations().add(new URLHashMethodNameSoapActionServiceConfiguration());
server = serverFactoryBean.create();
return new Runnable() {
@Override
public void run() {
if (serverFactoryBean.getServer() != null) {
serverFactoryBean.getServer().destroy();
}
if (serverFactoryBean.getBus() != null) {
serverFactoryBean.getBus().shutdown(true);
}
ProtocolServer httpServer = serverMap.get(addr);
if (httpServer != null) {
httpServer.close();
serverMap.remove(addr);
}
}
};
}
use of org.apache.dubbo.rpc.ProtocolServer in project dubbo by alibaba.
the class XmlRpcProtocol method doExport.
@Override
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
final URL httpUrl = url.setProtocol("http");
String addr = httpUrl.getIp() + ":" + httpUrl.getPort();
ProtocolServer protocolServer = serverMap.get(addr);
if (protocolServer == null) {
RemotingServer remotingServer = httpBinder.bind(httpUrl, new InternalHandler(httpUrl.getParameter("cors", false)));
serverMap.put(addr, new ProxyProtocolServer(remotingServer));
}
final String path = httpUrl.getAbsolutePath();
XmlRpcServletServer xmlRpcServer = new XmlRpcServletServer();
PropertyHandlerMapping propertyHandlerMapping = new PropertyHandlerMapping();
try {
propertyHandlerMapping.setRequestProcessorFactoryFactory(new RequestProcessorFactoryFactory() {
@Override
public RequestProcessorFactory getRequestProcessorFactory(Class pClass) throws XmlRpcException {
return new RequestProcessorFactory() {
@Override
public Object getRequestProcessor(XmlRpcRequest pRequest) throws XmlRpcException {
return impl;
}
};
}
});
propertyHandlerMapping.addHandler(XmlRpcProxyFactoryBean.replace(type.getName()), type);
} catch (Exception e) {
throw new RpcException(e);
}
xmlRpcServer.setHandlerMapping(propertyHandlerMapping);
XmlRpcServerConfigImpl xmlRpcServerConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
xmlRpcServerConfig.setEnabledForExceptions(true);
xmlRpcServerConfig.setContentLengthOptional(false);
skeletonMap.put(path, xmlRpcServer);
return new Runnable() {
@Override
public void run() {
skeletonMap.remove(path);
}
};
}
Aggregations