Valley Grain Academy_10th Heaven

tenth day

video deletion

rear end

Relevant sdk is in the server-side SDK of Alibaba Cloud Video on Demand Documentation –> Java SDK –> Media Asset Management –> Delete Video

Copy the previous InitObject to utils

delete method


    @DeleteMapping("{id}")
    public R removeAliyunVideo(@PathVariable String id){
    
    
        try{
    
    
            DefaultAcsClient defaultAcsClient = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建一个删除视频request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            //设置id
            deleteVideoRequest.setVideoIds(id);
            //获取结果
            DeleteVideoResponse acsResponse = defaultAcsClient.getAcsResponse(deleteVideoRequest);
            return R.ok();
            //初始化对象
        }catch (Exception e){
    
    
            throw new GuliException(20001,"删除视频失败");
        }

    }

front end

video.js added

, 
  //删除视频
  deleteAliyunVideo(id){
    return request({
      
        url: 'http://localhost:8003/eduvod/video/'+id,
        method:'delete'
    })
  }
//视频操作
      //上传成功的方法List
      handleVodUploadSuccess(response,file,fileList){
    
    

        console.log(response.data.videoId)
        this.video.videoSourceId=response.data.videoId
        this.video.videoOriginalName=file.name

      },
      //上传前

      handleUploadExceed(files, fileList){
    
    
        this.$message.warning('如果要重新上传,请先删除已有视频')
      },


      //删除上传
      beforeVodRemove(file,fileList){
    
    
        return this.$confirm(`确认删除 ${
      
      file.name}?`);


      },
      //点击确认调用的方法
      handleVodRemove(){
    
    
        console.log("删除api")
        videoapi.deleteAliyunVideo(this.video.videoSourceId)
        .then(response=>{
    
    
          //提示信息
          this.message({
    
    
            type: 'success',
            message: '视频删除成功'
          });
          //文件列表清空
          this.fileList= []
          this.video.videoSourceId=''
          this.video.videoOriginalName=''
        })
       
      },

microservice

is an architectural style

There are multiple services, each service runs independently, does not affect each other, multiple services run independently, and the service occupies an independent process

springcloud

It is a type of microservice, not a technology/framework itself, but a collection of many technologies/frameworks

It needs to be implemented based on springboot

Basic service components

Service DiscoveryNetfix Eureka ( Nacos )
Service CallNetfix Feign
FuseNetfix Hystrix
Service GatewaySpring Cloud GateWayDistributed
ConfigurationSpring Cloud Config ( Nacos )
Message BusSpring Cloud Bus ( Nacos )

The version of springcloud corresponds to the version of springboot

Need to inquire on the official website

https://spring.io/projects/spring-cloud#overview

The name in the front is based on the name of the London subway station, and the year and time version is used directly if there is no writing in the back

GA: stable version

SNAPSHOT: snapshot version, updated at any time

M: Milestone Edition

SR: official version

CURRENT: current recommended version

Better not choose SNAPSHOT

Click on the following detailed documents

Here are the corresponding two versions:

insert image description here

Nacos

Similar to an intermediary, all services are registered to the registration center, and services that require certain functions can be implemented by calling other services

Common registry

Eureka: Eureka was used for early service registration, but it encountered performance bottlenecks and was no longer maintained

Zookeeper: can cooperate with dubbo

Consul: developed with go (prohibited (China) use?)

Nacos implementation principle

(Consumers with calling actions, and producers called)

(The identity of the producer and consumer is relative, depending on who calls who)

Principle of the main function

insert image description here

There is also a heartbeat mechanism (connection confirmation)

Self-protection mechanism (to ensure that there is no avalanche)

Dynamic dns (flexible network optimization)

Docker install Nacos

Official website document

//or see me this

Get Started Quickly with Nacos (Docker)

Pull mirror:

docker pull nacos/nacos-server

Create a mapping file:

mkdir -p /root/nacos/init.d /root/nacos/logs
touch /root/nacos/init.d/custom.properties

write something

vim /root/nacos/init.d/custom.properties

management.endpoints.web.exposure.include=*

Then save and exit vim

Build container:

docker run -d -p 8848:8848 -e MODE=standalone -e PREFER_HOST_MODE=hostname -v /root/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties -v /root/nacos/logs:/home/nacos/logs --restart always --name nacos nacos/nacos-server

Run the container:

docker start nacos

address:

http://server ip:8848/nacos/#/login

Username: nacos

Password: nacos

insert image description here

registration service

Introduce nacos dependencies:

In the pom.xml of service_edu and service

<!--        服务注册-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

service_edu configuration

application

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.199.100:8848 #服务地址

Start class annotation

@EnableDiscoveryClient

Started successfully and found
insert image description here

In the same way, add vod to the dependency annotation configuration

insert image description here

Feign

for service calls

rely:

<!--        服务调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

Caller (service_edu) note:

Open feign client

