基于SpringBoot、Docker和阿里云的稀土掘金社区自动签到与自动抽奖脚本:设计、开发与部署实践

创建一个SpringBoot项目

添加依赖

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

 <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>${hutool.version}version>
        dependency>
 
 
    <dependency>
      <groupId>com.squareup.okhttp3groupId>
      <artifactId>okhttpartifactId>
    dependency>    

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>   

开启定时任务

基于SpringBoot、Docker和阿里云的稀土掘金社区自动签到与自动抽奖脚本:设计、开发与部署实践_第1张图片

编写自动签到代码

@Api(tags = "掘金")
@RestController
@RequestMapping("/jue-jin")
@Slf4j
@ResponseInfoSkin
public class JueJin {
    public static final String COOKIE = ":cookie";

    public static final String JUEJIN="juejin";
    // 每天9点15中开始执行
    public static final String IN_TIME = "0 15 09 ? * *";
    // 每天9点17中开始执行
    public static final String DRAW_TIME = "0 17 09 ? * *";
    @Resource
    private RedisServiceUtil redisServiceUtil;

    @SneakyThrows
    @GetMapping("sign-in")
    @ApiOperation("签到")
    @Scheduled(cron = IN_TIME)
    public String signIn() {
        log.info("掘金自动签到开始");
        String str = redisServiceUtil.get(JUEJIN + COOKIE);
        //List list = JSONUtil.toBean(str, List.class);
        List<String> list = JsonUtils.toObject(str, List.class);
        if (CommUtil.isEmpty(list)) {
            return "失败";
        }
        Map<String, String> header = Maps.newHashMap();
        //签到任务接口
        String url = "https://api.juejin.cn/growth_api/v1/check_in";
        for (String id : list) {
            //你的cookie
            String juejinCookie =id;
            RequestBody requestBody = new FormBody.Builder().build();
            String post = post(url, juejinCookie, requestBody, header);
            log.info("执行成功的cookie:{},执行结果为::{}", id, post);

        }

        return "成功";
    }

    //post请求的工具类方法
    public static String post(String url, String cookie, RequestBody requestBody, Map<String, String> header) throws Exception {

        String userAgent = "okhttp/3.12.1;jdmall;android;version/10.3.4;build/92451;";

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .headers(Headers.of(header))
                .addHeader("Cookie", cookie)
                .addHeader("User-Agent", userAgent)
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("connection", "Keep-Alive")
                .addHeader("accept", "*/*")
                .build();

        Response response = client.newCall(request).execute();
        String result = response.body().string();
        log.info("post请求,result:{}", result);
        return result;
    }

    @SneakyThrows
    @GetMapping("lottery-draw")
    @ApiOperation("抽奖")
    @Scheduled(cron = DRAW_TIME)
    public String lotteryDraw() throws Exception {
        log.info("掘金自动抽奖开始");
        String str = redisServiceUtil.get(JUEJIN + COOKIE);
        //List list = JSONUtil.toBean(str, List.class);
        List<String> list = JsonUtils.toObject(str, List.class);
        if (CommUtil.isEmpty(list)) {
            return "失败";
        }
        Map<String, String> header = Maps.newHashMap();
        //抽奖接口
        String drawUrl = "https://api.juejin.cn/growth_api/v1/lottery/draw";
        for (String id : list) {
            //你的cookie
            String juejinCookie =id;
            RequestBody requestBody = new FormBody.Builder().build();
            String response = post(drawUrl, juejinCookie, requestBody, header);
            log.info("执行成功的cookie:{},执行结果为::{}", id, response);
        }

        return "success";
    }

    @PostMapping
    @ApiOperation("存cookie")
    public String set(@org.springframework.web.bind.annotation.RequestBody List<String> cookies) {
        String list = JSONUtil.toJsonStr(cookies);
        redisServiceUtil.set(JUEJIN + ":cookie", list);
        return "success";
    }
}

swagger 接口

基于SpringBoot、Docker和阿里云的稀土掘金社区自动签到与自动抽奖脚本:设计、开发与部署实践_第2张图片

如何找到自己的掘金cookie

基于SpringBoot、Docker和阿里云的稀土掘金社区自动签到与自动抽奖脚本:设计、开发与部署实践_第3张图片

复制自己的cookie

粘贴到接口:存cookie

基于SpringBoot、Docker和阿里云的稀土掘金社区自动签到与自动抽奖脚本:设计、开发与部署实践_第4张图片

定时:@Scheduled(cron = IN_TIME)

这个注解就不在这里展开了

编写Dockerfile

FROM eclipse-temurin:8-jre

## 创建目录,并使用它作为工作目录
RUN mkdir -p /home/project/kkxx-poi
WORKDIR /home/project/kkxx-poi
## 将后端项目的 Jar 文件,复制到镜像中
COPY ./kkxx-poi-1.1-SNAPSHOT.jar app.jar

## 设置 TZ 时区
## 设置 JAVA_OPTS 环境变量,可通过 docker run -e "JAVA_OPTS=" 进行覆盖
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx512m"

## 暴露后端项目的 9091 端口
EXPOSE 9091

#  -Djava.security.egd=file:/dev/./urandom 这是加快生成随机数的配置
# https://blog.csdn.net/qq_39581637/article/details/122719838
## 启动后端项目
CMD java ${JAVA_OPTS}  -jar app.jar

复制到自己服务器某个目录

基于SpringBoot、Docker和阿里云的稀土掘金社区自动签到与自动抽奖脚本:设计、开发与部署实践_第5张图片

构建镜像

docker build -f /home/project/Dockerfile -t kkxx-poi-image:test .

启动镜像

docker run -d --name kkxx-poi-name -p 9092:9091 kkxx-poi-image:test

手动测试swagger文档地址

http://ip:9092/doc.html

你可能感兴趣的:(spring,boot,docker,阿里云)