简体   繁体   English

你如何在 Spring Boot 中分离角色? (Web 与调度程序等。)

[英]How do you separate roles in Spring Boot ? (Web vs Scheduler, etc..)

I am coming from a (mostly) Python Django/Celery background and starting with Spring Boot.我来自(主要是)Python Django/Celery 背景并从 Spring Boot 开始。

I am having a hard time to understand how do you separate the roles.我很难理解你是如何区分角色的。

For example when having a Django/Celery project, I will have one one side the web backends started as gunicorn , and on the other side the workers started as celery (so, different commands, but pointing at the same code).例如,当有一个 Django/Celery 项目时,我会让网络后端在一侧以gunicorn开始,而在另一侧工作人员以celery开始(因此,不同的命令,但指向相同的代码)。

But on Spring Boot, you only have a single entrypoint, and as soon as the scheduler is injected, the jobs will start being processed.但是在 Spring Boot 上,你只有一个入口点,一旦调度程序被注入,作业就会开始被处理。

What is the correct way do separate those like in a Django/Celery application ?将 Django/Celery 应用程序中的那些分开的正确方法是什么? Should I put almost all my code as a library and then create 2 final applications, one that will setup the @DispatcherServlet and another one that will setup @EnableScheduling , or is there some kind of configuration to be injected at runtime ?我应该将几乎所有代码作为一个库,然后创建 2 个最终应用程序,一个将设置@DispatcherServlet另一个将设置@EnableScheduling ,还是在运行时注入某种配置?

In my opinion, if 'the web' and 'the scheduler' are both the important function in the application, then we don't need to separate them as long as you are creating a monolithic application.在我看来,如果“网络”和“调度程序”都是应用程序中的重要功能,那么只要您创建的是单体应用程序,我们就不需要将它们分开。

Because you are using spring boot, so @DispatcherServlet and all the other web component that a web application needed will be injected and configured automatically.因为您使用的是 Spring Boot,所以 @DispatcherServlet 和 Web 应用程序所需的所有其他 Web 组件将被自动注入和配置。 The only thing you have to do is creating some class that annotated with @Controller or @RestController and set up the @RequestMapping methods inside those class.您唯一需要做的就是创建一些用@Controller 或@RestController 注释的类,并在这些类中设置@RequestMapping 方法。

How about Scheduler?调度器呢? you need to add @EnableScheduling in one of the @Configuration class first, then create Scheduler class in scheduler package like below code sample.您需要首先在@Configuration 类之一中添加@EnableScheduling,然后在调度程序包中创建调度程序类,如下面的代码示例。

You can use cron property to set up the specify execute time just like Linux crontab.您可以使用 cron 属性来设置指定的执行时间,就像 Linux crontab 一样。 The jobs will start being processed only if the cron time is up.只有当 cron 时间到时,作业才会开始处理。

@Component
public class PlatformScheduler {

    @Autowired
    private BatchService batchService;
   
    @Scheduled(cron = "0 0 12 * * *")
    public void dailyInitialize() {

        clearCompletedBatches();
        queryBatchesToRunToday();

    }


    @Scheduled(fixedRate = 10000, initialDelay = 10000)
    private void harvestCompletedBatches() {

        batchService.harvestCompletedBatches();

    }

    @Scheduled(fixedRate = 10000, initialDelay = 10000)
    private void executeWaitingBatches() {

        batchService.executeWaitingBatches(new DateTime());

    }

}

The most simple project hierarchy will be like below, 'the web' and 'the scheduler' can be in the same project safely and share the same @Service components without harm.最简单的项目层次结构如下所示,“web”和“调度程序”可以安全地在同一个项目中,并共享相同的 @Service 组件而不会造成伤害。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM