feign声明式客户端

参考地址: https://blog.csdn.net/qq_30643885/article/details/85341275

Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,只需要创建一个接口,然后在上面添加注释即可。Feign 默认策略为轮询。

1、SpringCloud 支持两种客户端调用工具

(1)Rest RestTemplate 基本上不用;

(2)Feign 客户端,在实际开发中用的最多,Feign是一个声明式Http客户端调用工具,采用接口+注解的方式实现,易读性较强。

2、步骤

(1)引入依赖

<!-- SpringBoot整合fegnin客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

(2)定义接口,加注解@FeignClient(name="servername"),

  方法名上加@requestMapping("/method_name")

import java.util.List;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.chen.springcloud.entity.Dept;
 
@FeignClient(value="MICROSERVICECLOUD-DEPT")
public interface DeptClientService {
 
    @RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET)
    public Dept get(@PathVariable("id") long id);
    
    @RequestMapping(value="/dept/list",method=RequestMethod.GET)
    public List<Dept> list();
    
    @RequestMapping(value="/dept/add",method=RequestMethod.POST)
    public boolean add(Dept dept);
    
}

(3)在Application中开启Feign权限

原文地址:https://www.cnblogs.com/zhaoyanhaoBlog/p/11949693.html