GenericService 通用服务 invoke 援引
https://www.cnblogs.com/xhj123/p/9076530.html
// 当前应用配置
ApplicationConfig applicationConfig= new ApplicationConfig();
applicationConfig.setName(“dubboConsumer”);//应用名称配置
ReferenceConfig referenceConfig = new ReferenceConfig();
referenceConfig.setInterface(ApiContext.getContext().getInterfaceName());
referenceConfig.setVersion(ApiContext.getContext().getVersion());
referenceConfig.setGeneric(true); // 声明为泛化接口
referenceConfig.setApplication(applicationConfig);.//
referenceConfig.setRegistry(registryConfig);//RegistryConfig 注册中心相关配置
referenceConfig.setTimeout(30000);
GenericService genericService = ReferenceConfigCache.getCache().get(referenceConfig);
// 如果启动OpenApi时 后端Dubbo服务没有提供者则需要销毁缓存,否则等提供者正常运行后无法正常获取提供者
if (genericService == null) {
ReferenceConfigCache.getCache().destroy(referenceConfig);
return new ApiResponse(ErrorCodeConst.GLOBAL_UNKNOW_SERVICE, "服务不可用");
}
//接口防重
if(checkRepeatRequest(apiEntity)) {
return new ApiResponse(ErrorCodeConst.GLOBAL_REQUEST_REPEAT, "请求已被处理!");
}
response = genericService.$invoke(ApiContext.getContext().getInterfaceMethod(), ApiContext.getContext().getInvokerContext().getParamTypes(), ApiContext.getContext().getInvokerContext().getArgs());
if (MediaType.APPLICATION_OCTET_STREAM_VALUE.equals(accept)) {
outStream(request, response1, response);
return null;
}
apiResponse = new ApiResponse();
apiResponse.setCode("0");
apiResponse.setData(response);
PropertyFilter filter = new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if ("class".equals(name)) {
return false;
}
return true;
}
};
LogBusinessDto log = this.getLog(request);
SerializerFeature[] serializerFeatureArray =new SerializerFeature[] {SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.QuoteFieldNames};
SerializeWriter sw = new SerializeWriter(serializerFeatureArray);
JSONSerializer serializer = new JSONSerializer(sw);
serializer.getPropertyFilters().add(filter);
serializer.getMapping().put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
serializer.write(apiResponse);
log.setOutParam(sw + "");
try {
this.saveLog(log);
}catch (Exception e){
logger.info("============{}",e.getMessage());
}
logger.info("response={} times={}", sw, String.valueOf(System.currentTimeMillis() - time1));
return sw.toString();
/**
*
* @param apiEntity
* @return true 重复请求 false 正常请求
*/
public Boolean checkRepeatRequest(ApiEntity apiEntity){
Boolean result = false;
//老版本调法 无需校验是否重复请求
if(!StringUtils.isEmpty(apiEntity.getUniqueKey())) {
String redisKey = "CHECK_REPEAT_REQUEST_"+apiEntity.getUniqueKey();
String lockResult = jedisCluster.set(redisKey, UUID.randomUUID().toString(), "NX", "PX", 10*60*1000);
if (!LOCK_SUCCESS.equals(lockResult)) {
result = true;
logger.error("重复的请求,method={},key={}",apiEntity.getMethod(),apiEntity.getUniqueKey());
}
}
return result;
}