聲明接口時在代碼中通過@Resource注入容器之后即可使用。@FeignClient注解的常用屬性如下:
- value/name:value和name的作用一樣,用於指定FeignClient的名稱;如果沒有配置url,而且項目使用了Eureka 、nacos或者ribbon,name屬性會作為微服務的名稱,用於服務發現。反之,只是一個名稱。此屬性和 spring.application.name 對應。
- url:一般用於調試,作用是指定@FeignClient調用的API地址,而非從服務中心獲取。
url和name都可以使用占位符,比如:@FeignClient(name = “your.feign.name",url="${your.feign.url}”);
- decode404:當發生http 404錯誤時,如果該字段為true,會調用decoder進行解碼;否則,拋出FeignException。
- configuration:Feign配置類,作用是自定義Feign的Encoder、Decoder、LogLevel、Contract。
- fallback:定義容錯的處理類,當調用遠程接口失敗或超時時,會調用對應接口的容錯邏輯,fallback指定的類必須實現@FeignClient標記的接口。
- fallbackFactory:工廠類,用於生成fallback類實例,實現每個接口通用的容錯邏輯,減少重復的代碼。
- path:定義當前FeignClient的統一前綴。
- contextId:為某個接口設置單獨的超時,與與config里的屬性對應。
添加依賴和啟動注解
spring-boot-starter-parent版本是2.5.0,需要添加的maven坐標:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.4-SNAPSHOT</version>
</dependency>
添加啟動注解:
@SpringBootApplication
@EnableFeignClients//添加這個注解
public class XXXXApplication {
public static void main(String[] args) {
SpringApplication.run(XXXXApplication.class, args);
}
}
沙場秋點兵
首先,在yaml文件中配置常量self.github.url=https://api.github.com
,然后,創建接口類,此接口類定義了需要調用的目標服務中的方法,用Feign官方文檔中調用GitHub服務的例子來舉例。
import com.swagger.demo.bean.Contributor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
//@FeignClient(name = "github", url = "https://api.github.com")
@FeignClient(name = "github", url = "${self.github.url}",
fallback = GithubFeignClient.MyDefaultFallback.class)
public interface GithubFeignClient {
@GetMapping(value = "/repos/{owner}/{repo}/contributors")
ResponseEntity<List<Contributor>> listContributors(
@PathVariable("owner") String owner,
@PathVariable("repo") String repo);
/**
* 容錯處理類,當調用失敗時,簡單返回空list
*/
@Component
class MyDefaultFallback implements GithubFeignClient {
@Override
public ResponseEntity<List<Contributor>> listContributors(
@PathVariable("owner") String owner,
@PathVariable("repo") String repo) {
return new ResponseEntity(new ArrayList<>(), HttpStatus.BAD_REQUEST);
}
}
}
// --------- 新增實體類 -----------
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class Contributor {
/**
* 作者
*/
private String login;
/**
* 貢獻次數
*/
private int contributions;
}
上述被注釋的、使用注解@FeignClient的方法也可以使用。MyDefaultFallback類是容錯類。下面創建一個controller:
import com.swagger.demo.bean.Contributor;
import com.swagger.demo.bean.GithubDTO;
import com.swagger.demo.service.GithubFeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/github")
public class GithubClientController {
@Resource
private GithubFeignClient githubFeignClient;
@PostMapping("/listContributors")
public ResponseEntity<List<Contributor>> listContributors(@RequestBody GithubDTO githubDTO) {
return githubFeignClient.listContributors(githubDTO.getOwner(), githubDTO.getFeign());
}
}
創建idea http類型的測試用例:
### 查詢github 貢獻者信息
POST http://localhost:8087/wiener/github/listContributors
Content-Type: application/json
{
"owner": "openFeign",
"feign": "feign"
}
啟動服務,執行如上測試用例,執行結果如下所示,說明服務調用成功:
last-modified: Tue, 01 Feb 2022 03:52:53 GMT
link: <https://api.github.com/repositories/10981994/contributors?page=2>; rel="next", <https://api.github.com/repositories/10981994/contributors?page=6>; rel="last"
[
{
"login": "adriancole",
"contributions": 135
},
{
"login": "velo",
"contributions": 126
},
{
"login": "kdavisk6",
"contributions": 121
},
{
"login": "snyk-bot",
"contributions": 45
},
{
"login": "quidryan",
"contributions": 43
},
{
"login": "rspieldenner",
"contributions": 14
},
{
"login": "davidmc24",
"contributions": 12
},
{
"login": "radio-rogal",
"contributions": 12
},
{
"login": "ahus1",
"contributions": 6
},
{
"login": "spencergibb",
"contributions": 5
},
{
"login": "ashleyfrieze",
"contributions": 5
},
{
"login": "allenxwang",
"contributions": 5
},
{
"login": "gimbimloki",
"contributions": 4
},
{
"login": "jkschneider",
"contributions": 4
},
{
"login": "nmiyake",
"contributions": 4
},
{
"login": "rfalke",
"contributions": 4
},
{
"login": "SimY4",
"contributions": 3
},
{
"login": "bstick12",
"contributions": 3
},
{
"login": "mstrYoda",
"contributions": 3
},
{
"login": "jacob-meacham",
"contributions": 3
},
{
"login": "pnepywoda",
"contributions": 3
},
{
"login": "santhosh-tekuri",
"contributions": 3
},
{
"login": "jerzykrlk",
"contributions": 3
},
{
"login": "Augustine-C",
"contributions": 2
},
{
"login": "benmanbs",
"contributions": 2
},
{
"login": "schlosna",
"contributions": 2
},
{
"login": "dharmeshjogadia",
"contributions": 2
},
{
"login": "edio",
"contributions": 2
},
{
"login": "androidfred",
"contributions": 2
},
{
"login": "stromnet",
"contributions": 2
}
]
小結
以上就是這篇文章的全部內容了,希望本文對大家的學習或者工作具有一定的參考和學習價值;如果有疑問,大家可以在評論區留言交流,也希望大家多多點贊關注。謝謝大家對樓蘭胡楊的支持!