@FeignClient|@FeignClient 实现简便http请求封装方式

目录

  • @FeignClient实现http请求封装
    • 使用流程
  • 将http请求封装为FeignClient
    • 1.配置拦截器
    • 2.注入feignClientbean
    • 3.配置pom引用
    • 4.写feignClient
    • 5.写熔断器
【@FeignClient|@FeignClient 实现简便http请求封装方式】
@FeignClient实现http请求封装 我们一般在代码中调用http请求时,都是封装了http调用类,底层自己定义请求头,在写的时候,也是需要对返回的值进行json解析,很不方便。
  • name:name属性会作为微服务的名称,用于服务发现
  • url:host的意思,不用加http://前缀
  • decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException

使用流程
(1)创建接口类(FeignApi),来统一规范需要调用的第三方接口
@FeignClient(name = "aaa", url = "localhost:8080", decode404 = true)public interface FeignApi {/*** http请求*/@PostMapping("/api/xxxx/baiduaaa")ResponseResult getSomeMoneyForYourSelfAAA(@RequestBody AAAParam param); /*** 模仿上面写的Get方式请求*/@GetMapping("/api/xxxx/baidubbb")ResponseResult getSomeMoneyForYourSelfBBB(@RequestBody AAAParam param); }

(2)在启动类加上注解,会去扫包注册Bean
@EnableFeignClients(basePackages = {"com.aaa"})

(3)业务代码调用处:
ResponseResult response = pmsFeignApi.getSomeMoneyForYourSelfAAA(param);


将http请求封装为FeignClient
1.配置拦截器
import java.io.IOException; import java.io.InterruptedIOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; public class OkHttpRetryInterceptor implements Interceptor {undefinedprivate static final Logger LOGGER = LoggerFactory.getLogger(OkHttpRetryInterceptor.class); /*** 最大重试次数*/private intexecutionCount; /*** 重试的间隔*/private longretryInterval; OkHttpRetryInterceptor(Builder builder) {undefinedthis.executionCount = builder.executionCount; this.retryInterval = builder.retryInterval; }@Overridepublic Response intercept(Chain chain) throws IOException {undefinedRequest request = chain.request(); Response response = doRequest(chain, request); int retryNum = 0; while ((response == null || !response.isSuccessful()) && retryNum <= executionCount) {undefinedLOGGER.info("intercept Request is not successful - {}", retryNum); final long nextInterval = getRetryInterval(); try {undefinedLOGGER.info("Wait for {}", nextInterval); Thread.sleep(nextInterval); } catch (final InterruptedException e) {undefinedThread.currentThread().interrupt(); throw new InterruptedIOException(); }retryNum++; // retry the requestresponse = doRequest(chain, request); }return response; }private Response doRequest(Chain chain, Request request) {undefinedResponse response = null; try {undefinedresponse = chain.proceed(request); } catch (Exception e) {undefined}return response; }/*** retry间隔时间*/public long getRetryInterval() {undefinedreturn this.retryInterval; }public static final class Builder {undefinedprivate intexecutionCount; private long retryInterval; public Builder() {undefinedexecutionCount = 3; retryInterval = 1000; }public Builder executionCount(int executionCount) {undefinedthis.executionCount = executionCount; return this; }public Builder retryInterval(long retryInterval) {undefinedthis.retryInterval = retryInterval; return this; }public OkHttpRetryInterceptor build() {undefinedreturn new OkHttpRetryInterceptor(this); }}}


2.注入feignClient bean
import java.util.concurrent.TimeUnit; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.netflix.feign.FeignAutoConfiguration; import org.springframework.cloud.netflix.feign.ribbon.CachingSpringLoadBalancerFactory; import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import feign.Client; import feign.Feign; import feign.ribbon.RibbonClient; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; @Configuration@ConditionalOnMissingBean({ OkHttpClient.class, Client.class })@ConditionalOnClass(Feign.class)@AutoConfigureBefore(FeignAutoConfiguration.class)public class FeignClientConfig {undefined@Value("${feign.invoke.http.connectTimeoutMillis:3000}")private int connectTimeoutMillis; @Value("${feign.invoke.http.readTimeoutMillis:10000}")private int readTimeoutMillis; @Value("${feign.invoke.http.retryExecutionCount:3}")private int retryExecutionCount; @Value("${feign.invoke.http.retryInterval:1000}")private int retryInterval; public FeignClientConfig() {undefined}@Bean@ConditionalOnMissingBean({ OkHttpClient.class })public OkHttpClient okHttpClient() {undefinedOkHttpRetryInterceptor okHttpRetryInterceptor = new OkHttpRetryInterceptor.Builder().executionCount(retryExecutionCount).retryInterval(retryInterval).build(); return new OkHttpClient.Builder().retryOnConnectionFailure(true).addInterceptor(okHttpRetryInterceptor).connectionPool(new ConnectionPool()).connectTimeout(connectTimeoutMillis, TimeUnit.MILLISECONDS).readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS).build(); }@Bean@ConditionalOnMissingBean({ Client.class })public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory) {undefinedif (cachingFactory == null) {undefinedRibbonClient.Builder builder = RibbonClient.builder(); builder.delegate(new feign.okhttp.OkHttpClient(this.okHttpClient())); return builder.build(); } else {undefinedreturn new LoadBalancerFeignClient(new feign.okhttp.OkHttpClient(this.okHttpClient()), cachingFactory,clientFactory); }}}


3.配置pom引用
io.github.openfeign feign-ribbon 9.0.0


4.写feignClient
@FeignClient(name = "xxxApi", url = "${xxx.url}")public interface xxxClient {@RequestMapping(method = RequestMethod.POST)public String createLink(@RequestHeader(name = "accessKey", defaultValue = "https://www.it610.com/article/xx") String accessKey,@RequestHeader(name = "accessSecret") String accessSecret, @RequestBody String linkConfig); }


5.写熔断器
@Autowiredprivate xxxClient xxClient; @HystrixCommand(commandKey = "xxxLink", fallbackMethod = "xxxError", commandProperties = { @HystrixProperty(name = "requestCache.enabled", value = "https://www.it610.com/article/true"),@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "https://www.it610.com/article/5000") })public String xxLink(String accessKey, String accessSecret, String linkConfig) {LOG.info("[xxLink]LinkConfig is {}", linkConfig); String resp = xxxClient.createLink(accessKey, accessSecret, linkConfig); LOG.info("[xxxLink] response : {}", resp); return resp; }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读