Search in sources :

Example 6 with ShenyuException

use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.

the class ApacheDubboProxyService method genericInvoker.

/**
 * Generic invoker object.
 *
 * @param body     the body
 * @param metaData the meta data
 * @param exchange the exchange
 * @return the object
 * @throws ShenyuException the shenyu exception
 */
public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws ShenyuException {
    ReferenceConfig<GenericService> reference = ApacheDubboConfigCache.getInstance().get(metaData.getPath());
    if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterface())) {
        ApacheDubboConfigCache.getInstance().invalidate(metaData.getPath());
        reference = ApacheDubboConfigCache.getInstance().initRef(metaData);
    }
    GenericService genericService = reference.get();
    Pair<String[], Object[]> pair;
    if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.dubboBodyIsEmpty(body)) {
        pair = new ImmutablePair<>(new String[] {}, new Object[] {});
    } else {
        pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes());
    }
    return Mono.fromFuture(invokeAsync(genericService, metaData.getMethodName(), pair.getLeft(), pair.getRight()).thenApply(ret -> {
        if (Objects.isNull(ret)) {
            ret = Constants.DUBBO_RPC_RESULT_EMPTY;
        }
        exchange.getAttributes().put(Constants.RPC_RESULT, ret);
        exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
        return ret;
    })).onErrorMap(exception -> exception instanceof GenericException ? new ShenyuException(((GenericException) exception).getExceptionMessage()) : new ShenyuException(exception));
}
Also used : GenericService(org.apache.dubbo.rpc.service.GenericService) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) GenericException(org.apache.dubbo.rpc.service.GenericException)

Example 7 with ShenyuException

use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.

the class AlibabaDubboProxyService method genericInvoker.

/**
 * Generic invoker object.
 *
 * @param body     the body
 * @param metaData the meta data
 * @return the object
 * @throws ShenyuException the shenyu exception
 */
public ResponseFuture genericInvoker(final String body, final MetaData metaData) throws ShenyuException {
    ReferenceConfig<GenericService> reference = AlibabaDubboConfigCache.getInstance().get(metaData.getPath());
    if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterface())) {
        AlibabaDubboConfigCache.getInstance().invalidate(metaData.getPath());
        reference = AlibabaDubboConfigCache.getInstance().initRef(metaData);
    }
    try {
        GenericService genericService = reference.get();
        Pair<String[], Object[]> pair;
        if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.dubboBodyIsEmpty(body)) {
            pair = new ImmutablePair<>(new String[] {}, new Object[] {});
        } else {
            pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes());
        }
        genericService.$invoke(metaData.getMethodName(), pair.getLeft(), pair.getRight());
    } catch (GenericException e) {
        LOG.error("dubbo invoker have exception", e);
        throw new ShenyuException(e.getExceptionMessage());
    }
    FutureAdapter<?> adapter = (FutureAdapter<?>) RpcContext.getContext().getFuture();
    return adapter.getFuture();
}
Also used : GenericService(com.alibaba.dubbo.rpc.service.GenericService) FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) GenericException(com.alibaba.dubbo.rpc.service.GenericException)

Example 8 with ShenyuException

use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.

the class NacosServerRegisterRepository method registerURI.

private void registerURI(final String contextPath, final String serviceName, final RpcTypeEnum rpcType) {
    try {
        List<Instance> healthyInstances = namingService.selectInstances(serviceName, true);
        List<URIRegisterDTO> registerDTOList = new ArrayList<>();
        healthyInstances.forEach(healthyInstance -> {
            if (contextPath.equals(healthyInstance.getMetadata().get("contextPath"))) {
                String metadata = healthyInstance.getMetadata().get("uriMetadata");
                URIRegisterDTO uriRegisterDTO = GsonUtils.getInstance().fromJson(metadata, URIRegisterDTO.class);
                registerDTOList.add(uriRegisterDTO);
                String serviceConfigName = RegisterPathConstants.buildServiceConfigPath(rpcType.getName(), contextPath);
                if (!metadataConfigCache.contains(serviceConfigName)) {
                    subscribeMetadata(serviceConfigName);
                    metadataConfigCache.add(serviceConfigName);
                }
            }
        });
        if (!RPC_URI_TYPE_SET.contains(rpcType)) {
            return;
        }
        if (registerDTOList.isEmpty()) {
            URIRegisterDTO uriRegisterDTO = new URIRegisterDTO();
            uriRegisterDTO.setContextPath(Constants.PATH_SEPARATOR + contextPath);
            registerDTOList.add(uriRegisterDTO);
        }
        publishRegisterURI(registerDTOList);
    } catch (NacosException e) {
        throw new ShenyuException(e);
    }
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) URIRegisterDTO(org.apache.shenyu.register.common.dto.URIRegisterDTO) ArrayList(java.util.ArrayList) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 9 with ShenyuException

use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.

the class NacosServerRegisterRepository method init.

@Override
public void init(final ShenyuServerRegisterPublisher publisher, final ShenyuRegisterCenterConfig config) {
    this.publisher = publisher;
    String serverAddr = config.getServerLists();
    Properties properties = config.getProps();
    Properties nacosProperties = new Properties();
    nacosProperties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
    nacosProperties.put(PropertyKeyConst.NAMESPACE, properties.getProperty("nacosNameSpace"));
    // the nacos authentication username
    nacosProperties.put(PropertyKeyConst.USERNAME, properties.getProperty(PropertyKeyConst.USERNAME, ""));
    // the nacos authentication password
    nacosProperties.put(PropertyKeyConst.PASSWORD, properties.getProperty(PropertyKeyConst.PASSWORD, ""));
    // access key for namespace
    nacosProperties.put(PropertyKeyConst.ACCESS_KEY, properties.getProperty(PropertyKeyConst.ACCESS_KEY, ""));
    // secret key for namespace
    nacosProperties.put(PropertyKeyConst.SECRET_KEY, properties.getProperty(PropertyKeyConst.SECRET_KEY, ""));
    try {
        this.configService = ConfigFactory.createConfigService(nacosProperties);
        this.namingService = NamingFactory.createNamingService(nacosProperties);
    } catch (NacosException e) {
        throw new ShenyuException(e);
    }
    subscribe();
}
Also used : ShenyuException(org.apache.shenyu.common.exception.ShenyuException) Properties(java.util.Properties) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 10 with ShenyuException

use of org.apache.shenyu.common.exception.ShenyuException in project incubator-shenyu by apache.

the class NacosServerRegisterRepository method subscribeMetadata.

private void subscribeMetadata(final String serviceConfigName) {
    registerMetadata(readData(serviceConfigName));
    LOGGER.info("subscribe metadata: {}", serviceConfigName);
    try {
        configService.addListener(serviceConfigName, defaultGroup, new Listener() {

            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(final String config) {
                registerMetadata(config);
            }
        });
    } catch (NacosException e) {
        throw new ShenyuException(e);
    }
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) ShenyuException(org.apache.shenyu.common.exception.ShenyuException) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

ShenyuException (org.apache.shenyu.common.exception.ShenyuException)47 ArrayList (java.util.ArrayList)12 Test (org.junit.jupiter.api.Test)12 Method (java.lang.reflect.Method)10 NacosException (com.alibaba.nacos.api.exception.NacosException)8 URIRegisterDTO (org.apache.shenyu.register.common.dto.URIRegisterDTO)8 List (java.util.List)7 MetaData (org.apache.shenyu.common.dto.MetaData)6 Optional (java.util.Optional)5 Map (java.util.Map)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Constants (org.apache.shenyu.common.constant.Constants)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 Objects (java.util.Objects)3 Properties (java.util.Properties)3 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 Mono (reactor.core.publisher.Mono)3