本系列對(duì)應(yīng)的是尚硅谷周陽(yáng)Spring Cloud的思維導(dǎo)圖整理的筆記,用來(lái)方便自己后面的知識(shí)點(diǎn)回顧。分別以每個(gè)知識(shí)點(diǎn)作為一篇文章詳細(xì)講述。
知識(shí)點(diǎn)傳送門(mén):
項(xiàng)目源碼
- SpringCloud01:微服務(wù)概述與SpringCloud
- SpringCloud02:Rest微服務(wù)構(gòu)建案例工程模塊
- SpringCloud03:Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)
- SpringCloud04:Ribbon負(fù)載均衡
- SpringCloud05:Feign負(fù)載均衡
- SpringCloud06:Hystrix斷路器
- SpringCloud07:zuul路由網(wǎng)關(guān)
- SpringCloud08:SpringCloud Config分布式配置中心
一、概述
官網(wǎng)解釋?zhuān)?br> http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign
Feign是一個(gè)聲明式WebService客戶(hù)端。使用Feign能讓編寫(xiě)Web Service客戶(hù)端更加簡(jiǎn)單, 它的使用方法是定義一個(gè)接口,然后在上面添加注解,同時(shí)也支持JAX-RS標(biāo)準(zhǔn)的注解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對(duì)Feign進(jìn)行了封裝,使其支持了Spring MVC標(biāo)準(zhǔn)注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負(fù)載均衡。
Feign是一個(gè)聲明式的Web服務(wù)客戶(hù)端,使得編寫(xiě)Web服務(wù)客戶(hù)端變得非常容易,
只需要?jiǎng)?chuàng)建一個(gè)接口,然后在上面添加注解即可。
參考官網(wǎng):https://github.com/OpenFeign/feign
Feign能干什么
Feign旨在使編寫(xiě)Java Http客戶(hù)端變得更容易。
前面在使用Ribbon+RestTemplate時(shí),利用RestTemplate對(duì)http請(qǐng)求的封裝處理,形成了一套模版化的調(diào)用方法。但是在實(shí)際開(kāi)發(fā)中,由于對(duì)服務(wù)依賴(lài)的調(diào)用可能不止一處,往往一個(gè)接口會(huì)被多處調(diào)用,所以通常都會(huì)針對(duì)每個(gè)微服務(wù)自行封裝一些客戶(hù)端類(lèi)來(lái)包裝這些依賴(lài)服務(wù)的調(diào)用。所以,F(xiàn)eign在此基礎(chǔ)上做了進(jìn)一步封裝,由他來(lái)幫助我們定義和實(shí)現(xiàn)依賴(lài)服務(wù)接口的定義。在Feign的實(shí)現(xiàn)下,我們只需創(chuàng)建一個(gè)接口并使用注解的方式來(lái)配置它(以前是Dao接口上面標(biāo)注Mapper注解,現(xiàn)在是一個(gè)微服務(wù)接口上面標(biāo)注一個(gè)Feign注解即可),即可完成對(duì)服務(wù)提供方的接口綁定,簡(jiǎn)化了使用Spring cloud Ribbon時(shí),自動(dòng)封裝服務(wù)調(diào)用客戶(hù)端的開(kāi)發(fā)量。
Feign集成了Ribbon
利用Ribbon維護(hù)了MicroServiceCloud-Dept的服務(wù)列表信息,并且通過(guò)輪詢(xún)實(shí)現(xiàn)了客戶(hù)端的負(fù)載均衡。而與Ribbon不同的是,通過(guò)feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡(jiǎn)單的實(shí)現(xiàn)了服務(wù)調(diào)用
二、Feign使用步驟
1.參考microservicecloud-consumer-dept-80,新建新建microservicecloud-consumer-dept-feign
修改主啟動(dòng)名:DeptConsumer80_Feign_App
2.microservicecloud-consumer-dept-feign工程pom.xml修改,主要添加對(duì)feign的支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
3.修改microservicecloud-api工程
1)POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2)新建DeptClientService接口并新增注解@FeignClient
package com.atguigu.springcloud.service;
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.atguigu.springcloud.entities.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)mvn clean、mvn install
4.microservicecloud-consumer-dept-feign工程修改Controller,添加上一步新建的DeptClientService接口
package com.atguigu.springcloud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptClientService;
@RestController
public class DeptController_Feign
{
@Autowired
private DeptClientService service = null;
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return this.service.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list()
{
return this.service.list();
}
@RequestMapping(value = "/consumer/dept/add")
public Object add(Dept dept)
{
return this.service.add(dept);
}
}
5.microservicecloud-consumer-dept-feign工程修改主啟動(dòng)類(lèi)
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})
@ComponentScan("com.atguigu.springcloud")
public class DeptConsumer80_Feign_App
{
public static void main(String[] args)
{
SpringApplication.run(DeptConsumer80_Feign_App.class, args);
}
}
6.測(cè)試
- 啟動(dòng)3個(gè)eureka集群
- 啟動(dòng)3個(gè)部門(mén)微服務(wù)8001/8002/8003
- 啟動(dòng)Feign啟動(dòng)
- 訪問(wèn)http://localhost/consumer/dept/list
- Feign自帶負(fù)載均衡配置項(xiàng)
總結(jié):Feign通過(guò)接口的方法調(diào)用Rest服務(wù)(之前是Ribbon+RestTemplate),
該請(qǐng)求發(fā)送給Eureka服務(wù)器(http://MICROSERVICECLOUD-DEPT/dept/list),通過(guò)Feign直接找到服務(wù)接口,由于在進(jìn)行服務(wù)調(diào)用的時(shí)候融合了Ribbon技術(shù),所以也支持負(fù)載均衡作用。