Java的新项目学成在线笔记-day15(八)

 

3.3  Api接口 
此api接口是课程学习页面请求学习服务获取课程学习地址。
定义返回值类型:
 spring

[AppleScript] 纯文本查看 复制代码api

?app

1post

2学习

3fetch

4this

5spa

@Data @ToString @NoArgsConstructor public class GetMediaResult extends ResponseResult {     public GetMediaResult(ResultCode resultCode, String fileUrl) {         super(resultCode);       .net

  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中。
一、查看服务名称是否为xc-service-search
 

[AppleScript] 纯文本查看 复制代码

?

1

2

注意修改application.xml中的服务名称: spring:   application:  

  name: xc‐service‐search


二、配置搜索服务的配置文件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 #请求处理的超时时间


三、添加eureka依赖:
 

[AppleScript] 纯文本查看 复制代码

?

1

2

3

4

5

<!‐‐ 导入Eureka客户端的依赖 ‐‐>

 <dependency> 

  <groupId>org.springframework.cloud</groupId>

   <artifactId>spring‐cloud‐starter‐netflix‐eureka‐client</artifactId>

  </dependency>


四、修改启动类,在class上添加以下注解:
 

[AppleScript] 纯文本查看 复制代码

?

1

@EnableDiscoveryClient


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)

}