目录
前言
一、背景介绍
二、集成
1.引入OpenFeign依赖
2.各模块代码示例
2.1 provider
2.2 api
2.3 consumer
总结
前言
Openfeign是一种声明式,模板化的http客户端,只需创建一个接口并添加注解就可以使用,它还整合了Ribbon实现负载均衡。
一、背景介绍
项目包含三个模块(module):consumer,api,provider。模拟的业务场景是consumer通过调用api模块提供的接口来调用provider实现文件下载。
consumer模块:依赖api模块,并通过调用api模块提供的接口来调用provider接口
api模块: 封装FeignClient接口
provider模块: 业务实现类,提供文件下载的接口
二、集成
1.引入OpenFeign依赖
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
2.各模块代码示例
2.1 provider
作为服务提供方,提供一个服务下载的接口
@GetMapping("test/download/file")
public void testDowanload(@RequestParam String filename, HttpServletResponse response) {
File file = new File("E:/ZTest/test.txt");
String fileName = "test";
response.setHeader("content-type","application/octet-stream");
response.setContentType("application/octet-stream");
try {
response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(fileName+ ".txt","UTF-8"));
} catch (UnsupportedEncodingException e) {
//throw new Exception("错误信息");
}
byte[] buff = new byte[1024];
FileInputStream bis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
bis = new FileInputStream(file);
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e2){
e2.printStackTrace();
} finally {
if(bis != null){
try{
bis.close();
//os.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
}
2.2 api
基于OpenFeign封装服务下载的接口。api层其实也可以作为一个模块集成在provider里面,此处为了清晰,我们把api单独提出来,废话不多说上api层的代码:
package com.rock.demo.feign.api;
@FeignClient(name = "feignTest", url = "localhost:8081/feign",
configuration= DemoFeignApi.FeignAPIConfiguration.class)
public interface DemoFeignApi {
@GetMapping(value = "/test/download/file")
Response download(@RequestParam("filename") String fileName);
class FeignAPIConfiguration {
@Bean
public Logger feignLogger() {
return new Slf4jLogger();
}
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
}
这里有两点需要特别注意的:
1.注意download方法的返回类型是Respone,并且是feign.Response,这是通过OpenFeign进行文件下载所必须的.
2.通过@FeignClient注解后面的url,可以知道OpenFeign支持单独使用或与注册中心联用。
2.3 consumer
该模块依赖API模块,通过调用API层的接口,获取下载的文件。
1.使用OpenFeign,要在启动类中添加@EnableFeignClients注解
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
2.pom.xml添加依赖
<dependency>
<groupId>com.rock.demo</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
3.调用OpenFeign接口实现获取文件内容
需要注意HttpResponse的ContentType和Header,"application/octet-stream" 代表二进制数据传输.
@GetMapping("/test/file/download")
public void testDownload(@RequestParam String filename, HttpServletResponse httpResponse){
/*设置参数*/
Response feignResponse= demoFeignApi.download(filename);
try {
httpResponse.reset();
httpResponse.setHeader("content-type","application/octet-stream");
httpResponse.setContentType("application/octet-stream");
httpResponse.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(filename+ ".txt" ,"UTF-8"));
ServletOutputStream outputStream = httpResponse.getOutputStream();
Response.Body body = feignResponse.body();
InputStream inputStream = body.asInputStream();
IOUtils.copy(inputStream,outputStream);
}catch (Exception ex){
ex.printStackTrace();
}
}
总结
本文介绍了如何在SpringBoot中集成Openfeign, 并以文件下载为案例,介绍了如何使用openfeign,以及使用openfeign下载文件时的注意事项。
|