feign 是一個讓rest服務調用更簡潔的開源項目,很多介紹文章或書也稱它為聲明式REST調用。傳統的web service中,通過引用wsdl來自動生成一些client的代理類(或stub代碼),feign跟這個有點類似,但是更靈活。
先回顧一下,上節中service-consumer對服務的調用代碼:
1 @GetMapping("/order/{userId}/{orderNo}") 2 public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) { 3 UserDTO user = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user/" + userId, UserDTO.class).getBody(); 4 if (user != null) { 5 return user.getUserName() + " 的訂單" + orderNo + " 找到啦!"; 6 } 7 8 return "用戶不存在!"; 9 }
如果調用的參數比較多,調用的代碼會充斥着很多拼裝參數這樣的代碼,不太優雅。另外還有一個問題,通常為了安全起見,一些服務(或服務中的某些方法)可能要求認證后,才能調用,如果每個調用的地方,都要調用登錄之類的服務來處理,這類與業務無關的代碼就會大量侵入業務邏輯中,不好維護。
下面看看用feign如何改進:
一、添加依賴引用
compile 'org.springframework.cloud:spring-cloud-starter-feign'
二、定義feignClient接口
package com.cnblogs.yjmyzz.spring.cloud.study.service.client;
import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;
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;
@FeignClient(name = "service-provider-demo", configuration = BasicAuthConfiguration.class)
public interface UserFeignClient {
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
UserDTO findUser(@PathVariable("id") Integer userId);
}
這里面一個BasicAuthConfiguration類,也是自己寫的
package com.cnblogs.yjmyzz.spring.cloud.study.service.client;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BasicAuthConfiguration {
@Bean
public BasicAuthRequestInterceptor basicAuthorizationInterceptor() {
return new BasicAuthRequestInterceptor("app01", "passwd01");
}
}
這上面的app01, passwd01,就是服務端分配的用戶名及密碼
附:服務提供方的application.yml中可參考下面這樣設置
security:
basic:
enabled: true
user:
name: app01
password: passwd01
三、feignClient的使用
package com.cnblogs.yjmyzz.spring.cloud.study.service.controller;
import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;
import com.cnblogs.yjmyzz.spring.cloud.study.service.client.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/order/{userId}/{orderNo}")
public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) {
UserDTO user = userFeignClient.findUser(userId);
if (user != null) {
return user.getUserName() + " 的訂單" + orderNo + " 找到啦!";
}
return "用戶不存在!";
}
}
最后,main入口類上要增加一個注解
package com.cnblogs.yjmyzz.spring.cloud.study.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ServiceConsumer {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumer.class, args);
}
}
起作用的主要是@EnableFeignClients這個注解。
