微服务 分片 运维管理

news/2024/4/27 21:42:24/文章来源:https://blog.csdn.net/m0_62434717/article/details/128961503

微服务 分片 运维管理

  • 分片
    • 分片的概念
    • 分片案例环境搭建
    • 案例改造成任务分片
    • Dataflow类型调度
      • 代码示例
  • 运维管理
    • 事件追踪
    • 运维平台
      • 搭建步骤
      • 使用步骤


分片

分片的概念

当只有一台机器的情况下,给定时任务分片四个,在机器A启动四个线程,分别处理四个分片的内容
在这里插入图片描述
当有两台机器的情况下,分片由两个机器进行分配,机器A负责索引为0,1分片内容,机器B负责2,3分片内容
在这里插入图片描述
当有三台机器的时候,情况如图所示
在这里插入图片描述
当有四台机器的时候
在这里插入图片描述
当有五台机器的时候
在这里插入图片描述

当分片消耗资源少的时候,第一种情况和第二种情况没有太大区别,反之,如果消耗资源很大的时候,CPU的利用率效率会降低

分片数建议服务器个数倍数


分片案例环境搭建

案例需求
数据库中有一些列的数据,需要对这些数据进行备份操作,备份完之后,修改数据的状态,标记已经备份了

第一步:添加依赖

<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version>
</dependency>
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version>
</dependency>
<!--mysql驱动-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

第二步:添加配置

spring:datasource:url: jdbc:mysql://localhost:3306/elastic-job-demo?serverTimezone=GMT%2B8driverClassName: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceusername: rootpassword: 2022

第三步:添加实体类

