這篇文章主要介紹“如何通過Feign去消費服務(wù)”,在日常操作中,相信很多人在如何通過Feign去消費服務(wù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何通過Feign去消費服務(wù)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

一、Feign簡介
Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創(chuàng)建一個接口并注解。
它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的編碼器和解碼器。Feign默認集成
了Ribbon,了解springcloud架構(gòu)可以加求求:三五三六二四七二五九,并和Eureka結(jié)合,默認實現(xiàn)了負載均衡的效果。
簡而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有負載均衡的能力
整合了Hystrix,具有熔斷的能力
二、準備工作
繼續(xù)用上一節(jié)的工程, 啟動eureka-server,端口為8761; 啟動service-hi 兩次,端口分別為8762 、8773.
三、創(chuàng)建一個feign的服務(wù)
新建一個spring-boot工程,取名為serice-feign,在它的pom文件引入Feign的起步依賴spring-cloud-starter-feign、
Eureka的起步依賴spring-cloud-starter-netflix-eureka-client、Web的起步依賴spring-boot-starter-web,代碼如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>service-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-feign</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.forezp</groupId> <artifactId>sc-f-chapter3</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
在工程的配置文件application.yml文件,指定程序名為service-feign,端口號為8765,服務(wù)注冊地址為http://localhost:8761/eureka/ ,代碼如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign
在程序的啟動類ServiceFeignApplication ,加上@EnableFeignClients注解開啟Feign的功能:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run( ServiceFeignApplication.class, args );
    }
}定義一個feign接口,通過@ FeignClient(“服務(wù)名”),來指定調(diào)用哪個服務(wù)。比如在代碼中調(diào)用了service-hi服務(wù)的“/hi”接口,代碼如下:
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}在Web層的controller層,對外暴露一個"/hi"的API接口,通過上面定義的Feign客戶端SchedualServiceHi 來消費服務(wù)。代碼如下:
@RestController
public class HiController {
    //編譯器報錯,無視。 因為這個Bean是在程序啟動的時候注入的,編譯器感知不到,所以報錯。
    @Autowired
    SchedualServiceHi schedualServiceHi;
    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHi.sayHiFromClientOne( name );
    }
}啟動程序,多次訪問http://localhost:8765/hi?name=forezp,瀏覽器交替顯示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
到此,關(guān)于“如何通過Feign去消費服務(wù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
                標題名稱:如何通過Feign去消費服務(wù)-創(chuàng)新互聯(lián)
                
                文章鏈接:http://www.chinadenli.net/article20/dgesjo.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、App設(shè)計、網(wǎng)站建設(shè)、建站公司、外貿(mào)建站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)