lilele200706

 

Feign技术

 

Feign技术

它是什么

Feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。

Feign主要是社区,大家习惯面对接口编程,这个是很多开发人员的规范。只需要创建一个接口,添加注解即可!调用微服务访问的两种方法:

  1. 微服务名字(Ribbon)

  2. 接口和注解(Feign)

     

SpringCloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端。

Feign代替了RestTemplate,通过接口调用服务,使代码的可读性变高,但性能降低了,加了一层。Feign本质还是找Eureka发现这些服务,Feign只是相当于在服务调用时融合了Ribbon,支持负载均衡。

它能干什么

Feign旨在使java Http客户端变的更容易。

使用Ribbon+RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法。

使用开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。Feign在这个基础上做了进一步封装,由他来帮组我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解来配置它(如dao接口上Mapper注解,在微服务接口上标Feign注解)即可完成服务提供方的接口绑定,简化了使用SpringCloud Ribbon时,自动封装服务调用客户端的开发量。

怎么使用

1.导包(在API模块和消费者模块)

<!--    Feign-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-feign</artifactId>
   <version>1.4.6.RELEASE</version>
</dependency>

2.在API模块写一个接口

@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT-8001")
public interface DeptClientService {
   @PostMapping("/dept/add")
   public boolean addDept(Dept dept);

   @GetMapping("/dept/get/{id}")
   public Dept queryById(@PathVariable("id") Long id);

   @GetMapping("/dept/list")
   public List<Dept> queryAll();
}

3.重构消费者模块

原来:

@RestController
public class DeptConsumerController {
  @Autowired
  private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法,简单的Restful服务模板

  //private static final String REST_URL_PREFIX = "http://localhost:8001";
  //Ribbon,访问服务这里的地址应该是一个变量,通过服务名来访问。
  private static final String REST_URL_PREFIX = "SPRINGCLOUD-PROVIDER-DEPT-8001";
  @RequestMapping("/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id){
      return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
  }
  @RequestMapping("/consumer/dept/add")
  public boolean add(Dept dept){
      return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
  }
  @RequestMapping("/consumer/dept/list")
  public List<Dept> list(){
      return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
  }
}

重构后:

@RestController
public class DeptConsumerController {
  @Autowired
  private DeptClientService service = null;//没有写实现类,自动配置的

  @RequestMapping("/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id){
      return this.service.queryById(id);
  }
  @RequestMapping("/consumer/dept/add")
  public boolean add(Dept dept){
      return this.service.addDept(dept);
  }
  @RequestMapping("/consumer/dept/list")
  public List<Dept> list(){
      return this.service.queryAll();
  }
}

4.添加注解

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"org.lele.springcloud"})//Feign
@ComponentScan("org.lele.springcloud")//Feign
public class FeignDeptConsumer_80 {
  public static void main(String[] args){
      SpringApplication.run(FeignDeptConsumer_80.class,args);
  }
}

5.启动测试

先启动Eureka注册中心服务,在启动服务提供者,最后启动Feign服务消费者

 

posted on 2021-12-05 22:56  lilele200706  阅读(71)  评论(0)    收藏  举报

导航