3.3 Api接口
此api接口是课程学习页面请求学习服务获取课程学习地址。
定义返回值类型:
[AppleScript] 纯文本查看 复制代码
?
1 2 3 4 5 | @Data @ToString @NoArgsConstructor public class GetMediaResult extends ResponseResult { public GetMediaResult ( ResultCode resultCode , String fileUrl ) { super ( resultCode ) ; this.fileUrl = fileUrl; } / / 媒资文件播放地址 private String fileUrl; } |
定义接口,学习服务根据传入课程ID、章节Id(课程计划ID)来取学习地址。
[AppleScript] 纯文本查看 复制代码
?
1 2 3 | @Api ( value = "录播课程学习管理" , description = "录播课程学习管理" ) public interface CourseLearningControllerApi { @ApiOperation ( "获取课程学习地址" ) public GetMediaResult getmedia ( String courseId , String teachplanId ) ; } |
3.4 服务端开发
3.4.1 需求分析
学习服务根据传入课程ID、章节Id(课程计划ID)请求搜索服务获取学习地址。
3.4.2 搜索服务注册Eureka
学习服务要调用搜索服务查询课程媒资信息,所以需要将搜索服务注册到eureka中。
1、查看服务名称是否为xc-service-search
[AppleScript] 纯文本查看 复制代码
?
1 2 | 注意修改 application .xml中的服务名称: spring : application : name : xc‐service‐search |
2、配置搜索服务的配置文件application.yml,加入Eureka配置 如下:
[AppleScript] 纯文本查看 复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13 | eureka : client : registerWithEureka : true #服务注册开关 fetchRegistry : true #服务发现开关 serviceUrl : #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔 defaultZone: ${EUREKA_SERVER:http://localhost:50101/eureka/,http://localhost:50102/eureka/} instance : prefer‐ip‐ address : true #将自己的ip地址注册到Eureka服务中 ip‐ address : $ { IP_ADDRESS : 127.0 . 0.1 } instance‐ id : $ { spring. application . name } : $ { server.port } #指定实例id ribbon: MaxAutoRetries: 2 #最大重试次数,当Eureka中可以找到服务,但是服务连不上时将会重试,如果eureka中找不 到服务则直接走断路器 MaxAutoRetriesNextServer : 3 #切换实例的重试次数 OkToRetryOnAllOperations: false #对所有操作请求都进行重试,如果是get则可以,如果是post,put等操作 没有实现幂等的情况下是很危险的,所以设置为false ConnectTimeout : 5000 #请求连接的超时时间 ReadTimeout : 6000 #请求处理的超时时间 |
3、添加eureka依赖:
[AppleScript] 纯文本查看 复制代码
?
1 2 3 4 5 | < !‐‐ 导入Eureka客户端的依赖 ‐‐ > < dependency > < groupId > org.springframework.cloud < / groupId > < artifactId > spring‐cloud‐starter‐netflix‐eureka‐client < / artifactId > < / dependency > |
4、修改启动类,在class上添加如下注解:
[AppleScript] 纯文本查看 复制代码
?
3.4.3 搜索服务客户端
在学习服务创建搜索服务的客户端接口,此接口会生成代理对象,调用搜索服务:
[AppleScript] 纯文本查看 复制代码
?
1 2 3 4 5 6 7 8 | package com.xuecheng.learning.client; import com.xuecheng.api.search.EsCourseControllerApi; import com.xuecheng.framework.client.XcServiceList; import org.springframework.cloud.netflix.feign.FeignClient; @FeignClient ( value = "xc‐service‐search" ) public interface CourseSearchClient { @GetMapping ( value = "/getmedia/{teachplanId}" ) public TeachplanMediaPub getmedia ( @PathVariable ( "teachplanId" ) String teachplanId ) ; } |