课程发布业务流程

一、需求分析

1.通过id获取course对象,判断课程是否为空

AssertUtil.isNotNull(courseId,"课程id错误");
Course course = super.selectById(courseId);
AssertUtil.isNotNull(course,"课程不存在");

2.后台根据课程ID查询课程,判断课程是否是下架,

 AssertUtil.isEquals(course.getStatus(), AllConstants.CourseConstants.STATUS_OFF_LINE,"课程出现异常了");

3.修改课程状态为上架

course.setStatus(AllConstants.CourseConstants.STATUS_ON_LINE);
course.setOnlineTime(new Date());
super.updateById(course);

4.把课程保存到远程ES中,需要调用ES微服务的Feign接口来完成。

        CourseDoc courseDoc = new CourseDoc();
        CourseMarket courseMarket = courseMarketService.selectById(courseId);
        courseMarket.setPrice(courseDoc.getPrice());
        courseDoc.setCharge(courseMarket.getCharge()==1?"免费":"付费");

        CourseDetail courseDetail = courseDetailService.selectById(courseId);
        courseDetail.setDescription(courseDoc.getDescription());

        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(courseDetail,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);
        JSONResult jsonResult = courseFeignClient.save(courseDoc);
        AssertUtil.isTrue(jsonResult.isSuccess(), jsonResult.getMessage());

二、创建一个search模块(alibaba-edu-service-search、alibaba-edu-pojo-search),是为了使用ES搜索,步骤如下:

1.alibaba-edu-service-search微服务中步骤

1.1.编写启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class SearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(SearchApplication.class);
    }
}

1.2.导入 alibaba-edu-pojo-search

<dependency>
  <groupId>com.yl</groupId>
  <artifactId>alibaba-edu-pojo-search</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

1.3.添加配置

spring:
  application:
    name: service-search
  elasticsearch:
    rest:
      uris: http://localhost:9200

1.4.在alibaba-edu-service-search微服务中写一个接口继承ElasticsearchRepository<实体类Doc,id类型>

import com.yl.ymcc.coursedoc.CourseDoc;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CourseEsRepository extends ElasticsearchRepository<CourseDoc,Long> {

}

1.5.Controller层写一个接口

import com.yl.result.JSONResult;
import com.yl.ymcc.coursedoc.CourseDoc;
import com.yl.ymcc.service.impl.CourseEsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/es/course")
public class CourseController {

    @Autowired
    private CourseEsServiceImpl courseEsService;


    @PostMapping("/save")
    public JSONResult save(@RequestBody CourseDoc doc){
        courseEsService.save(doc);
        return JSONResult.success();
    }
}

1.6.Service实现类中:注入CouserEsRepository

import com.yl.ymcc.coursedoc.CourseDoc;
import com.yl.ymcc.repository.CourseEsRepository;
import com.yl.ymcc.service.ICourseEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CourseEsServiceImpl implements ICourseEsService {

    @Autowired
    private CourseEsRepository courseEsRepository;

    @Override
    public void save(CourseDoc doc) {
        courseEsRepository.save(doc);
    }
}

2.alibaba-edu-pojo-search模块中的步骤

2.1.导入依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    </dependencies>

2.2.写一个DOC实体类

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.math.BigDecimal;
import java.util.Date;

@Data
@Document(indexName = "course",type = "_doc")
public class CourseDoc {

    @Id
    private Long id;
    // 课程名称
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String name;
    // 课程等级
    @Field(type = FieldType.Keyword)
    private Long gradeId;
    @Field(type = FieldType.Integer)
    private Integer status;
    @Field(type = FieldType.Keyword)
    private String pic;
    @Field(type = FieldType.Date)
    private Date onlineTime;
    // 收费
    @Field(type = FieldType.Keyword)
    private String description;
    @Field(type = FieldType.Keyword)
    private String charge;
    @Field(type = FieldType.Double)
    private BigDecimal price;
    @Field(type = FieldType.Double)
    private BigDecimal priceOld;
    @Field(type = FieldType.Integer)
    private Integer saleCount;
    @Field(type = FieldType.Integer)
    private Integer viewCount;
    @Field(type = FieldType.Integer)
    private Integer commentCount;
}

三、抽一个alibaba-edu-api-course模块

1.导入feign依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2.写一个feign接口和降级类

 feign接口

//feign接口,方法与调用的服务controller中的接口要一致
import com.yl.result.JSONResult;
import com.yl.ymcc.coursedoc.CourseDoc;
import com.yl.ymcc.fallback.CourseFeignClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "service-search",fallbackFactory = CourseFeignClientFallbackFactory.class)
public interface CourseFeignClient {

    @PostMapping("/es/course/save")
    JSONResult save(@RequestBody CourseDoc doc);
}

 降级类

//降级类
import com.yl.result.JSONResult;
import com.yl.ymcc.clients.CourseFeignClient;
import com.yl.ymcc.coursedoc.CourseDoc;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class CourseFeignClientFallbackFactory implements FallbackFactory<CourseFeignClient> {
    @Override
    public CourseFeignClient create(Throwable throwable) {
        return new CourseFeignClient() {
            @Override
            public JSONResult save(CourseDoc doc) {
                throwable.printStackTrace();
                return JSONResult.error("发布失败");
            }
        };
    }
}

四、alibaba-edu-service-course模块中调用feign

1.导入alibaba-edu-api-course模块

        <dependency>
            <groupId>com.yl</groupId>
            <artifactId>alibaba-edu-api-course</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2.启动类中开启feign

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.yl.ymcc") // 开启feign
public class CourseApplication {
    public static void main(String[] args) {
        SpringApplication.run(CourseApplication.class);
    }
}

3.配置中开启降级

feign:
  sentinel:
    enabled: true 

3. Service实现类中注入feign接口

    @Autowired
    private CourseFeignClient courseFeignClient;

4.使用

  JSONResult jsonResult = courseFeignClient.save(courseDoc);
        AssertUtil.isTrue(jsonResult.isSuccess(), jsonResult.getMessage());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值