App下載

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

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

簡(jiǎn)介

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

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

使用

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

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

@FeignClient(value = "PROVIDER-NAME")
public interface DeptClientService {
// 請(qǐng)求的路徑需與服務(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來(lái)獲取所需的對(duì)象,而是通過(guò)之前定義的接口來(lái)獲取

@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();
    }

}
  • 修改啟動(dòng)類

@EnableFeignClients 開(kāi)啟Feign負(fù)載均衡

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

  • 對(duì)比:

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){
                                                //請(qǐng)求的路徑,返回的對(duì)象
        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);
        //請(qǐng)求的路徑,傳遞的參數(shù),返回的對(duì)象
        return template.postForObject(url+ "/dev/add",dept,Boolean.class);
    }
    @RequestMapping("/consumer/list")
    public List<Dept> list(){
        //請(qǐng)求的路徑,返回的對(duì)象
        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通過(guò)接口來(lái)實(shí)現(xiàn),更符合我們的面向接口編程的習(xí)慣
  2. 在Ribbon的Controller中我們需要將url拼接,而Feign幫我們進(jìn)行了拼接

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

1 人點(diǎn)贊