追梦好歌 發表於 2015-1-18 15:17:00

Net作业调度(四)—quartz.net持久化和集群

<h3>介绍</h3>
<p>在实际使用quartz.net中,持久化能保证实例重启后job不丢失、 集群能均衡服务器压力和解决单点问题。</p>
<p>quartz.net在这两方面配置都比较简单。</p>
<h2>持久化</h2>
<p>quartz.net的持久化,是把job、trigger一些信息存储到数据库里面,以解决内存存储重启丢失。</p>
<h3><strong>下载sql脚本</strong></h3>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;https://github.com/quartznet/quartznet/blob/master/database/tables/tables_sqlServer.sql</p>
<h3><strong>创建个数据库,并执行脚本</strong></h3>
<p><img src="//images0.cnblogs.com/blog/307762/201501/181125329017601.png" alt=""></p>
<p>&nbsp;&nbsp;QRTZ_BLOB_TRIGGERS &nbsp;以Blob 类型存储的触发器。</p>
<p>&nbsp;&nbsp;QRTZ_CALENDARS &nbsp;&nbsp;存放日历信息, quartz.net可以指定一个日历时间范围。</p>
<p>&nbsp;&nbsp;QRTZ_CRON_TRIGGERS &nbsp;cron表达式触发器。</p>
<p>&nbsp;&nbsp;QRTZ_JOB_DETAILS &nbsp; &nbsp; &nbsp;job详细信息。</p>
<p>&nbsp;&nbsp;QRTZ_LOCKS&nbsp; &nbsp; &nbsp; &nbsp;集群实现同步机制的行锁表</p>
<p>&nbsp; QRTZ_SCHEDULER_STATE &nbsp; 实例信息,集群下多使用。</p>
<h3>&nbsp;quartz.net 配置</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">===持久化====
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存储类型</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.jobStore.type</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Quartz.Impl.AdoJobStore.JobStoreTX, Quartz</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">表明前缀</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.jobStore.tablePrefix</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">QRTZ_</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">驱动类型</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.jobStore.driverDelegateType</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数据源名称</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.jobStore.dataSource</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">myDS</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接字符串</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.dataSource.myDS.connectionString</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">Data Source=(local);Initial Catalog=JobScheduler;User ID=sa;Password=123465</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">sqlserver版本</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.dataSource.myDS.provider</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SqlServer-20</span><span style="color: rgba(128, 0, 0, 1)">"</span>;</pre>
</div>
<h3>启动客户端</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> properties =<span style="color: rgba(0, 0, 0, 1)"> JobsManager.GetProperties();
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> schedulerFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StdSchedulerFactory(properties);
            scheduler </span>=<span style="color: rgba(0, 0, 0, 1)"> schedulerFactory.GetScheduler();
            scheduler.Start();

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">var job = JobBuilder.Create&lt;MonitorJob&gt;()
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    .WithIdentity("test", "value")
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    .Build();

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">var trigger = (ICronTrigger) TriggerBuilder.Create()
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    .WithIdentity("test", "value")
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    .WithCronSchedule("0 0/5 * * * ?")
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    .Build();
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">scheduler.ScheduleJob(job, trigger);</span></pre>
</div>
<h3>补充</h3>
<p>&nbsp; &nbsp; &nbsp;1: 持久化后,job只有添加一次了(数据库已经有了),所以不能再执行端写添加job的行为。<span style="color: rgba(0, 0, 0, 1)">这时候需要一个管理工具,动态添加操作。</span></p>
<p>&nbsp; &nbsp; &nbsp;2: quartz.net 支持sql server、sqlite、mysql、oracle、mongodb(非官方版)。</p>
<h1>集群</h1>
<p>部署图:</p>
<p><img src="//images0.cnblogs.com/blog/307762/201501/181416397612551.png" alt="">&nbsp;</p>
<p>如图quartz.net 的集群模式是依赖数据库表的,所以要持久化配置。 &nbsp;集群节点之间是不通信的,这样分布式的架构,很方便进行水平扩展。</p>
<p>1: 除了线程池数量,instanceId可以不同外,各个节点的配置必须是一样的。</p>
<p>2:集群中节点的系统时间一致。 &nbsp;</p>
<p>3:多线程、集群中。quartz.net 利用数据库锁来保证job不会重复执行。</p>
<p>&nbsp; &nbsp; &nbsp;源码在DBSemaphore.cs、UpdateLockRowSemaphore.cs、StdRowLockSemaphore.cs</p>
<p>4:集群化后,某节点失效后,剩余的节点能保证job继续执行下去。</p>
<p>实例配置后启动。</p>
<div class="cnblogs_code">
<pre>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">cluster</span>
            properties[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.jobStore.clustered</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">true</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
            properties[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">quartz.scheduler.instanceId</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">AUTO</span><span style="color: rgba(128, 0, 0, 1)">"</span>;</pre>
</div>
<p>简单管理界面:</p>
<p><img src="//images0.cnblogs.com/blog/307762/201501/181503535266416.png" alt=""></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div class="rights">
<span>作者:蘑菇先生</span>
<span>出处: http://mushroom.cnblogs.com/</span>
<div>
本文版权归作者和博客园共有,欢迎转载。未经作者同意下,必须在文章页面明显标出原文链接及作者,否则保留追究法律责任的权利。<br>如果您认为这篇文章还不错或者有所收获,可以点击右下角的<b>【推荐】</b>按钮,因为你的支持是我继续写作,分享的最大动力!
</div>
</div><br><br>
来源:https://www.cnblogs.com/mushroom/p/4231642.html
頁: [1]
查看完整版本: Net作业调度(四)—quartz.net持久化和集群