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));
}
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();
}
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);
}
}
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();
}
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);
}
}
Aggregations