微服务 分片 运维管理(上)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 微服务 分片 运维管理(上)


分片

分片的概念

当只有一台机器的情况下,给定时任务分片四个,在机器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%2B8
    driverClassName: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: 2022

第三步:添加实体类

@Data
    public 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;
        }
    }

第四步:添加任务类

        @Autowired
        private FileCustomMapper fileCustomMapper;
        @Override
        public 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);
    }

案例改造成任务分片

第一步:修改任务配置类

@Configuration
    public class JobConfig {
        @Bean
        public 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.MyElasticJob
            System.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
    @Slf4j
    public class FileCustomElasticjob implements SimpleJob {
        @Autowired
        private FileCustomMapper fileCustomMapper;
        @Override
        public 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映射文件

@Mapper
    public 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);
    }
相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
目录
相关文章
|
6月前
|
存储 运维 数据安全/隐私保护
微服务应用运维入门
微服务应用运维入门
|
1月前
|
人工智能 运维 监控
构建高性能微服务架构:现代后端开发的挑战与策略构建高效自动化运维系统的关键策略
【2月更文挑战第30天】 随着企业应用的复杂性增加,传统的单体应用架构已经难以满足快速迭代和高可用性的需求。微服务架构作为解决方案,以其服务的细粒度、独立性和弹性而受到青睐。本文将深入探讨如何构建一个高性能的微服务系统,包括关键的设计原则、常用的技术栈选择以及性能优化的最佳实践。我们将分析微服务在处理分布式事务、数据一致性以及服务发现等方面的挑战,并提出相应的解决策略。通过实例分析和案例研究,我们的目标是为后端开发人员提供一套实用的指南,帮助他们构建出既能快速响应市场变化,又能保持高效率和稳定性的微服务系统。 【2月更文挑战第30天】随着信息技术的飞速发展,企业对于信息系统的稳定性和效率要求
|
1月前
|
Kubernetes 安全 Java
运维人少,如何批量管理上百个微服务、上千条流水线?
云效 AppStack 平台针对微服务和云原生环境下的应用管理难题,提供了以应用为中心的资源、流水线和权限管理解决方案。
|
1月前
|
运维 应用服务中间件 调度
微服务容器化的运维
【2月更文挑战第27天】
|
6月前
|
运维 监控 数据安全/隐私保护
微服务应用运维的注意事项
微服务应用运维的注意事项
|
6月前
|
运维 负载均衡 监控
微服务应用运维
微服务应用运维
|
8月前
|
运维 调度 数据库
微服务 分片 运维管理(下)
微服务 分片 运维管理(下)
60 0
|
10月前
|
运维 SpringCloudAlibaba 安全
SpringCloud Alibaba微服务运维二 - 集成ELK日志
SpringCloud Alibaba微服务运维二 - 集成ELK日志
499 0
|
10月前
|
存储 运维 SpringCloudAlibaba
SpringCloud Alibaba微服务运维一 - 集成SkyWalking
SpringCloud Alibaba微服务运维一 - 集成SkyWalking
495 0
|
11月前
|
自然语言处理 运维 监控
《云原生架构容器&微服务优秀案例集》——01 互联网——站酷 基于 ASM 解决多语言技术栈下服务管理难题,实现运维提效
《云原生架构容器&微服务优秀案例集》——01 互联网——站酷 基于 ASM 解决多语言技术栈下服务管理难题,实现运维提效
203 0

热门文章

最新文章