一、我们在定时任务里面调用调用微服务,但是这个微服务时间执行时间比较久,直接报错了;
请问下 blade-job调用微服务,如何设置调用微服务等待的时长,不让超时
feign是有一个强制的过期时间的,如果执行任务时间很长,你又要一定等他执行完获取返回后进行下一步操作,可以用异步调用,如果异步也不行,就用mq吧,或者重新设计方法调用链。
配置文件参考
# application.yml
feign:
client:
config:
default: # 针对所有服务
connectTimeout: 5000 # 连接超时(单位:ms)
readTimeout: 30000 # 读取响应超时(单位:ms)
ribbon:
ConnectTimeout: 5000 # Ribbon 连接超时
ReadTimeout: 30000 # Ribbon 读取超时
MaxAutoRetries: 1 # 重试次数(同一实例)
MaxAutoRetriesNextServer: 1 # 切换实例重试次数
异步调用参考
// 启用异步支持(在启动类添加)
@EnableAsync
public class Application { ... }
// 在 Service 中定义异步方法
@Service
public class AsyncService {
@Autowired
private RemoteClient remoteClient;
@Async // 使用独立线程池执行
public CompletableFuture<Response> asyncCall() {
Response result = remoteClient.call();
return CompletableFuture.completedFuture(result);
}
}
// 定时任务中调用
@Scheduled(fixedDelay = 5000)
public void scheduledTask() {
asyncService.asyncCall()
.thenAccept(response -> {
// 异步处理结果(如更新数据库、触发后续操作)
})
.exceptionally(ex -> {
// 异常处理(如记录日志、重试)
return null;
});
}
# application.yml
feign:
client:
config:
default: # 针对所有服务
connectTimeout: 5000 # 连接超时(单位:ms)
readTimeout: 30000 # 读取响应超时(单位:ms)
ribbon:
ConnectTimeout: 5000 # Ribbon 连接超时
ReadTimeout: 30000 # Ribbon 读取超时
MaxAutoRetries: 1 # 重试次数(同一实例)
MaxAutoRetriesNextServer: 1 # 切换实例重试次数
采用这种方式,直接在当前服务里面 修改yml可以么
还在么
扫一扫访问 Blade技术社区 移动端