使用feign调用媒资服务完成静态页面的上传

依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Cloud 微服务远程调用 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>
<!--feign支持Multipart格式传参-->
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.8.0</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.8.0</version>
</dependency>

在nacos配置feign-dev.yaml公用配置文件

feign:
  hystrix:
    enabled: true
  circuitbreaker:
    enabled: true
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000  #熔断超时时间
ribbon:
  ConnectTimeout: 60000 #连接超时时间
  ReadTimeout: 60000 #读超时时间
  MaxAutoRetries: 0 #重试次数
  MaxAutoRetriesNextServer: 1 #切换实例的重试次数

ribbon:

  1. ConnectTimeout:

    • 描述: 连接超时时间(毫秒)。
    • 默认值: 通常依赖于客户端的HTTP库(如Apache HttpClient或Netty)的默认设置。
    • 解释: 这个设置决定了在建立与服务的连接时,客户端愿意等待的最长时间。如果在这个时间内没有成功建立连接,那么会抛出超时异常。
  2. ReadTimeout:

    • 描述: 读超时时间(毫秒)。
    • 默认值: 同样依赖于客户端的HTTP库。
    • 解释: 一旦与服务建立了连接,这个设置决定了客户端等待服务响应的最长时间。如果在这个时间内没有收到任何响应,那么会抛出超时异常。
  3. MaxAutoRetries:

    • 描述: 针对同一个服务实例的最大自动重试次数。
    • 默认值: 通常是1。
    • 解释: 当一个请求失败时(例如,由于超时、连接拒绝等),Ribbon会尝试重新发送请求到相同的服务实例。这个设置决定了针对同一个实例的最大重试次数。如果设置为0,那么不会进行重试。
  4. MaxAutoRetriesNextServer:

    • 描述: 切换到下一个服务实例之前的最大自动重试次数。
    • 默认值: 通常是1。
    • 解释: 当一个请求对某个服务实例失败并且已经进行了MaxAutoRetries次重试后,Ribbon会尝试将请求发送到负载均衡器中的下一个服务实例。这个设置决定了在切换到下一个实例之前,对前一个实例的最大重试次数。

hystrix:

  1. hystrix: 这是Hystrix的配置根节点。
  2. command: 用于配置Hystrix命令的节点。
  3. default: 表示这是一个默认配置,它将应用于没有指定特定配置的Hystrix命令。
  4. execution: 配置与命令执行相关的设置。
  5. isolation: 配置命令的隔离策略。Hystrix支持两种隔离策略:线程隔离(thread)和信号量隔离(semaphore)。在这里,我们使用了线程隔离。
  6. thread: 指定使用线程隔离策略。
  7. timeoutInMilliseconds: 设置命令执行的超时时间。如果在这个时间内命令没有完成,Hystrix会触发断路,并返回一个回退值或抛出异常。在这里,超时时间被设置为30000毫秒,即30秒

在内容管理service工程和内容管理api工程都引入此配置文件

shared-configs:
  - data-id: feign-${spring.profiles.active}.yaml
    group: xuecheng-plus-common
    refresh: true

在内容管理service工程配置feign支持Multipart 

@Configuration
public class MultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary//注入相同类型的bean时优先使用
    @Scope("prototype")
    public Encoder feignEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    //将file转为Multipart
    public static MultipartFile getMultipartFile(File file) {
        FileItem item = new DiskFileItemFactory().createItem("file", MediaType.MULTIPART_FORM_DATA_VALUE, true, file.getName());
        try (FileInputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = item.getOutputStream();) {
            IOUtils.copy(inputStream, outputStream);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonsMultipartFile(item);
    }
}

编写feign接口

/**
 * @description 媒资管理服务远程接口
 * @author Mr.M
 * @date 2022/9/20 20:29
 * @version 1.0
 */
 @FeignClient(value = "media-api",configuration = MultipartSupportConfig.class)
public interface MediaServiceClient {

 @RequestMapping(value = "/media/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
 String uploadFile(@RequestPart("filedata") MultipartFile upload,@RequestParam(value = "objectName",required=false) String objectName);
}

在启动类添加@EnableFeignClients注解

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉普兰德做的到吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值