http请求工具:Feign在springboot、spring下使用

9 篇文章 0 订阅
8 篇文章 0 订阅

Feign

使用feign,实现请求第三方接口,作为一个 http请求工具.

常在springboot项目中使用feign,作为HTTP请求工具,类似Rest Template,也可以单独作为工具类使用

  1. 在springboot项目中使用feign,作为HTTP请求工具,类似 Rest Template
  2. 在spring、springMVC环境下使用,也可以单独使用,类似 http client
一、在spring boot环境体系下使用

第一步,在maven 引入依赖,pom.xml

<!-- 用于springboot项目环境下,或者springcloud项目环境下 集成 feign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

第二步,编写feign客户端,相当于实际请求接口

只需要定义方法请求,类似服务端controller的请求,url配置可以使用springcloud体系里面微服务配置

@FeignClient(name = "studentFeignClient", url = "${student.feign.url}")
public interface StudentFeignClient {

    @GetMapping("/list")
    public ResultUtis getList() ;
}

第三步,在启动类上注册扫描

// 开启 feign
@EnableFeignClients
@SpringBootApplication
public class FeignLearnApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignLearnApplication.class, args);
    }
}

第四步,客户端实际方法中调用服务端接口

@RestController
@RequestMapping("/student")
public class StudentController {
    
    // 实际调用,自动注入
    @Autowired
    private StudentFeignClient studentFeignClient ;

    @GetMapping("/list")
    public ResultUtis getList() {
        return studentFeignClient.getList();
    }
}

以往使用Rest Template的做法:

@RestController
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	private RestTemplate restTemplate ;
	
    // 学生服务端接口URL
	@Value("${student.feign.url}")
	private String studentServerUrl ;
	
	@GetMapping("/list")
	public ResultUtis getList() {
		// 类似
		return restTemplate.getForObject(studentServerUrl + "/list", 
                                         ResultUtis.class);
	}
	
}

相比较下,feign请求访问服务接口更加简洁,更加方便

二、不在springboot体系下

feign可以在spring、springMVC环境下使用,也可以单独使用,类似 http client

第一步,配置maven,添加JAR包,pom.xml

<!-- 以下几个用于 spring环境、springmvc环境下的,或者 不是 spring容器内的项目 集成 feign -->
<!-- feign-core 是核心,其他JAR用不到,可以不用引入 -->
 <dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-core</artifactId>
    <version>${feign.version}</version>
</dependency>

 <dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-gson</artifactId>
    <version>${feign.version}</version>
</dependency>

 <dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-jackson</artifactId>
    <version>${feign.version}</version>
</dependency>



 <dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-httpclient</artifactId>
    <version>${feign.version}</version>
</dependency>


 <dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-okhttp</artifactId>
    <version>${feign.version}</version>
</dependency>

 <dependency>
     <groupId>io.github.openfeign</groupId>
     <artifactId>feign-ribbon</artifactId>
    <version>${feign.version}</version>
</dependency>

第二步,编写feign客户端,实际访问请求接口

/**
 * @description
 *	不在spring环境下的  feign 接口定义
 * @author TianwYam
 * @date 2020年12月25日下午6:25:42
 */
public interface StudentFeignClientNoSpring {
    

    // 不使用spring环境下的,只能使用 feign自带的 RequestLine 来声明 方法 请求 类型
    @RequestLine("GET /list")
    public ResultUtis getList() ;

}

第三步,调用方式

/**
 * @description
 *	在不是 spring环境下 、 springboot环境下的 使用feign
 * @author TianwYam
 * @date 2020年12月25日下午6:17:56
 */
public class FeignNoSpringMain {

    public static void main(String[] args) {
        
        
        StudentFeignClientNoSpring studentFeignClient = Feign.builder()
            .decoder(new GsonDecoder())
            .target(StudentFeignClientNoSpring.class, "http://localhost:8081/api/v1/student");
        
        ResultUtis list = studentFeignClient.getList();
        
        System.out.println(JSON.toJSONString(list, SerializerFeature.PrettyFormat));
    }
    
}

此处写成main方式,实际项目中,可以写成一个工具类

/**
 * @description
 *	Feign工具类
 * @author TianwYam
 * @date 2021年1月30日上午11:24:21
 */
public class FeignClientUtils {

    /**
     * @description
     *	获取 feign客户端
     * @author TianwYam
     * @date 2021年1月30日上午11:23:34
     * @param <T> 客户端类
     * @param url 调用客户端请求的URL
     * @param clazz 客户端 class
     * @return
     */
    public static <T> T getClient(String url, Class<T> clazz) {

        return Feign.builder()
                .decoder(new GsonDecoder())
                .encoder(new GsonEncoder())
                .target(clazz, url);
    }
}

相对于httpclient下方便许多,不需要复杂的去设置 请求方式get\post等,也不需要对返回值做解析

feign是直接跟服务端的controller保持一致,定义请求方法,相对来说更加简单,更加容易

刚开始写微信公众号,请多多关注,欢迎,多谢!
微信公众号:《Java学习积累》
请点一下订阅,多谢!!!
微信公众号:Java学习积累

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天涯共明月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值