@EnableFeignClients

Caller interface:

service_edu creates a client.VodClient

package com.lkw.eduservice.client;


import com.lkw.commonutils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient("service-vod")//被调用的服务的名字
@Component
public interface VodClient {
    
    
    //定义方法路径
    //把service_vod的方法完完全全的复制来,路径改成完全路径
    @DeleteMapping("eduvod/video/{id}")
    public R removeAliyunVideo(@PathVariable("id") String id);
}

implement the call

Inject vodClient in the deleteVideo call of EduVideoController

@Autowired
private VodClient vodClient;

An error occurred:

no server available

It is because modules that depend on nacos must be configured and added for service registration

Maybe the oss just now has no nacos configuration or annotations

batch deletion

To transfer multiple ids, you can use List

Write the controller of vodservice first

//删除多个视频的方法
@DeleteMapping("delete-batch")
public R deleteBatch(@RequestParam("videoIdList") List videoIdList) {
    
    

    vodService.removeMoreAlyVideo(videoIdList);
    return R.ok();
}

Then implement the implementation class of the service interface

@Override
public void removeMoreAlyVideo(List videoIdList) {
    
    
    try {
    
    
        DefaultAcsClient defaultAcsClient = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET);
        //创建一个删除视频request对象
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        //遍历用逗号拼接
        String videoIds = StringUtils.join(videoIdList.toArray(), ",");//数组加分隔符拼接成字符串//或者听说String.join(",",list)也行
        //设置id
        deleteVideoRequest.setVideoIds(videoIds);
        //获取结果
        DeleteVideoResponse acsResponse = defaultAcsClient.getAcsResponse(deleteVideoRequest);
    }catch(ClientException e){
    
    
        throw new GuliException(20001,"视频删除失败");
    }
}

Add interface method in VodClient:

//删除多个视频的方法
@DeleteMapping("eduvod/video/delete-batch")
public R deleteBatch(@RequestParam("videoIdList") List videoIdList);

Write the calling method in EduVideoServiceImpl

// first injection

    //注入
    @Autowired
    VodClient vodClient;

Change to:

@Override
public void removeVideoByCourseId(String courseId) {


    //根据课程id查询课程所有视频id
    QueryWrapper<EduVideo> wrapperVod=new QueryWrapper<>();
    wrapperVod.eq("course_id",courseId);
    wrapperVod.select("video_source_id");
    List<EduVideo> eduVideoList = baseMapper.selectList(wrapperVod);

    List<String> videoIds = eduVideoList.stream().map(EduVideo::getVideoSourceId).collect(Collectors.toList());

    vodClient.deleteBatch(videoIds);

    QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
    wrapper.eq("course_id",courseId);
    baseMapper.delete(wrapper);
    //TODO 删除对应视频的文件
}

stockings brother test

Slightly, if there is a bug in the front end, don’t fix it first, read it after learning

Hystrix

is a fuse

The fuse is a certain amount of time? No response to the request, and then

Fuse pre-knowledge

Springcloud's experience in interface calls:

interface request call

Feign---- call according to the service name ---->Hystrix---- unable to adjust disconnection ---->Ribbon---- load balancing- >Http Client

Hystrix provides delay fault tolerance

use

at service_edu

rely:

Nacos automatically introduces ribbon openfeign and automatically introduces hystrix

Configuration:

feign:
  hystrix:
    enabled: true

It seems that the timeout period cannot be configured in yml

Create a fuse implementation class:

package com.lkw.eduservice.client.impl;

import com.lkw.commonutils.R;
import com.lkw.eduservice.client.VodClient;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class VodClientImpl implements VodClient {
    
    
    @Override
    public R removeAliyunVideo(String id) {
    
    
        return R.error().message("删除视频错误");
    }



    @Override
    public R deleteBatch(List videoIdList) {
    
    
        return R.error().message("删除多个视频错误");
    }
}

modify comment

@FeignClient(name="service-vod",fallback = VodClientImpl.class)//被调用的服务的名字,熔断器的实现类

SupplementeduvideoController

//刪除小节
//删除小节同时把小节中的视频删除
@DeleteMapping("{id}")
public R deleteVideo(@PathVariable String id) {
    
    
    System.out.println(id);
    //根据小节id查询出视频id,进行删除
    EduVideo eduVideobyId = eduVideoService.getById(id);
    String videoSourceId = eduVideobyId.getVideoSourceId();
    //判断是否有视频,有就删除
    if (!StringUtils.isEmpty(videoSourceId)) {
    
    
        //远程调用vod删除视频
        R r = vodClient.removeVideo(videoSourceId);
        if(r.getCode()==20001)
            throw new GuliException(20001,"删除视频失败");
    }
    //删除小节
    eduVideoService.removeById(id);
    return R.ok();
}

breakpoint debugging

slightly

Guess you like

Origin blog.csdn.net/m0_52070517/article/details/128050624