微服务-OpenFeign基本使用

一、前言

二、OpenFeign基本使用

1、OpenFeign简介

OpenFeign是一种声明式、模板化的HTTP客户端,它使得调用RESTful网络服务变得简单。在Spring Cloud中使用OpenFeign,可以做到像调用本地方法一样使用HTTP请求访问远程服务,开发者无需关注远程HTTP请求的细节。

OpenFeign的Spring应用架构一般分为三部分,分别是注册中心、服务提供者和服务消费者。服务提供者向服务注册中心注册自己,然后服务消费者通过OpenFeign发送请求时,OpenFeign会向服务注册中心获取关于服务提供者的信息,然后再向服务提供者发送网络请求。

使用OpenFeign,只需要创建一个接口并添加注解,就可以实现远程服务调用。它简化了RestTemplate的代码,实现了Ribbon的负载均衡,使代码变得更加简洁。OpenFeign支持Spring MVC的注解,如@RequestMapping、@PathVariable等,这使得它在与Spring Cloud集成时更加方便。

2、Feign和OpenFeign

Feign和OpenFeign的区别在于,Feign是Spring Cloud的一部分,而OpenFeign是对Feign的封装,提供了更多的功能和更好的集成。OpenFeign不仅支持Spring MVC的注解,还支持JAX-RS注解和JPA注解等。此外,OpenFeign还支持自定义注解和编码器,使得它能够更好地满足不同的需求。

OpenFeign 组件的前身是 Netflix Feign 项目,它最早是作为 Netflix OSS 项目的一部分,由 Netflix 公司开发。后来 Feign 项目被贡献给了开源组织,于是才有了我们今天使用的 Spring Cloud OpenFeign 组件。

Feign 和 OpenFeign 有很多大同小异之处,不同的是 OpenFeign 支持 MVC 注解。

可以认为 OpenFeign 为 Feign 的增强版。

简单总结下 OpenFeign 能用来做什么:

OpenFeign 是声明式的 HTTP 客户端,让远程调用更简单。
提供了HTTP请求的模板,编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息
整合了Ribbon(负载均衡组件)和 Hystix(服务熔断组件),不需要显示使用这两个组件
Spring Cloud Feign 在 Netflix Feign的基础上扩展了对SpringMVC注解的支持

3、OpenFegin通讯优化

3.1、GZIP简介

(1)gzip介绍:gzip是一种数据格式,采用用deflate算法压缩数据;gzip是一种流行的数据压缩算法,应用十分广泛,尤其是在Linux平台。

(2)gzip能力:当Gzip压缩到一个纯文本数据时,效果是非常明显的,大约可以减少70%以上的数据大小。

(3)gzip作用:网络数据经过压缩后实际上降低了网络传输的字节数,最明显的好处就是可以加快网页加载的速度。网页加载速度加快的好处不言而喻,除了节省流量,改善用户的浏览体验外,另一个潜在的好处是Gzip与搜索引擎的抓取工具有着更好的关系。例如 Google就可以通过直接读取gzip文件来比普通手工抓取更快地检索网页。

3.2、HTTP协议中关于压缩传输的规定(原理)

在这里插入图片描述
第一:客户端向服务器请求头中带有:Accept-Encoding:gzip, deflate 字段,向服务器表示,客户端支持的压缩格式(gzip或者deflate),如果不发送该消息头,服务器是不会压缩的。

第二:服务端在收到请求之后,如果发现请求头中含有Accept-Encoding字段,并且支持该类型的压缩,就对响应报文压缩之后返回给客户端,并且携带Content-Encoding:gzip消息头,表示响应报文是根据该格式压缩过的。

第三:客户端接收到响应之后,先判断是否有Content-Encoding消息头,如果有,按该格式解压报文。否则按正常报文处理。

3.3、在OpenFeign技术中应用GZIP压缩

在Spring Cloud微服务体系中,一次请求的完整流程如下:
在这里插入图片描述
在整体流程中,如果使用GZIP压缩来传输数据,涉及到两次请求-应答。而这两次请求-应答的连接点是Application Client,那么我们需要在Application Client中配置开启GZIP压缩,来实现压缩数据传输。

3.4、只配置OpenFeign请求-应答的GZIP压缩

在交互数据量级不够的时候,看不到压缩内容。

这里只开启Feign请求-应答过程中的GZIP,也就是浏览器-Application Client之间的请求应答不开启GZIP压缩。

在全局配置文件中,使用下述配置来实现OpenFeign请求-应答的GZIP压缩
在这里插入图片描述

4、OpenFegin示例

模块说明:
user模块调用product模块的接口

4.1、依赖准备

<!--nacos-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<!--openfegin-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

4.2、启动类添加EnableFeginClients

启动类上添加@EnableFeignClients注解,才能把使用@FeginClient标记的类识别的fegin接口。

@EnableSwagger2
@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
@EnableFeignClients
public class UserApplication {
    
    
    public static void main(String[] args) throws UnknownHostException {
    
    
        ConfigurableApplicationContext application = SpringApplication.run(UserApplication.class, args);

        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        // 未配置默认空白
        if (path == null) {
    
    
            path = "";
        }
        // 未配置默认空白
        if (port == null) {
    
    
            port = "8080";
        }
        log.info("\n----------------------------------------------------------\n\t" +
                "项目启动成功,访问路径如下:\n\t" +
                "本地路径: http://localhost:" + port + path + "/\n\t" +
                "网络地址: http://" + ip + ":" + port + path + "/\n\t" +
                "接口地址: http://" + ip + ":" + port + path + "/swagger-ui.html\n\t"  +
                "API文档: http://" + ip + ":" + port + path + "/doc.html\n" +
                "----------------------------------------------------------");
    }
}

4.3、编写fegin接口

注意:@FeignClient中的value传入的是product模块在nacos上注册的服务名称。
在这里插入图片描述

@FeignClient(value = "product-demo",path = "")
@Service
public interface ProductService {
    
    
    @RequestMapping(value = "/test/hello",method = RequestMethod.GET)
    String hello();
}

4.4、调用fegin接口

@RestController
@RequestMapping("/user")
public class UserController {
    
    
    @Autowired
    private ProductService productService;

    @GetMapping("/test")
    public String test(){
    
    
        return productService.hello();
    }
}

在这里插入图片描述

三、总结

使用OpenFeign可以让你更轻松地编写HTTP客户端代码,并且无需手动处理底层的HTTP请求和响应。

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。

猜你喜欢

转载自blog.csdn.net/wmj20001225/article/details/132670611