更新時(shí)間:2021-09-29 08:44:28 來源:動(dòng)力節(jié)點(diǎn) 瀏覽2116次
Feign 旨在使編寫 Java Http 客戶端更容易。在使用Ribbon+RestTemplate時(shí),我們使用RestTemplate對(duì)http請(qǐng)求進(jìn)行封裝,形成一組模板化的調(diào)用方法。但是在實(shí)際開發(fā)中,由于依賴服務(wù)的調(diào)用可能不止一個(gè),而且一個(gè)接口往往會(huì)在多個(gè)地方被調(diào)用,所以通常每個(gè)微服務(wù)都會(huì)封裝一些客戶端類來封裝這些依賴的服務(wù)調(diào)用。所以Feign在這個(gè)基礎(chǔ)上做了進(jìn)一步的封裝,他會(huì)幫我們定義和實(shí)現(xiàn)依賴服務(wù)接口的定義。在Feign的實(shí)現(xiàn)下,我們只需要?jiǎng)?chuàng)建一個(gè)接口,并使用注解進(jìn)行配置即可(之前Dao接口用Mapper注解標(biāo)注,現(xiàn)在是微服務(wù)接口,標(biāo)注了Feign注解),和服務(wù)提供者的接口綁定,簡(jiǎn)化了使用Spring cloud Ribbon時(shí)自動(dòng)封裝服務(wù)調(diào)用客戶端的開發(fā)量。Feign集成了Ribbon,使用Ribbon維護(hù)MicroServiceCloud-Dept的服務(wù)列表信息,通過輪詢實(shí)現(xiàn)客戶端負(fù)載均衡。與 Ribbon 不同的是,feign 只需要定義服務(wù)綁定接口,并使用聲明式方法來優(yōu)雅簡(jiǎn)單地實(shí)現(xiàn)服務(wù)調(diào)用。
1.參考80個(gè)微服務(wù)搭建microservicecloud-consumer-dept-feign
(1) 與80相比,pom.xml文件增加了feign的依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
(2)application.yml復(fù)制80
(3)新建一個(gè)主啟動(dòng)類DeptConsumer80_Feign_App.java,并添加如下注解
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;
import org.springframework.context.annotation.ComponentScan;
@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);
}
}
2.修改微服務(wù)microservicecloud-api
(1)在pom.xml文件中添加feign依賴
<依賴>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</依賴>
(2)新增接口DeptClientService.java
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)執(zhí)行clean install構(gòu)建jar包
3.微服務(wù)microservicecloud-consumer-dept-feign新建一個(gè)DeptController_Feign.java
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);
}
}
4.啟動(dòng)eureka,3個(gè)微服務(wù)實(shí)例,啟動(dòng)Feign,輸入http://localhost/consumer/dept/list,測(cè)試成功
Feign通過接口方法調(diào)用Rest服務(wù)(以前是Ribbon+RestTemplate),將請(qǐng)求發(fā)送到Eureka服務(wù)器(http://MICROSERVICECLOUD-DEPT/dept/list),通過Feign直接找到服務(wù)接口,因?yàn)楫?dāng)服務(wù)被稱為 Incorporating Ribbon 技術(shù)時(shí),它也支持負(fù)載均衡。
大家想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的SpringCloud教程,里面的內(nèi)容更加詳細(xì),希望對(duì)大家能夠有所幫。
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)