更新時間:2021-09-29 08:44:28 來源:動力節(jié)點(diǎn) 瀏覽1653次
Feign 旨在使編寫 Java Http 客戶端更容易。在使用Ribbon+RestTemplate時,我們使用RestTemplate對http請求進(jìn)行封裝,形成一組模板化的調(diào)用方法。但是在實(shí)際開發(fā)中,由于依賴服務(wù)的調(diào)用可能不止一個,而且一個接口往往會在多個地方被調(diào)用,所以通常每個微服務(wù)都會封裝一些客戶端類來封裝這些依賴的服務(wù)調(diào)用。所以Feign在這個基礎(chǔ)上做了進(jìn)一步的封裝,他會幫我們定義和實(shí)現(xiàn)依賴服務(wù)接口的定義。在Feign的實(shí)現(xiàn)下,我們只需要創(chuàng)建一個接口,并使用注解進(jìn)行配置即可(之前Dao接口用Mapper注解標(biāo)注,現(xiàn)在是微服務(wù)接口,標(biāo)注了Feign注解),和服務(wù)提供者的接口綁定,簡化了使用Spring cloud Ribbon時自動封裝服務(wù)調(diào)用客戶端的開發(fā)量。Feign集成了Ribbon,使用Ribbon維護(hù)MicroServiceCloud-Dept的服務(wù)列表信息,通過輪詢實(shí)現(xiàn)客戶端負(fù)載均衡。與 Ribbon 不同的是,feign 只需要定義服務(wù)綁定接口,并使用聲明式方法來優(yōu)雅簡單地實(shí)現(xiàn)服務(wù)調(diào)用。
1.參考80個微服務(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)新建一個主啟動類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新建一個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.啟動eureka,3個微服務(wù)實(shí)例,啟動Feign,輸入http://localhost/consumer/dept/list,測試成功
Feign通過接口方法調(diào)用Rest服務(wù)(以前是Ribbon+RestTemplate),將請求發(fā)送到Eureka服務(wù)器(http://MICROSERVICECLOUD-DEPT/dept/list),通過Feign直接找到服務(wù)接口,因?yàn)楫?dāng)服務(wù)被稱為 Incorporating Ribbon 技術(shù)時,它也支持負(fù)載均衡。
大家想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點(diǎn)的SpringCloud教程,里面的內(nèi)容更加詳細(xì),希望對大家能夠有所幫。
初級 202925
初級 203221
初級 202629
初級 203743