Java中项目如何调用接口?
HttpClient
Apache Jakarta Common下的子项目
Okhttp
一个处理网路请求的开源项目,是安卓端最火的轻量级框架,由Square公司贡献
HttpURLConnection
Java的标准,继承自URLConnection
RestTemplate
Spring提供的用于访问Rest服务的客户端
1、什么是Feign
Feign是Netflix开发的声明式、模板化的HTTP客户端,其灵感来自Retrofit、JAXRS-2.0以及WebSocket。
Feign支持多种注解,Spring Cloud openfeign对Feign进行了增强,使其支持Spring MVC注解,另外还整合了Ribbon和Eurek,从而使用Feign的使用更加方便。
1.1优势
Feign可以做到使用HTTP请求远程服务时就像调用本地方法一样的体验,开发时完全感知不到这是远程方法,更感知不到这是个HTTP请求。就像Dubbo一样,consumer直接调用接口方法调用provider。
1.2Feign的设计架构
1.3Ribbon&Feign对比
Ribbon+RestTemplate进行微服务调用
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
# 调用方法内部
String url = "http://service-consumer/consume/hello";
return restTemplate.getForObject(url,String.class);
Feign进行微服务调用
2、Spring Cloud Alibaba快速整合Feign
1)引入依赖
<!--openfeign 远程调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2)编写调用接口+@FeignClient注解
@FeignClient(value = "service-provider",path = "/provide")
public interface HelloService {
@RequestMapping("/hello")
String provideHello();
}
3)调用端在启动类上添加注解@EnableFeignClients
@EnableFeignClients
@SpringBootApplication
public class CloudAlibabaDemoConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudAlibabaDemoConsumerApplication.class, args);
}
}
4)发起调用,就和调用本地方法一样,调用远程服务
@Autowired
private HelloService helloService;
@GetMapping("provideFeign")
public String provideFeign(){
return helloService.provideHello();
}
利用Feign的继承特性可以让服务的接口单独抽出来,作为公共依赖,以方便使用
3、Spring Cloud Feign自定义配置及使用
Feign提供了很多的扩展机制,让用户可以更加灵活的使用
3.1日志配置
1)定义配置类,指定日志级别
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
四种日志级别:
NONE:不记录任何日志(默认),性能最佳,适用于生产
BASIC:仅记录请求方法、URL、响应状态代码及执行时间,适用生产环境追踪问题
HEADERS:在BASIC级别上,额外记录请求和响应的header
FULL:记录请求和响应的header、body和元数据,适用于开发及测试环境定位问题
2)局部配置,让调用的微服务生效
@FeignClient(value = "service-provider",path = "/provide",configuration = FeignConfig.class)
public interface HelloService {
@RequestMapping("/hello")
String provideHello();
}
3)yml文件配置Client的日志级别,logging.level.feign接口包路径=debug
logging:
level:
com.dys.cloud_alibaba_demo.consumer.feign: debug
结果:
3.2契约配置
Spring Cloud在Feign的基础上做了扩展,可以让Feign支持Spring MVC的注解来调用。原生的Feign是不支持Spring MVC注解的,如果想在Spring Cloud中使用原生注解,可通过配置契约来改变这个配置,Spring Cloud中默认的是SpringMvcContract
1)注解修改契约配置
@Bean
public Contract feignContract(){
return new Contract.Default();
}
2)配置文件修改契约配置
feign.client.config.contract=feign.Contract.Default
使用原生契约
@FeignClient(value = "service-provider",path = "/provide")
public interface HelloService {
@RequestLine("GET /hello")
String provideHello();
}
3.3通过拦截器实现认证
使用场景:统一添加header信息,对body中信息做修改或替换
public class FeignAuthRequestInterceptor implements RequestInterceptor{
@Override
public void apply(RequestTemplate template){
//业务逻辑
String access_token = UUID.randomUUID().toString();
template.header("Authorization".acess_token);
}
}
@Configuration
public class FeignConfig{
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
/**
*自定义拦截器
*/
@Bean
public FeignAuthRequestInterceptor feignAuthRequestInterceptor(){
return new FeignAuthRequestInterceptor();
}
}
3.4超时时间配置
通过Options配置连接超时时间和读取超时时间,第一个参数连接超时时间(ms),默认2s,第二个参数请求处理的超时时间(ms),默认5s。
@Configuration
public class FeignConfig{
@Bean
public Request.Options options(){
return new Request.Options(5000,10000);
}
}
3.5客户端组件配置
3.6GZIP压缩配置(开启压缩可以有效节省网络资源,提升接口性能)
3.7编码器解码器配置