简介:
在SpringBoot工程下使用OpenFeign进行服务间调用(未引入SpingCloud)
注:本次实例未引入分布式架构(SpringCloud),使用方式⑤
在实际开发中,经常需要调用其他服务提供的接口,因此也出现了很多成熟的工具包,在Java项目中调用http接口的方式有:
①通过JDK网络类Java.net.HttpURLConnection;
②通过common封装好的HttpClient;
③通过Apache封装好的CloseableHttpClient;
④通过Spring-RestTemplate;
⑤通过GitHub-OpenFeign-Feign;
⑥通过SpringCloud-Fegin;
目前④⑤⑥用的是最多的,在分布式架构SpringCloud时首选SpringCloud-Fegin组件,其余情况RestTemplate和OpenFeign各有各的优势。前一代访问restful服务,一般使用Apache的HttpClient,不过此种方法使用起来太过繁琐,现在使用的很少。
Feign 和 OpenFeign 两者区别:
Feign | OpenFeign |
---|---|
Feign是Springcloud组件中的一个轻量级Restful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。 | OpenFeign是springcloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。 |
RestTemplate 和 OpenFeign 两者区别:
RestTemplate | OpenFeign |
---|---|
RestTemplate 采用了模版设计的设计模式,提供了多种便捷访问远程Http服务的方法,目前常用的3种都有支持:HttpClient、OkHttp、JDK原生的URLConnection(默认的)。但使用RestTemplate时,URL参数是以编程方式构造的,数据被发送到其他服务,依赖别的spring版块、参数传递不灵活。开发体验、可读性、可维护性一般,性能和灵活性极佳。 | Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。只需要像调用方法一样调用它就可以完成服务请求及相关处理。避免了调用目标服务时,需要不断的解析/封装json数据的繁琐。开发体验、可读性、可维护性极佳,性能中等(RestTemplate的50%左右),灵活性中等(内置功能可满足绝大多数需求)。 |
其实Feign的本质是对RestTemplate的包装,让服务远程调用变得更简单,使用起来像是本地调用普通方法。 | |
OpenFeign相比restTemplate代码里请求服务端的controller层,需要在客户端创建一个service层,并创建一个service接口(要用到@FeignClient注解),其方法和服务端的controller里的方法相对应,之后客户端直接调这个接口就行了。 | |
OpenFeign的引入直接砍掉了restTemplate,客户端controller在调用服务端时不需要再关注请求的方式、地址以及是forObject还是forEntity,完全面向接口调用,层次结构更加明了,而且OpenFeign自身集成Ribbon,所以默认开启轮询的负载均衡。而且还可以和hystrix相结合,写一个类实现service接口,其中实现的方法的方法体便是降级或熔断的fallback方法(需要在接口中指定该实现类)。这样结构更清晰,耦合也更低。 |
OpenFeign使用示例:
1.加入依赖,导包
<!-- Feign -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>10.10.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>10.10.1</version>
</dependency>
<!-- feign使用jackson数据编码 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>10.10.1</version>
</dependency>
2.在yml配置文件中配置相关信息
feign:
upgradeUrl: http://localhost:8888/v1
archivistUrl: http://localhost:8000/v1/
connectTimeout: 1000
readTimeout: 3500
followRedirects: true
token: eyJhbGciOiJIUzUxMiJ9.eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIs
3.在config包新建FeignProperties类获取yml配置
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author xiaotao
*/
@Data
@ConfigurationProperties(prefix = "feign")
public class FeignProperties {
private String upgradeUrl;
private String archivistUrl;
//连接超时
private long connectTimeout;
//请求接口超时
private long readTimeout;
private boolean followRedirects;
//请求令牌,根据调用服务是否使用
private String token;
}
4.在config包新建FeignConfiguration类配置Bean,给对应的Client配置目标地址。
options方法指定连接超时时长及响应超时时长,retryer方法指定重试策略,target方法绑定接口与服务端地址。返回类型为绑定的接口类型。
import cn.com.tiza.service.FileClient;
import cn.com.tiza.service.UpgradeTaskClient;
import feign.*;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
/**
* @author xiaotao
*/
@Configuration
@EnableConfigurationProperties(FeignProperties.class)
public class FeignConfiguration {
@Autowired
private FeignProperties properties;
/**
* 启用Fegin自定义注解 如@RequestLine @Param
*/
@Bean
public Contract feignContract() {
return new Contract.Default();
}
/**
* new Retryer.Default(5000, 5000, 3)
* period=5000发起当前请求的时间间隔,单位毫秒
* maxPeriod=5000发起当前请求的最大时间间隔,单位毫秒
* maxAttempts=3 最多请求次数,包括第一次
*/
//upgradeUrl
@Bean
public UpgradeTaskClient connect() {
return Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.options(this.options())
.retryer(new Retryer.Default(5000, 5000, 3))
.requestInterceptor(template -> template.header("Content-Type", "application/json"))
.target(UpgradeTaskClient.class, properties.getUpgradeUrl());
}
//archivistUrl
@Bean
public FileClient fileConnect() {
return Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.options(this.options())
.retryer(new Retryer.Default(5000, 5000, 3))
.requestInterceptor(template -> template.header("Api-Token", properties.getToken())
.header("Content-Type", "application/json"))
.target(FileClient.class, properties.getArchivistUrl());
}
/**
* connectTimeoutMillis=1000 链接超时时间
readTimeoutMillis=3500 响应超时时间,如果超过3.5秒没有接过发起下一次请求
*/
private Request.Options options() {
return new Request.Options(properties.getConnectTimeout(), TimeUnit.SECONDS, properties.getReadTimeout(),
TimeUnit.SECONDS, properties.isFollowRedirects());
}
}
5.在service包新建feign调用接口
/**
* @author xiaotao
*/
public interface UpgradeTaskClient {
/**
* @param taskId
* @param restData
* @return
*/
@RequestLine("POST /task/op/{taskId}")
Result op(@Param("taskId") String taskId, @RequestBody RestData<RestTaskData> restData);
/**
* @param taskId
* @param restData
* @return
*/
@RequestLine("POST /task/vehicle/{taskId}")
Result vehicle(@Param("taskId") String taskId, @RequestBody RestData<RestTaskData> restData);
}
public interface FileClient {
/**
* FTP上传
* @param dto
* @return
*/
@RequestLine("POST files")
Long getUpgradeFiles(@RequestBody FileRequest dto);
/**
* FTP下载
* @param id
* @return
*/
@RequestLine("GET files/{id}")
Map<String, String> downloadFile(@Param("id") Long id);
}
5.在方法中直接调用
@Autowired
private UpgradeTaskClient upgradeTaskClient;
@Autowired
private FileClient fileClient;
public String download(Long id, HttpServletRequest req, HttpServletResponse res) {
if (id != null) {
Map<String, String> map = fileClient.downloadFile(id);
return map.get("filePath");
}
return null;
}
/**
* 像调用方法一样使用即可
* Result result = upgradeTaskClient.vehicle(String.valueOf(id), restData);
*/
OpenFeign的GitHub源码地址: 👉点击这里👈.这个建议看看,可以了解它整合了什么功能,以及里面有很多简单的小例子。
Complete!!!