优雅编码之——传统项目中,使用openfeign替换掉项目中的httpclient

使用spring cloud时,当遇到服务与其他服务调用的场景,一般都会使用spring cloud openfeign来进行调用。

通过@feign注解让代码变得非常简单而又优雅,即便是跨服务的调用,写起来就像项目内部自己调自己的方法一样,之顺畅~

但当项目是非spring cloud项目,在服务内调用外部的http服务时,可能首先想到的就是httpclient或okhttp

将httpclient封装为一个工具类,哪里用到就拿出来调哪里,将请求url和参数信息传入,再将请求和响应的前后加上编解码的处理代码

如果项目中又遇到需要调很多这样接口的地方,又遇到各种请求头的适配、请求method的适配、参数类型的适配……,工具类的代码可能都能封装出好几十个方法来,代码看起来可能还是会有点别扭丑丑的~

那么有没有什么更好的方法来解决上面这一问题呢,答案是必须有的:feign就是专门干这个事的

正如feign官网所说的那样:Feign makes writing java http clients easier

feign


为了能让大家在传统项目中一起了解下feign这个组件,这里特意将feign快速引入到传统项目中的流程进行简单记录一下。


引入方式

以maven项目为例,添加pom依赖

    <properties>
        <openfeign.version>11.9.1</openfeign.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
        </dependency>
        <!--序列化方式支持gson、Jackson、fastjson、Sax等-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-gson</artifactId>
        </dependency>
         <!--可选项,slf4j日志支持-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-slf4j</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-bom</artifactId>
                <version>${openfeign.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

其中openfeign.version为选择的feign版本,最新的版本可直接从feign的github中获取

请求配置

普通请求

import com.haiyang.javastu.springtransactionmanager.service.TestApiService;
import feign.Feign;
import feign.Logger;
import feign.Request;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.slf4j.Slf4jLogger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class ApiRegisterConf {
    private final String testApiUrl = "http://127.0.0.1:8080";

    @Bean
    public TestApiService testApiService() {
        return Feign.builder()
                .decoder(new GsonDecoder())
                .encoder(new GsonEncoder())
                .options(new Request.Options(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true))
                //需要引入feign-slf4j依赖
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                .target(TestApiService.class, testApiUrl);
    }
}

其中TestApiService为:

import com.haiyang.javastu.springtransactionmanager.model.JsonResult;
import com.haiyang.javastu.springtransactionmanager.model.TestInfo;
import com.haiyang.javastu.springtransactionmanager.model.TestInfoResult;
import feign.Headers;
import feign.Param;
import feign.RequestLine;


public interface TestApiService {

    @Headers("Content-Type: application/json")
    @RequestLine("POST /hello/test.do")
    JsonResult<TestInfoResult> helloTest(TestInfo testInfo);
}

写个controller测试一下:

import com.haiyang.javastu.springtransactionmanager.model.JsonResult;
import com.haiyang.javastu.springtransactionmanager.model.TestInfo;
import com.haiyang.javastu.springtransactionmanager.model.TestInfoResult;
import com.haiyang.javastu.springtransactionmanager.service.TestApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(value = "test/feign/")
@RestController
public class TestFeignController {
    private final TestApiService testApiService;

    @Autowired
    public TestFeignController(TestApiService testApiService) {
        this.testApiService = testApiService;
    }

    @PostMapping(value = "helloTest")
    public JsonResult<TestInfoResult> helloTest(@RequestBody TestInfo testInfo) {
        return testApiService.helloTest(testInfo);
    }

}

发个请求测试一下

POST http://localhost:8080/test/feign/helloTest
Content-Type: application/json

{"id": 123}

输出结果:

{
  "msg": null,
  "code": 200,
  "data": {
    "id": 123,
    "name": "zhangsan",
    "age": 27,
    "hobbys": [
      "programing",
      "reading"
    ]
  }
}

result

如果想要输出feign的请求详细日志,记得需要将feign包的日志级别设为DEBUG级别

logging:
  level:
      feign: debug

请求携带自定义header

feign提供了requestInterceptor来实现对请求的拦截处理,如果遇到需要进行token之类的header透传的场景,用它来实现就可以了。

示例:

import com.haiyang.javastu.springtransactionmanager.service.TestApiService;
import feign.*;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.slf4j.Slf4jLogger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Configuration
public class ApiRegisterConf {
    private final String testApiUrl = "http://127.0.0.1:8080";

    @Bean
    public TestApiService testApiService() {
        return Feign.builder()
                .decoder(new GsonDecoder())
                .encoder(new GsonEncoder())
                .options(new Request.Options(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true))
                .logger(new Slf4jLogger())
                .logLevel(Logger.Level.FULL)
                //自定义请求头
                .requestInterceptor(new MyHeaderRequestInterceptor())
                .target(TestApiService.class, testApiUrl);
    }

    public static class MyHeaderRequestInterceptor implements RequestInterceptor {

        @Override
        public void apply(RequestTemplate template) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            //添加token,设置进请求头
            template.header("token", request.getHeader("token"));
        }
    }
}

写个测试请求测试一下

result

动态请求host

有时需要根据业务需要,可能需要向不同host发起请求。在feign中也提供了动态切换host的方式。

Overriding the Request Line

If there is a need to target a request to a different host then the one supplied when the Feign client was created, or you want to supply a target host for each request, include a java.net.URI parameter and Feign will use that value as the request target.

@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(URI host, Issue issue, @Param("owner") String owner, >@Param("repo") String repo);

只需要在请求的参数中包含URI参数就可以进行host的自定义了。

如上面的示例中,改成这样:

public interface TestApiService {

    @Headers("Content-Type: application/json")
    @RequestLine("POST /hello/test.do")
    JsonResult<TestInfoResult> helloTest(URI hostUri, TestInfo testInfo);
}

调用的地方将uri传入即可:

    @PostMapping(value = "helloTest")
    public JsonResult<TestInfoResult> helloTest(@RequestBody TestInfo testInfo) {
        URI uri = URI.create("http://192.168.1.4:8080");
        return testApiService.helloTest(uri, testInfo);
    }

其调用效果也是一样的。
result

spring cloud openfeign、openfeign、feign的区别

spring cloud openfeign

This project provides OpenFeign integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.

OpenFeign

Ultimately, Netflix decided to stop using Feign internally and ceased its development. As a result of this decision, Netflix fully transferred Feign to the open-source community under a new project named OpenFeign.

feign

Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign’s first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness.

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水中加点糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值