SpringCloud實戰(zhàn)之Feign聲明式服務(wù)調(diào)用
在前面的文章中可以發(fā)現(xiàn)當(dāng)我們通過RestTemplate調(diào)用其它服務(wù)的API時,所需要的參數(shù)須在請求的URL中進行拼接,如果參數(shù)少的話或許我們還可以忍受,一旦有多個參數(shù)的話,這時拼接請求字符串就會效率低下,并且顯得好傻。
那么有沒有更好的解決方案呢?答案是確定的有,Netflix已經(jīng)為我們提供了一個框架:Feign。
Feign是一個聲明式的Web Service客戶端,它的目的就是讓W(xué)eb Service調(diào)用更加簡單。Feign提供了HTTP請求的模板,通過編寫簡單的接口和插入注解,就可以定義好HTTP請求的參數(shù)、格式、地址等信息。
而Feign則會完全代理HTTP請求,我們只需要像調(diào)用方法一樣調(diào)用它就可以完成服務(wù)請求及相關(guān)處理。Feign整合了Ribbon和Hystrix(關(guān)于Hystrix我們后面再講),可以讓我們不再需要顯式地使用這兩個組件。
總起來說,F(xiàn)eign具有如下特性:
- 可插拔的注解支持,包括Feign注解和JAX-RS注解;
- 支持可插拔的HTTP編碼器和解碼器;
- 支持Hystrix和它的Fallback;
- 支持Ribbon的負載均衡;
- 支持HTTP請求和響應(yīng)的壓縮。
這看起來有點像我們springmvc模式的Controller層的RequestMapping映射。這種模式是我們非常喜歡的。Feign是用@FeignClient來映射服務(wù)的。
首先第一步,在原來的基礎(chǔ)上新建一個Feign模塊,接著引入相關(guān)依賴,引入Feign依賴,會自動引入Hystrix依賴的,如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
application.yml配置如下:
server: port: 8083 spring: application: name: feign-consumer eureka: client: service-url: defaultZone: http://localhost:8888/eureka/,http://localhost:8889/eureka/
接著在前面文章中的的的兩個provider1和provider2兩個模塊的服務(wù)新增幾個方法,如下代碼所示:
/**
* Created by cong on 2018/5/8.
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("訪問來1了......");
return "hello1";
}
@RequestMapping("/hjcs")
public List<String> laowangs(String ids){
List<String> list = new ArrayList<>();
list.add("laowang1");
list.add("laowang2");
list.add("laowang3");
return list;
}
//新增的方法
@RequestMapping(value = "/hellol", method= RequestMethod.GET)
public String hello(@RequestParam String name) {
return "Hello " + name;
}
@RequestMapping(value = "/hello2", method= RequestMethod.GET)
public User hello(@RequestHeader String name, @RequestHeader Integer age) {
return new User(name, age);
}
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
public String hello (@RequestBody User user) {
return "Hello "+ user. getName () + ", " + user. getAge ();
}
}
接著是上面代碼所需用到的User類,代碼如下:
/**
* Created by cong 2017/12/2.
*/
public class User {
private String name;
private Integer age;
//序列化傳輸?shù)臅r候必須要有空構(gòu)造方法,不然會出錯
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
接下來用Feign的@FeignClient(“服務(wù)名稱”)映射服務(wù)調(diào)用。代碼如下:
package hjc;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
/**
* Created by cong on 2018/5/17.
*/
//configuration = xxx.class 這個類配置Hystrix的一些精確屬性
//value=“你用到的服務(wù)名稱”
@FeignClient(value = "hello-service",fallback = FeignFallBack.class)
public interface FeignService {
//服務(wù)中方法的映射路徑
@RequestMapping("/hello")
String hello();
@RequestMapping(value = "/hellol", method= RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method= RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method= RequestMethod.POST)
String hello(@RequestBody User user);
}
接著在Controller層注入FeiService這個接口,進行遠程服務(wù)調(diào)用,代碼如下:
/**
* Created by cong on 2018/5/17.
*/
@RestController
public class ConsumerController {
@Autowired
FeignService feignService;
@RequestMapping("/consumer")
public String helloConsumer(){
return feignService.hello();
}
@RequestMapping("/consumer2")
public String helloConsumer2(){
String r1 = feignService.hello("hjc");
String r2 = feignService.hello("hjc", 23).toString();
String r3 = feignService.hello(new User("hjc", 23));
return r1 + "-----" + r2 + "----" + r3;
}
}
接著在Feign模塊的啟動類哪里打上Eureka客戶端的注解@EnableDiscoveryClient Feign客戶端的注解
@EnableFeignClients,代碼如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
接著啟動啟動類,瀏覽器上輸入localhost:8083/consumer 運行結(jié)果如下:


可以看到負載均衡輪詢出現(xiàn)hello1,hello2。
接著繼續(xù)在瀏覽器上輸入localhost:8083/consumer2,運行結(jié)果如下:

接下來我們進行Feign聲明式調(diào)用服務(wù)下的,服務(wù)降級的使用,那么我們就必須新建一個FeignFallBack類來繼承FeiService,代碼如下:
package hjc;
import org.springframework.stereotype.Component;
/**
* Created by cong on 2018/5/17.
*/
@Component
public class FeignFallBack implements FeignService{
//實現(xiàn)的方法是服務(wù)調(diào)用的降級方法
@Override
public String hello() {
return "error";
}
@Override
public String hello(String name) {
return "error";
}
@Override
public User hello(String name, Integer age) {
return new User();
}
@Override
public String hello(User user) {
return "error";
}
}
接著我們再把那兩個服務(wù)提供模塊provider1,provider2模塊進行停止,運行結(jié)果如下所示:

可以看到我們這幾個調(diào)用,都進行了服務(wù)降級了。
那么如果我們想精確的控制一下Hystrix的參數(shù)也是可以的,比方說跟Hystrix結(jié)合的參數(shù),那么可以在FeignClient注解里面配置一個Configuration=XXX類.class屬性,在哪個類里面精確的指定一下屬性。
或者在application.yml里面配置,如下:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutinMilliseconds: 5000
ribbon:
connectTimeout: 500
#如果想對單獨的某個服務(wù)進行詳細配置,如下
hello-service:
ribbon:
connectTimeout: 500
這里滿足了我們大部分場景的調(diào)用,但是有寫精細場景,還是要用原生的Hystrix,跟我們之前的Hystrix用法一下,不要走Feign客戶端調(diào)用就行了,如下:
/**
* Created by cong on 2018/5/17.
*/
public class HjcCommand extends HystrixCommand {
protected HjcCommand(HystrixCommandGroupKey group) {
super(group);
}
@Override
protected Object run() throws Exception {
return null;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法
這篇文章主要介紹了java 中JFinal getModel方法和數(shù)據(jù)庫使用出現(xiàn)問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
java:程序包org.apache.ibatis.annotations不存在報錯解決
這篇文章主要給大家介紹了關(guān)于java:程序包org.apache.ibatis.annotations不存在報錯的解決方法,這個錯誤是我在直接導(dǎo)入springboot項目的時候報錯的,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-04-04
SpringBoot整合flyway實現(xiàn)自動創(chuàng)建表的方法
這篇文章主要介紹了SpringBoot整合flyway實現(xiàn)自動創(chuàng)建表的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
關(guān)于SpringMVC中數(shù)據(jù)綁定@ModelAttribute注解的使用
這篇文章主要介紹了關(guān)于SpringMVC中數(shù)據(jù)綁定@ModelAttribute注解的使用,SpringMVC是一個基于Spring框架的Web框架,它提供了一種簡單、靈活的方式來開發(fā)Web應(yīng)用程序,在開發(fā)Web應(yīng)用程序時,我們需要將用戶提交的數(shù)據(jù)綁定到我們的Java對象上,需要的朋友可以參考下2023-07-07
@Configuration與@Component作為配置類的區(qū)別詳解
這篇文章主要介紹了@Configuration與@Component作為配置類的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

