Search in sources :

Example 1 with ProviderModel

use of org.apache.dubbo.rpc.model.ProviderModel in project dubbo by alibaba.

the class InvokeTelnetHandler method telnet.

@Override
@SuppressWarnings("unchecked")
public String telnet(Channel channel, String message) {
    if (StringUtils.isEmpty(message)) {
        return "Please input method name, eg: \r\ninvoke xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\n" + "invoke XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\n" + "invoke com.xxx.XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})";
    }
    String service = (String) channel.getAttribute(ChangeTelnetHandler.SERVICE_KEY);
    int i = message.indexOf("(");
    if (i < 0 || !message.endsWith(")")) {
        return "Invalid parameters, format: service.method(args)";
    }
    String method = message.substring(0, i).trim();
    String args = message.substring(i + 1, message.length() - 1).trim();
    i = method.lastIndexOf(".");
    if (i >= 0) {
        service = method.substring(0, i).trim();
        method = method.substring(i + 1).trim();
    }
    List<Object> list;
    try {
        list = JSON.parseArray("[" + args + "]", Object.class);
    } catch (Throwable t) {
        return "Invalid json argument, cause: " + t.getMessage();
    }
    StringBuilder buf = new StringBuilder();
    Method invokeMethod = null;
    ProviderModel selectedProvider = null;
    if (isInvokedSelectCommand(channel)) {
        selectedProvider = (ProviderModel) channel.getAttribute(INVOKE_METHOD_PROVIDER_KEY);
        invokeMethod = (Method) channel.getAttribute(SelectTelnetHandler.SELECT_METHOD_KEY);
    } else {
        for (ProviderModel provider : ApplicationModel.allProviderModels()) {
            if (!isServiceMatch(service, provider)) {
                continue;
            }
            selectedProvider = provider;
            List<Method> methodList = findSameSignatureMethod(provider.getAllMethods(), method, list);
            if (CollectionUtils.isEmpty(methodList)) {
                break;
            }
            if (methodList.size() == 1) {
                invokeMethod = methodList.get(0);
            } else {
                List<Method> matchMethods = findMatchMethods(methodList, list);
                if (CollectionUtils.isEmpty(matchMethods)) {
                    break;
                }
                if (matchMethods.size() == 1) {
                    invokeMethod = matchMethods.get(0);
                } else {
                    // exist overridden method
                    channel.setAttribute(INVOKE_METHOD_PROVIDER_KEY, provider);
                    channel.setAttribute(INVOKE_METHOD_LIST_KEY, matchMethods);
                    channel.setAttribute(INVOKE_MESSAGE_KEY, message);
                    printSelectMessage(buf, matchMethods);
                    return buf.toString();
                }
            }
            break;
        }
    }
    if (!StringUtils.isEmpty(service)) {
        buf.append("Use default service ").append(service).append(".");
    }
    if (selectedProvider == null) {
        buf.append("\r\nNo such service ").append(service);
        return buf.toString();
    }
    if (invokeMethod == null) {
        buf.append("\r\nNo such method ").append(method).append(" in service ").append(service);
        return buf.toString();
    }
    try {
        Object[] array = realize(list.toArray(), invokeMethod.getParameterTypes(), invokeMethod.getGenericParameterTypes());
        long start = System.currentTimeMillis();
        AppResponse result = new AppResponse();
        try {
            Object o = invokeMethod.invoke(selectedProvider.getServiceInstance(), array);
            result.setValue(o);
        } catch (Throwable t) {
            result.setException(t);
        }
        long end = System.currentTimeMillis();
        buf.append("\r\nresult: ");
        buf.append(JSON.toJSONString(result.recreate()));
        buf.append("\r\nelapsed: ");
        buf.append(end - start);
        buf.append(" ms.");
    } catch (Throwable t) {
        return "Failed to invoke method " + invokeMethod.getName() + ", cause: " + StringUtils.toString(t);
    }
    return buf.toString();
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) Method(java.lang.reflect.Method) ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Example 2 with ProviderModel

use of org.apache.dubbo.rpc.model.ProviderModel in project dubbo by alibaba.

the class Ls method listProvider.

public String listProvider() {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("As Provider side:" + System.lineSeparator());
    Collection<ProviderModel> providerModelList = ApplicationModel.allProviderModels();
    TTable tTable = new TTable(new TTable.ColumnDefine[] { new TTable.ColumnDefine(TTable.Align.MIDDLE), new TTable.ColumnDefine(TTable.Align.MIDDLE) });
    // Header
    tTable.addRow("Provider Service Name", "PUB");
    // Content
    for (ProviderModel providerModel : providerModelList) {
        tTable.addRow(providerModel.getServiceKey(), ServiceCheckUtils.isRegistered(providerModel) ? "Y" : "N");
    }
    stringBuilder.append(tTable.rendering());
    return stringBuilder.toString();
}
Also used : TTable(org.apache.dubbo.qos.textui.TTable) ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Example 3 with ProviderModel

use of org.apache.dubbo.rpc.model.ProviderModel in project dubbo by alibaba.

the class RegistryProtocol method registerStatedUrl.

private void registerStatedUrl(URL registryUrl, URL registeredProviderUrl, boolean registered) {
    ProviderModel model = ApplicationModel.getProviderModel(registeredProviderUrl.getServiceKey());
    model.addStatedUrl(new ProviderModel.RegisterStatedURL(registeredProviderUrl, registryUrl, registered));
}
Also used : ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Example 4 with ProviderModel

use of org.apache.dubbo.rpc.model.ProviderModel 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());
}
Also used : NettyServerBuilder(io.grpc.netty.NettyServerBuilder) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) Server(io.grpc.Server) Method(java.lang.reflect.Method) ServiceRepository(org.apache.dubbo.rpc.model.ServiceRepository) IOException(java.io.IOException) RpcException(org.apache.dubbo.rpc.RpcException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Example 5 with ProviderModel

use of org.apache.dubbo.rpc.model.ProviderModel in project dubbo by alibaba.

the class RegistryProtocol method getStatedUrl.

private ProviderModel.RegisterStatedURL getStatedUrl(URL registryUrl, URL providerUrl) {
    ProviderModel providerModel = ApplicationModel.getServiceRepository().lookupExportedService(providerUrl.getServiceKey());
    List<ProviderModel.RegisterStatedURL> statedUrls = providerModel.getStatedUrl();
    return statedUrls.stream().filter(u -> u.getRegistryUrl().equals(registryUrl) && u.getProviderUrl().getProtocol().equals(providerUrl.getProtocol())).findFirst().orElseThrow(() -> new IllegalStateException("There should have at least one registered url."));
}
Also used : ProviderModel(org.apache.dubbo.rpc.model.ProviderModel)

Aggregations

ProviderModel (org.apache.dubbo.rpc.model.ProviderModel)10 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 URL (org.apache.dubbo.common.URL)2 Registry (org.apache.dubbo.registry.Registry)2 ServiceRepository (org.apache.dubbo.rpc.model.ServiceRepository)2 Server (io.grpc.Server)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 TTable (org.apache.dubbo.qos.textui.TTable)1 AppResponse (org.apache.dubbo.rpc.AppResponse)1 ProtocolServer (org.apache.dubbo.rpc.ProtocolServer)1 RpcException (org.apache.dubbo.rpc.RpcException)1