我正在使用 Spring Integration 将文件加载到 sftp。我有 SessionFactory 像:
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="${host}"/>
<property name="port" value="${port}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<property name="knownHosts" value="${known.hosts}"/>
</bean>
我需要每隔几个小时检查一次 sftp 是否正常。这是在运行状况检查器中使用 DefaultSftpSessionFactory 的正确方法吗?
public class HealthChecker
{
@Autowired
private DefaultSftpSessionFactory sftpSessionFactory;
public String doHealthCheck()
{
try
{
SftpSession sftpSession = sftpSessionFactory.getSession();
if (sftpSession.isOpen())
{
sftpSession.close();
return "OK";
}
}
catch (Exception ex)
{
LOG.error("Can't connect to sftp", ex);
}
return "ERROR";
}
}
使用 HealthCheck 时我们需要使用一些最佳实践吗?
最佳答案
我认为您的解决方案背后的想法很好,只要记住健康检查服务不应该很重。以防万一,如果您使用任何监控工具,每 N 秒/分钟进行一次轮询,如果您的健康检查服务长时间运行,它可能会不时失败。
我还建议使用 Spring Actuator,类似的东西:
@Endpoint(id = "sftp")
@Component
@RequiredArgsConstructor
public class SftpEndpoint {
@ReadOperation
public Map<String, Object> sftp() {
}
}
关于Java健康检查最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51469838/