<section data-tool="mdx editor" data-website="https://editor.runjs.cool/" class="default"><section data-tool="mdx 编辑器" data-website="https://editor.runjs.cool/" class="markdown-body" components="[object Object]" style="color: #595959; font-size: 15px; font-family: -apple-system, system-ui, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif; background-image: linear-gradient( 90deg, rgba(60, 10, 30, 0.04) 3%, rgba(0, 0, 0, 0) 3% ), linear-gradient(360deg, rgba(60, 10, 30, 0.04) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center; padding: 0 12px;"><blockquote data-line="1" style="background: #fff9f9; margin: 2em 0; padding: 2px 20px; border-left: 4px solid #b2aec5;"> <p data-line="1" style="font-size: 15px; font-weight: 400; color: #666; line-height: 2;">有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准<code data-line="1" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">https://blog.zysicyj.top</code></p> </blockquote> <blockquote data-line="3" style="background: #fff9f9; margin: 2em 0; padding: 2px 20px; border-left: 4px solid #b2aec5;"> <p data-line="3" style="font-size: 15px; font-weight: 400; color: #666; line-height: 2;">全网最细面试题手册,支持艾宾浩斯记忆法。这是一份最全面、最详细、最高质量的 java面试题,不建议你死记硬背,只要每天复习一遍,有个大概印象就行了。 <span class="link" style="color: #036aca; border-bottom: 1px solid rgba(3, 106, 202, 0.8); font-weight: 400; text-decoration: none;">https://store.amazingmemo.com/chapterDetail/1685324709017001`</span></p> </blockquote> <figure><img src="https://blog-1253652709.cos.ap-guangzhou.myqcloud.com//picgo/202312312342374.png" alt="image.png" data-line="7" style="max-width: 100%;"><figcaption style="text-align: center; font-size: 14px;">image.png</figcaption></figure> <p data-line="10" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">在OpenFeign中,数据压缩是通过配置Feign客户端来实现的。OpenFeign支持对传输的数据进行压缩,以减少网络传输的数据量,提高传输效率。下面将详细介绍在OpenFeign中如何配置和使用数据压缩。</p> <h3 data-line="12" style="padding: 30px 0; margin: 0; color: #135ce0; font-size: 16px;">1. 启用OpenFeign的数据压缩</h3> <p data-line="14" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">要在OpenFeign中启用数据压缩,需要进行以下配置:</p> <h4 data-line="16" style="padding: 30px 0; margin: 0; color: #135ce0;">1.1 添加依赖</h4> <p data-line="18" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">首先,需要在项目中添加相应的依赖,通常是在<code data-line="18" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">pom.xml</code>文件中添加以下依赖:</p> <pre data-line="31" class="language-xml" style="overflow-x: auto; -webkit-overflow-scrolling: touch; position: relative; color: #d6e7ff; background: #030314; text-shadow: none; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-size: 1em; line-height: 1.5; letter-spacing: .2px; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; text-align: left; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; border: 1px solid #2a4555; border-radius: 5px; margin: 1em 0; overflow: auto; padding: 0;"><section class="code__header" style="display: flex; justify-content: space-between; align-items: center; font-size: 12px; height: 30px; line-height: 30px;"><span class="code__tools" style="display: flex; align-items: center; padding: 10px 12px; width: 75px;"><span class="red code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #ff605c;"></span><span class="yellow code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #ffbd44;"></span><span class="green code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #00ca4e;"></span></span><span class="code__tools_right" style="width: 75px;"> </span></section><pre style="overflow-x: auto; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; overflow: auto; -webkit-overflow-scrolling: touch; position: relative; line-height: 1.75; margin: 0;" data-title="true"><code class="language-xml code-highlight" data-line="31" style="float: left; color: #d6e7ff; background: #030314; text-shadow: none; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; line-height: 1.5; letter-spacing: .2px; white-space: pre; word-spacing: normal; word-wrap: normal; text-align: left; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; min-width: 100%; font-size: 12px; word-break: normal; display: block; flex: none; padding: 12px; padding-top: 0; max-width: unset;"><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>dependency</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>groupId</span><span class="token punctuation" style="color: #d6b007;">></span></span>io.github.openfeign<span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>groupId</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>artifactId</span><span class="token punctuation" style="color: #d6b007;">></span></span>feign-core<span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>artifactId</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>version</span><span class="token punctuation" style="color: #d6b007;">></span></span>10.14<span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>version</span><span class="token punctuation" style="color: #d6b007;">></span></span> <span class="token comment" style="color: #446e69;"><!-- 版本号根据实际情况调整 --></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>dependency</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>dependency</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>groupId</span><span class="token punctuation" style="color: #d6b007;">></span></span>io.github.openfeign<span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>groupId</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>artifactId</span><span class="token punctuation" style="color: #d6b007;">></span></span>feign-gson<span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>artifactId</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"><</span>version</span><span class="token punctuation" style="color: #d6b007;">></span></span>10.14<span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>version</span><span class="token punctuation" style="color: #d6b007;">></span></span> <span class="token comment" style="color: #446e69;"><!-- 版本号根据实际情况调整 --></span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token tag" style="color: #d6e7ff;"><span class="token tag" style="color: #d6e7ff;"><span class="token punctuation" style="color: #d6b007;"></</span>dependency</span><span class="token punctuation" style="color: #d6b007;">></span></span> </span></code></pre></pre> <h4 data-line="33" style="padding: 30px 0; margin: 0; color: #135ce0;">1.2 配置Feign客户端</h4> <p data-line="35" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">在Feign客户端的配置类中,需要添加对数据压缩的配置。可以通过<code data-line="35" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">Feign.builder()</code>方法来配置Feign客户端,启用数据压缩功能。</p> <pre data-line="70" class="language-java" style="overflow-x: auto; -webkit-overflow-scrolling: touch; position: relative; color: #d6e7ff; background: #030314; text-shadow: none; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-size: 1em; line-height: 1.5; letter-spacing: .2px; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; text-align: left; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; border: 1px solid #2a4555; border-radius: 5px; margin: 1em 0; overflow: auto; padding: 0;"><section class="code__header" style="display: flex; justify-content: space-between; align-items: center; font-size: 12px; height: 30px; line-height: 30px;"><span class="code__tools" style="display: flex; align-items: center; padding: 10px 12px; width: 75px;"><span class="red code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #ff605c;"></span><span class="yellow code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #ffbd44;"></span><span class="green code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #00ca4e;"></span></span><span class="code__tools_right" style="width: 75px;"> </span></section><pre style="overflow-x: auto; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; overflow: auto; -webkit-overflow-scrolling: touch; position: relative; line-height: 1.75; margin: 0;" data-title="true"><code class="language-java code-highlight" data-line="70" style="float: left; color: #d6e7ff; background: #030314; text-shadow: none; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; line-height: 1.5; letter-spacing: .2px; white-space: pre; word-spacing: normal; word-wrap: normal; text-align: left; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; min-width: 100%; font-size: 12px; word-break: normal; display: block; flex: none; padding: 12px; padding-top: 0; max-width: unset;"><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">Feign</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span>codec<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">Decoder</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span>codec<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">Encoder</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span>gson<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">GsonDecoder</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span>gson<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">GsonEncoder</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span>okhttp<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">OkHttpClient</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span>okhttp<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">OkHttpClient</span><span class="token punctuation" style="color: #d6b007;">.</span><span class="token class-name" style="color: #78f3e9;">Builder</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">okhttp3<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">Interceptor</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">okhttp3<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">Response</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">okhttp3<span class="token punctuation" style="color: #d6b007;">.</span>logging<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">HttpLoggingInterceptor</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation" style="color: #d6b007;">.</span>io<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">IOException</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation" style="color: #d6b007;">.</span>util<span class="token punctuation" style="color: #d6b007;">.</span>zip<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">GZIPInputStream</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">public</span> <span class="token keyword" style="color: #0fe468;">class</span> <span class="token class-name" style="color: #78f3e9;">FeignClientConfiguration</span> <span class="token punctuation" style="color: #d6b007;">{</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token keyword" style="color: #0fe468;">public</span> <span class="token class-name" style="color: #78f3e9;">MyFeignClient</span> <span class="token function" style="color: #78f3e9;">myFeignClient</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span> <span class="token punctuation" style="color: #d6b007;">{</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token keyword" style="color: #0fe468;">return</span> <span class="token class-name" style="color: #78f3e9;">Feign</span><span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">builder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">client</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token keyword" style="color: #0fe468;">new</span> <span class="token class-name" style="color: #78f3e9;">OkHttpClient</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token keyword" style="color: #0fe468;">new</span> <span class="token class-name" style="color: #78f3e9;">Builder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">addInterceptor</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token keyword" style="color: #0fe468;">new</span> <span class="token class-name" style="color: #78f3e9;">GzipRequestInterceptor</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">build</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">encoder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token keyword" style="color: #0fe468;">new</span> <span class="token class-name" style="color: #78f3e9;">GsonEncoder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">decoder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token keyword" style="color: #0fe468;">new</span> <span class="token class-name" style="color: #78f3e9;">GsonDecoder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">target</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token class-name" style="color: #78f3e9;">MyFeignClient</span><span class="token punctuation" style="color: #d6b007;">.</span><span class="token keyword" style="color: #0fe468;">class</span><span class="token punctuation" style="color: #d6b007;">,</span> <span class="token string" style="color: #49c6ec;">"http://example.com"</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">}</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token keyword" style="color: #0fe468;">static</span> <span class="token keyword" style="color: #0fe468;">class</span> <span class="token class-name" style="color: #78f3e9;">GzipRequestInterceptor</span> <span class="token keyword" style="color: #0fe468;">implements</span> <span class="token class-name" style="color: #78f3e9;">Interceptor</span> <span class="token punctuation" style="color: #d6b007;">{</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token annotation punctuation" style="color: #d6b007;">@Override</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token keyword" style="color: #0fe468;">public</span> <span class="token class-name" style="color: #78f3e9;">Response</span> <span class="token function" style="color: #78f3e9;">intercept</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token class-name" style="color: #78f3e9;">Chain</span> chain<span class="token punctuation" style="color: #d6b007;">)</span> <span class="token keyword" style="color: #0fe468;">throws</span> <span class="token class-name" style="color: #78f3e9;">IOException</span> <span class="token punctuation" style="color: #d6b007;">{</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token class-name" style="color: #78f3e9;"><span class="token namespace">okhttp3<span class="token punctuation" style="color: #d6b007;">.</span></span>Request</span> request <span class="token operator" style="color: #ec8e01; background: transparent;">=</span> chain<span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">request</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">newBuilder</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">addHeader</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token string" style="color: #49c6ec;">"Accept-Encoding"</span><span class="token punctuation" style="color: #d6b007;">,</span> <span class="token string" style="color: #49c6ec;">"gzip"</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">build</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token keyword" style="color: #0fe468;">return</span> chain<span class="token punctuation" style="color: #d6b007;">.</span><span class="token function" style="color: #78f3e9;">proceed</span><span class="token punctuation" style="color: #d6b007;">(</span>request<span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">}</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token punctuation" style="color: #d6b007;">}</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token punctuation" style="color: #d6b007;">}</span> </span></code></pre></pre> <p data-line="72" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">在上述代码中,我们通过<code data-line="72" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">Feign.builder()</code>方法配置了Feign客户端,并使用<code data-line="72" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">OkHttpClient</code>作为HTTP客户端。在<code data-line="72" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">GzipRequestInterceptor</code>中,我们添加了对GZIP压缩的支持,通过添加<code data-line="72" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">Accept-Encoding: gzip</code>的请求头来告知服务端可以接受GZIP压缩的响应。</p> <h3 data-line="74" style="padding: 30px 0; margin: 0; color: #135ce0; font-size: 16px;">2. 数据压缩的使用示例</h3> <p data-line="76" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">一旦配置了Feign客户端以启用数据压缩,接下来就可以像平常一样使用Feign来进行服务调用。OpenFeign会在请求中添加<code data-line="76" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">Accept-Encoding: gzip</code>的请求头,服务端在响应时会根据这个请求头来判断是否需要对响应数据进行GZIP压缩。</p> <pre data-line="85" class="language-java" style="overflow-x: auto; -webkit-overflow-scrolling: touch; position: relative; color: #d6e7ff; background: #030314; text-shadow: none; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-size: 1em; line-height: 1.5; letter-spacing: .2px; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; text-align: left; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; border: 1px solid #2a4555; border-radius: 5px; margin: 1em 0; overflow: auto; padding: 0;"><section class="code__header" style="display: flex; justify-content: space-between; align-items: center; font-size: 12px; height: 30px; line-height: 30px;"><span class="code__tools" style="display: flex; align-items: center; padding: 10px 12px; width: 75px;"><span class="red code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #ff605c;"></span><span class="yellow code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #ffbd44;"></span><span class="green code__circle" style="display: inline-block; align-items: center; width: 9px; height: 9px; margin-right: 8px; padding: 1px; border-radius: 50%; background-color: #00ca4e;"></span></span><span class="code__tools_right" style="width: 75px;"> </span></section><pre style="overflow-x: auto; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; overflow: auto; -webkit-overflow-scrolling: touch; position: relative; line-height: 1.75; margin: 0;" data-title="true"><code class="language-java code-highlight" data-line="85" style="float: left; color: #d6e7ff; background: #030314; text-shadow: none; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; line-height: 1.5; letter-spacing: .2px; white-space: pre; word-spacing: normal; word-wrap: normal; text-align: left; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; min-width: 100%; font-size: 12px; word-break: normal; display: block; flex: none; padding: 12px; padding-top: 0; max-width: unset;"><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation" style="color: #d6b007;">.</span></span><span class="token class-name" style="color: #78f3e9;">RequestLine</span></span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token keyword" style="color: #0fe468;">public</span> <span class="token keyword" style="color: #0fe468;">interface</span> <span class="token class-name" style="color: #78f3e9;">MyFeignClient</span> <span class="token punctuation" style="color: #d6b007;">{</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token annotation punctuation" style="color: #d6b007;">@RequestLine</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token string" style="color: #49c6ec;">"GET /users/{id}"</span><span class="token punctuation" style="color: #d6b007;">)</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"> <span class="token class-name" style="color: #78f3e9;">User</span> <span class="token function" style="color: #78f3e9;">getUserById</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token annotation punctuation" style="color: #d6b007;">@Param</span><span class="token punctuation" style="color: #d6b007;">(</span><span class="token string" style="color: #49c6ec;">"id"</span><span class="token punctuation" style="color: #d6b007;">)</span> <span class="token class-name" style="color: #78f3e9;">Long</span> id<span class="token punctuation" style="color: #d6b007;">)</span><span class="token punctuation" style="color: #d6b007;">;</span> </span><span class="code-line" style="display: block; padding-left: 16px; padding-right: 16px; margin-left: -16px; margin-right: -16px; border-left: 4px solid rgba(0, 0, 0, 0);"><span class="token punctuation" style="color: #d6b007;">}</span> </span></code></pre></pre> <p data-line="87" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">在上述代码中,我们定义了一个Feign客户端接口<code data-line="87" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">MyFeignClient</code>,并在接口的方法上使用了<code data-line="87" style="word-break: break-word; border-radius: 2px; overflow-x: auto; background-color: #fff5f5; color: #ff502c; font-size: 0.87em; padding: 0.065em 0.4em; max-width: unset;">@RequestLine</code>注解来描述服务调用的细节。</p> <h3 data-line="89" style="padding: 30px 0; margin: 0; color: #135ce0; font-size: 16px;">3. 数据压缩的注意点</h3> <ul data-line="92" style="padding-left: 28px; list-style: disc outside;"> <li data-line="91" style="line-height: 2; color: #595959; margin-bottom: 0; list-style: inherit;"><strong data-line="91" style="color: #036aca;">服务端支持</strong>:在使用OpenFeign的数据压缩功能时,需要确保服务端支持对数据的压缩和解压缩,以确保数据的正确传输和解析。</li> <li data-line="92" style="line-height: 2; color: #595959; margin-bottom: 0; list-style: inherit;"><strong data-line="92" style="color: #036aca;">性能影响</strong>:数据压缩会增加服务端和客户端的计算负担,需要评估压缩和解压缩对系统性能的影响,以及是否值得在性能和带宽之间进行权衡。</li> </ul> <p data-line="94" style="color: #595959; font-size: 15px; line-height: 2; font-weight: 400;">通过以上配置和示例,我们可以在OpenFeign中启用数据压缩,并对传输的数据进行压缩,以提高网络传输效率。</p></section></section>
本文由博客一文多发平台 [OpenWrite] 发布!