@Datapublic class FileCustom {//唯⼀标识private Long id;//⽂件名private String name;//⽂件类型private String type;//⽂件内容private String content;//是否已备份private Boolean backedUp = false;public FileCustom(){}public FileCustom(Long id, String name, String type, String content){this.id = id;this.name = name;this.type = type;this.content = content;}}

第四步:添加任务类

@Autowiredprivate FileCustomMapper fileCustomMapper;@Overridepublic void execute(ShardingContext shardingContext) {doWork();}private void doWork() {//查询出所有的备份任务List<FileCustom> fileCustoms = fileCustomMapper.selectAll();for (FileCustom custom:fileCustoms){backUp(custom);}}private void backUp(FileCustom custom){System.out.println("备份的方法名:"+custom.getName()+"备份的类型:"+custom.getType());System.out.println("=======================");//模拟进行备份操作try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}fileCustomMapper.changeState(custom.getId(),1);}}

第五步: 添加任务调度配置

@Bean(initMethod = "init")public SpringJobScheduler fileScheduler(FileCustomElasticjob job, CoordinatorRegistryCenter registryCenter){LiteJobConfiguration jobConfiguration = createJobConfiguration(job.getClass(),"0/5 * * * * ?",1);return new SpringJobScheduler(job,registryCenter,jobConfiguration);}

案例改造成任务分片

第一步:修改任务配置类

@Configurationpublic class JobConfig {@Beanpublic static CoordinatorRegistryCenter registryCenter(@Value("${zookeeper.url}") String url, @Value("${zookeeper.groupName}") String groupName) {ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(url, groupName);//设置节点超时时间zookeeperConfiguration.setSessionTimeoutMilliseconds(100);//zookeeperConfiguration("zookeeper地址","项目名")CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);regCenter.init();return regCenter;}//功能的方法private static LiteJobConfiguration createJobConfiguration(Class clazz, String corn, int shardingCount,String shardingParam) {JobCoreConfiguration.Builder jobBuilder = JobCoreConfiguration.newBuilder(clazz.getSimpleName(), corn, shardingCount);if(!StringUtils.isEmpty(shardingParam)){jobBuilder.shardingItemParameters(shardingParam);}//定义作业核心配置newBuilder("任务名称","corn表达式","分片数量")JobCoreConfiguration simpleCoreConfig = jobBuilder.build();// 定义SIMPLE类型配置 cn.wolfcode.MyElasticJobSystem.out.println("MyElasticJob.class.getCanonicalName---->"+ MyElasticJob.class.getCanonicalName());SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig,clazz.getCanonicalName());//定义Lite作业根配置LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).overwrite(true).build();return simpleJobRootConfig;}@Bean(initMethod = "init")public SpringJobScheduler fileScheduler(FileCustomElasticjob job, CoordinatorRegistryCenter registryCenter){LiteJobConfiguration jobConfiguration = createJobConfiguration(job.getClass(),"0/10 * * * * ?",4,"0=text,1=image,2=radio,3=vedio");return new SpringJobScheduler(job,registryCenter,jobConfiguration);}}

第二步:修改任务类

@Component@Slf4jpublic class FileCustomElasticjob implements SimpleJob {@Autowiredprivate FileCustomMapper fileCustomMapper;@Overridepublic void execute(ShardingContext shardingContext) {doWork(shardingContext.getShardingParameter());log.info("线程ID:{},任务的名称:{},任务的参数:{},分片个数:{},分片索引号:{},分片参数:{}",Thread.currentThread().getId(),shardingContext.getJobName(),shardingContext.getJobParameter(),shardingContext.getShardingTotalCount(),shardingContext.getShardingItem(),shardingContext.getShardingParameter());}private void doWork(String shardingParameter) {//查询出所有的备份任务List<FileCustom> fileCustoms = fileCustomMapper.selectByType(shardingParameter);for (FileCustom custom:fileCustoms){backUp(custom);}}private void backUp(FileCustom custom){System.out.println("备份的方法名:"+custom.getName()+"备份的类型:"+custom.getType());System.out.println("=======================");//模拟进行备份操作try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}fileCustomMapper.changeState(custom.getId(),1);}}

第三步:修改Mapper映射文件

@Mapperpublic interface FileCustomMapper {@Select("select * from t_file_custom where backedUp = 0")List<FileCustom> selectAll();@Update("update t_file_custom set backedUp = #{state} where id = #{id}")int changeState(@Param("id") Long id, @Param("state")int state);@Select("select * from t_file_custom where backedUp = 0 and type = #{type}")List<FileCustom> selectByType(String shardingParameter);}

Dataflow类型调度

Dataflow类型的定时任务需要实现Dataflowjob接口,该接口提供2个方法供覆盖,分别用于抓取(fetchData)和处理( processData)数据,我们继续对例子进行改造。
Dataflow类型用于处理数据流,他和SimpleJob不同,它以数据流的方式执行,调用fetchData抓取数据,知道抓取不到数据才停止作业。

定时任务开始的时候,先抓取数据,判断数据是否为空,若不为空则进行处理数据

代码示例

第一步:创建任务类

@Componentpublic class FileDataflowJob implements DataflowJob<FileCustom> {@Autowiredprivate FileCustomMapper fileCustomMapper;//抓取数据@Overridepublic List<FileCustom> fetchData(ShardingContext shardingContext) {System.out.println("开始抓取数据......");List<FileCustom> fileCustoms = fileCustomMapper.selectLimit(2);return fileCustoms;}//处理数据@Overridepublic void processData(ShardingContext shardingContext, List<FileCustom> data) {for(FileCustom custom:data){backUp(custom);}}private void backUp(FileCustom custom){System.out.println("备份的方法名:"+custom.getName()+"备份的类型:"+custom.getType());System.out.println("=======================");//模拟进行备份操作try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}fileCustomMapper.changeState(custom.getId(),1);}}

第二步:创建任务配置类

@Configurationpublic class JobConfig {@Beanpublic static CoordinatorRegistryCenter registryCenter(@Value("${zookeeper.url}") String url, @Value("${zookeeper.groupName}") String groupName) {ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(url, groupName);//设置节点超时时间zookeeperConfiguration.setSessionTimeoutMilliseconds(100);//zookeeperConfiguration("zookeeper地址","项目名")CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);regCenter.init();return regCenter;}//功能的方法private static LiteJobConfiguration createJobConfiguration(Class clazz, String corn, int shardingCount,String shardingParam,boolean isDateFlowJob) {JobCoreConfiguration.Builder jobBuilder = JobCoreConfiguration.newBuilder(clazz.getSimpleName(), corn, shardingCount);if(!StringUtils.isEmpty(shardingParam)){jobBuilder.shardingItemParameters(shardingParam);}//定义作业核心配置newBuilder("任务名称","corn表达式","分片数量")JobCoreConfiguration simpleCoreConfig = jobBuilder.build();// 定义SIMPLE类型配置 cn.wolfcode.MyElasticJobJobTypeConfiguration jobConfiguration;if(isDateFlowJob){jobConfiguration = new DataflowJobConfiguration(simpleCoreConfig,clazz.getCanonicalName(),true);}else{jobConfiguration = new SimpleJobConfiguration(simpleCoreConfig,clazz.getCanonicalName());}//定义Lite作业根配置LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(jobConfiguration).overwrite(true).build();return simpleJobRootConfig;}@Bean(initMethod = "init")public SpringJobScheduler fileDatFlowaScheduler(FileDataflowJob job, CoordinatorRegistryCenter registryCenter){LiteJobConfiguration jobConfiguration = createJobConfiguration(job.getClass(),"0/10 * * * * ?",1,null,true);return new SpringJobScheduler(job,registryCenter,jobConfiguration);}}

第三步:创建Mapper映射文件

@Mapperpublic interface FileCustomMapper {@Update("update t_file_custom set backedUp = #{state} where id = #{id}")int changeState(@Param("id") Long id, @Param("state")int state);@Select("select * from t_file_custom where backedUp = 0 limit #{count}")List<FileCustom> selectLimit(int count);}

运维管理

事件追踪

Elastic-Job-Lite在配置中提供了JobEventConfiguration,支持数据库方式配置,会在数据库中自动创建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG两张表以及若干索引来近路作业的相关信息。

修改Elastic-job配置类

第一步:在ElasticJobConfig配置类中注入DataSource

在这里插入图片描述

第二步:在任务配置中增加事件追踪配置

在这里插入图片描述
运行结果

该表记录每次作业的执行历史,分为两个步骤:
1.作业开始执行时间想数据库插入数据
2.作业完成执行时向数据库更新数据,更新is_success,complete_time和failure_cause(如果任务执行失败)

在这里插入图片描述

该表记录作业状态变更痕迹表,可通过每次作业运行的task_id查询作业状态变化的生命轨迹和运行轨迹

在这里插入图片描述

运维平台

搭建步骤

1.解压缩

在这里插入图片描述

2.进入bin目录,并执行

bin\start.bat

3.打开浏览器访问http://localhost:8899
用户名:root 密码:root

在这里插入图片描述

使用步骤

第一步:注册中心配置

在这里插入图片描述

第二步:事件追踪数据源配置

在这里插入图片描述
之后就可以使用了
在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_255651.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python编程自动化办公案例(1)

作者简介&#xff1a;一名在校计算机学生、每天分享Python的学习经验、和学习笔记。 座右铭&#xff1a;低头赶路&#xff0c;敬事如仪 个人主页&#xff1a;网络豆的主页​​​​​​ 目录 前言 一.使用库讲解 1.xlrd 2.xlwt 二.主要案例 1.批量合并 模板如下&#xf…

Monkey

文章目录一、简介二、原理2.1 特殊处理三、命令3.1 启动3.2 关闭四、事件4.1 触摸事件4.2 手势事件4.3 二指缩放事件4.4 轨迹事件4.5 屏幕旋转事件4.6 基本导航事件4.7 主要导航事件4.8 系统按键事件4.9 启动activity事件4.10 键盘事件4.11 其他类型事件五、参数5.1 常规类参数…

go语言实现的一个基于go-zero框架的微服务影院票务系统cinema-ticket

一个基于go-zero框架的微服务影院票务系统cinema-ticket 前言 项目基本介绍 项目开源地址&#xff1a;butane123/cinema-ticket: 一个基于go-zero框架的微服务影院票务系统cinema-ticket (github.com) 这是一个微服务影院票务系统&#xff0c;基于go-zero框架实现&#xff0c…

【Java进阶打卡】JDBC- jdbc连接池

【Java进阶打卡】JDBC- jdbc连接池概述自定义数据库连接池归还连接-装饰设计模式归还连接-适配器设计模式动态代理动态代理-归还数据库连接概述 自定义数据库连接池 DataSource接口概述 javax.sql.DataSource接口&#xff1a;数据源&#xff08;数据库连接池&#xff09; Java…

LaoCat带你认识容器与镜像(实践篇二上)

实践篇主要以各容器的挂载和附加命令为主。 本章内容 本文实操全部基于Ubuntu 20.04 宿主机 > linux服务器本身 Docker > 20.10.22 在开始本章内容之前&#xff0c;我解答一个问题&#xff0c;有小伙伴问我说&#xff0c;有的容器DockerHub官网并没有提供任何可参考的文…

软件测试标准流程

软件测试的基本流程大概要经历四个阶段&#xff0c;分别是制定测试计划、测试需求分析、测试用例设计与编写以及测试用例评审。因此软件测试的工作内容&#xff0c;远远没有许多人想象的只是找出bug那么简单。准确的说&#xff0c;从一个项目立项以后&#xff0c;软件测试从业者…

【项目精选】基于Java的敬老院管理系统的设计和实现

本系统主要是针对敬老院工作人员即管理员和员工设计的。敬老院管理系统 将IT技术为养老院提供一个接口便于管理信息,存储老人个人信息和其他信息,查找 和更新信息的养老院档案,节省了员工的劳动时间,大大降低了成本。 其主要功能包括&#xff1a; 系统管理员用户功能介绍&#…

面临激烈竞争的汽车之家仍有新的增长机会

来源&#xff1a;猛兽财经 作者&#xff1a;猛兽财经 竞争激烈是汽车之家面临的主要问题 在汽车之家&#xff08;ATHM&#xff09;2021财年的20-F文件中&#xff0c;汽车之家将自己描述为中国最大的“汽车服务平台”运营商&#xff0c;但行业数据却显示&#xff0c;汽车之家的…

Python 如何快速搭建环境?

Python可应用于多平台包括 Linux 和 Mac OS X。 你可以通过终端窗口输入 “python” 命令来查看本地是否已经安装Python以及Python的安装版本。 Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, 等等。) Win 9x/NT/2000 Macintosh (Intel, PPC, 68K) OS/2 DOS (多个…

10条终身受益的Salesforce职业发展建议!

Salesforce这个千亿美金巨兽&#xff0c;在全球范围内有42,000多名员工。作为一家发展迅速的科技公司&#xff0c;一直在招聘各种角色&#xff0c;包括销售、营销、工程师和管理人员等。 据IDC估计&#xff0c;从2016年到2020年&#xff0c;该生态系统创造了190万个工作岗位。…

训练营day16

104.二叉树的最大深度 559.n叉树的最大深度111.二叉树的最小深度222.完全二叉树的节点个数104.二叉树的最大深度 力扣题目链接 给定一个二叉树&#xff0c;找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没有子节点的节点。 示…

Java基础-网络编程

1. 网络编程入门 1.1 网络编程概述 计算机网络 是指将地理位置不同的具有独立功能的多台计算机及其外部设备&#xff0c;通过通信线路连接起来&#xff0c;在网络操作系统&#xff0c;网络管理软件及网络通信协议的管理和协调下&#xff0c;实现资源共享和信息传递的计算机系统…

Vue中路由缓存及activated与deactivated的详解

目录前言一&#xff0c;路由缓存1.1 引子1.2 路由缓存的方法1.2.1 keep-alive1.2.2 keep-alive标签中的include属性1.2.3 include中多组件的配置二&#xff0c;activated与deactivated2.1 引子2.2 介绍activated与deactivated2.3 解决需求三&#xff0c;整体代码总结前言 在Vu…

【深度学习基础8】卷积神经网络 经典网络

一、卷积操作 1. 基本原理 相信大家对卷积操作并不陌生,先来回顾一下卷积的工作原理(2-D):👇 卷积的目的是进行特征提取,不同的卷积核可以提取到不同的特征,比如下面的三个卷积核的功能分别是:模糊化、锐化、边缘化👇 卷积的本质就是滤波器, 将滤波器沿着图像…

【JavaScript】面向对象和构造函数详解

&#x1f4bb; 【JavaScript】面向对象和构造函数详解 &#x1f3e0;专栏&#xff1a;JavaScript &#x1f440;个人主页&#xff1a;繁星学编程&#x1f341; &#x1f9d1;个人简介&#xff1a;一个不断提高自我的平凡人&#x1f680; &#x1f50a;分享方向&#xff1a;目前…

加拿大访问学者家属如何办理探亲签证?

由于大多数访问学者的访学期限都为一年&#xff0c;家人来访不仅可以缓解访学的寂寞生活&#xff0c;而且也是家人到加拿大体验国外风情的好机会。家属在国内申请赴加签证时&#xff0c;如果材料齐全&#xff0c;一般上午递交了申请&#xff0c;下午就可以拿到签证。以下是家人…

基于merlin使用chatGPT进行对话

最近chatGPT很热&#xff0c;大家都想试用它。但由于各种限制&#xff0c;一般情况下国内不能试用。 下面给大家介绍基于merlin使用chatGPT&#xff08;目前每天只有11次问答次数&#xff09;。 1 打开merlin页面 访问地址merlin.foyer.work&#xff0c;点击“add to chrome”…

流程控制之循环

文章目录五、流程控制之循环5.1 步进循环语句for5.1.1 带列表的for循环语句5.1.2 不带列表的for循环语句5.1.3 类C风格的for循环语句5.2 while循环语句5.2.1 while循环读取文件5.2.2 while循环语句示例5.3 until循环语句5.4 select循环语句5.5 嵌套循环5.4 利用break和continue…

Elasticsearch安装IK分词器、配置自定义分词词库

一、分词简介 在Elasticsearch中&#xff0c;假设搜索条件是“华为手机平板电脑”&#xff0c;要求是只要满足了其中任意一个词语组合的数据都要查询出来。借助 Elasticseach 的文本分析功能可以轻松将搜索条件进行分词处理&#xff0c;再结合倒排索引实现快速检索。Elasticse…

crawler爬虫抓取数据

crawler爬虫实现 学习目标&#xff1a; 了解 crawler爬虫运行流程了解 crawler爬虫模块实现 1. crawler功能 初始化driver输入公司名称,并点击判断是否需要验证如果需要验证&#xff0c;获取验证图片并保存获取打码坐标点击验证图片判断查询结果选择第一条查询结果获取主要信…