App下載

SpringCloud微服務(wù)架構(gòu)中的Feign使用詳解

猿友 2021-08-04 14:47:31 瀏覽數(shù) (2274)
反饋

簡介

feign是聲明式的web service客戶端,它讓微服務(wù)之間的調(diào)用變得更簡單了,類似controller調(diào)用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign時提供負載均衡的http客戶端。

在springcloud中不僅可以使用Ribbo進行負載均衡,也可以使用Feign。Feign是在Ribbon的基礎(chǔ)上進行了一次改進,采用接口的方式實現(xiàn)負載均衡。

使用

  • 導(dǎo)入依賴
<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  • 編寫對應(yīng)的接口

@FeignClient(value = "PROVIDER-NAME")聲明這是一個FeignClient,value指明需要的服務(wù)id

@FeignClient(value = "PROVIDER-NAME")
public interface DeptClientService {
// 請求的路徑需與服務(wù)提供者的路徑一致
    @RequestMapping(value = "/dev/add")
    boolean add(Dept dept);

    @RequestMapping(value = "/dev/{id}")
    Dept queryByID(@PathVariable("id") Long id );

    @PostMapping(value = "/dev/list")
    List<Dept> queryAll();
}

  • 修改Controller

我們不再使用RestTemplate來獲取所需的對象,而是通過之前定義的接口來獲取

@RestController
public class ConsumerController {
    @Autowired
    private DeptClientService service;
    @RequestMapping("/consumer/get/{id}")
    public Dept getByID(@PathVariable("id") Long id){
        return this.service.queryByID(id);
    }
    @RequestMapping("/consumer/add")
    public boolean add(String dname){
        Dept dept = new Dept();
        dept.setDname(dname);
        return this.service.add(dept);
    }
    @RequestMapping("/consumer/list")
    public List<Dept> list(){
        return this.service.queryAll();
    }

}
  • 修改啟動類

@EnableFeignClients 開啟Feign負載均衡

@SpringBootApplication(scanBasePackages = "com")
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.service")
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class,args);
    }
}

  • 對比:

Ribbon

public class ConsumerController {
    @Autowired
    private RestTemplate template;
    private static final String url="http://PROVIDER-NAME";
    @RequestMapping("/consumer/get/{id}")
    public Dept getByID(@PathVariable long id){
                                                //請求的路徑,返回的對象
        Dept getEntity = template.getForObject(url + "/dev/" + id, Dept.class);
        return getEntity;
    }
    @RequestMapping("/consumer/add")
    public boolean add(String dname){
        Dept dept = new Dept();
        dept.setDname(dname);
        System.out.println(dept);
        //請求的路徑,傳遞的參數(shù),返回的對象
        return template.postForObject(url+ "/dev/add",dept,Boolean.class);
    }
    @RequestMapping("/consumer/list")
    public List<Dept> list(){
        //請求的路徑,返回的對象
        return template.postForObject(url+"/dev/list",void.class,List.class);
    }

}

Feign

@RestController
public class ConsumerController {
    @Autowired
    private DeptClientService service;
    @RequestMapping("/consumer/get/{id}")
    public Dept getByID(@PathVariable("id") Long id){
        return this.service.queryByID(id);
    }
    @RequestMapping("/consumer/add")
    public boolean add(String dname){
        Dept dept = new Dept();
        dept.setDname(dname);
        return this.service.add(dept);
    }
    @RequestMapping("/consumer/list")
    public List<Dept> list(){
        return this.service.queryAll();
    }

}

總結(jié)

Feign的使用與Ribbon不同的地方在于

  1. Feign通過接口來實現(xiàn),更符合我們的面向接口編程的習(xí)慣
  2. 在Ribbon的Controller中我們需要將url拼接,而Feign幫我們進行了拼接

以上就是SpringCloud為服務(wù)結(jié)構(gòu)中的Feign的基本介紹和使用的詳細內(nèi)容,想要了解更多關(guān)于SpringCloud Feign的其他內(nèi)容,請關(guān)注W3Cschool其它相關(guān)文章!也希望大家能夠多多支持!

1 人點贊