DaoCloud 云测试机 解决自动停止的问题

问题描述

我们知道上 DaoCloud 上面部署应用到免费的云测试主机, 它会在24小时内停止我们的应用. 如果我么你要继续使用, 必须重新去启动它, 非常的麻烦.

以前的规则是24小时内不操作, 便会停止服务. 那时候可以用360, 百度什么的定时做健康检测, 不断的请求服务, 就可以保证它不停止. 但是新的规则改变了, 不管是否有请求, 24小时内都会关闭我们的测试云主机.

解决方案

那上有政策, 下有对策. 解决方案就是利用 DaoCloud 给出的 API 定时重启自己不就好了?

DaoCloud 给出了自己的 API 文档, 可以操作我们的云测试主机和应用等等, 我们要做的就是通过 API 请求的方式, 定时重启自己.

DaoCloud API文档重启连接点击 这里.

(其中的access token就是在你的用户中心里面, 侧边栏 API 栏中的 API token)

我们要做的就是在项目启动的时候, 添加一个定时任务. 比如10小时后发送 API 请求, 重启自己即可. 如果你用的是 Spring 的项目, 建议你使用 Quartz来做定时任务, 贴上我自己的定时代码 (为了方便整体查看, 把所有代码移到一个类中, 大家可以自己优化代码):

import org.apache.log4j.Logger;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

@Component
public class RestartJob implements Job {
    private static final String TOKEN = "token {yourAccessToken}";
    private RestTemplate restTemplate = new RestTemplate();
    private SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    private Scheduler scheduler;

    private Logger logger = Logger.getLogger(RestartJob.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("============ 执行 重启任务 开始 ==================");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", TOKEN);

        ResponseEntity<Apps> responseEntity = restTemplate.exchange("http://openapi.daocloud.io/v1/apps",
                HttpMethod.GET,
                new HttpEntity<>(httpHeaders),
                Apps.class);

        String appId = responseEntity.getBody()
                .getApp()
                .stream()
                .filter(appInfo -> appInfo.getName().equals("yourAppName"))
                .findFirst()
                .orElse(new AppInfo("appId", "appName"))
                .getId();

        ResponseEntity<Void> voidResponseEntity = restTemplate.exchange("http://openapi.daocloud.io/v1/apps/{app_id}/actions/restart",
                HttpMethod.POST,
                new HttpEntity<>(httpHeaders),
                Void.class,
                appId);

        voidResponseEntity.getStatusCode();// ...

        try {
            scheduler.shutdown();
        } catch (Exception e) {
        }

        logger.info("============ 执行 重启任务 结束 ==================");
    }

    @PostConstruct
    public void startJob() throws Exception {
        logger.info("================== 开始注入 重启任务 =======================");
        JobDetail job = newJob(RestartJob.class)
                .withIdentity("job1", "group1")
                .build();

        Trigger trigger = newTrigger()
                .withIdentity("trigger1", "group1")
                .startAt(Date.from(LocalDateTime.now().plusHours(10).atZone(ZoneId.systemDefault()).toInstant()))
                .build();

        scheduler = schedulerFactory.getScheduler();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();
    }
}

这样项目在启动的时候, 会自己注册一个10小时候发送重启信息的请求到 DaoCloud 服务器. 所以10小时后, 项目会自动重启. 然后重启的时候又会继续注册一个10小时后重启的任务.

这样我们的测试服务就不会超过24小时, DaoCloud 就不会主动停止我们.

note: 你可以设定一个定时几点重启的任务, 不一定要10小时.

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Scroll to Top