Feign是通過提供面向接口操作的方式來替代RestTemplate API的Rest操作。
使用Feign
Feign這種技術(shù)應(yīng)用在服務(wù)消費端
- 修改pom.xml配置文件,加入Feign的依賴包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
- 由于Fegin是將Rest的操作轉(zhuǎn)換成接口的形式,所以我們需要新建一個接口,并在接口上聲明
@FeignClient注解
@FeignClient(value = "DEPT-PROVIDER",configuration = FeignClientConfig.class)
public interface DeptClientService {
@RequestMapping(method= RequestMethod.GET,value="/dept/get/{id}")
public Dept get(@PathVariable("id") long id) ;
@RequestMapping(method=RequestMethod.GET,value="/dept/list")
public List<Dept> list() ;
@RequestMapping(method=RequestMethod.POST,value="/dept/add")
public boolean add(Dept dept) ;
}
@Configuration
public class FeignClientConfig {
@Bean
public Logger.Level getFeignLoggerLevel() {
return feign.Logger.Level.FULL ;
}
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("admin", "admin");
}
}
其中configuration = FeignClientConfig.class不是必須的,將configuration屬性去除仍然能work。
- 將之前的Rest操作的API,替換成面向
DeptClientService接口的形式
@RestController
@RequestMapping("/consumer/dept")
public class ConsumerDeptController {
@Autowired
private DeptClientService deptClientService;
@RequestMapping(value = "/get")
public Dept get(long id) {
return this.deptClientService.get(id);
}
@RequestMapping("/list")
public List<Dept> list(){
return this.deptClientService.list();
}
@RequestMapping("/add")
public boolean add(Dept dept){
return this.add(dept);
}
/*
public static final String DEPT_GET_URL = "http://DEPT-PROVIDER/dept/get/";
public static final String DEPT_LIST_URL = "http://DEPT-PROVIDER/dept/list/";
public static final String DEPT_ADD_URL = "http://DEPT-PROVIDER/dept/add";
@Autowired
private RestTemplate restTemplate;
@Autowired
private HttpHeaders httpHeaders;
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping(value = "/get")
public Dept get(long id) {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("DEPT-PROVIDER") ;
System.out.println(
"【*** ServiceInstance ***】host = " + serviceInstance.getHost()
+ "、port = " + serviceInstance.getPort()
+ "、serviceId = " + serviceInstance.getServiceId());
//Dept dept = restTemplate.getForObject(DEPT_GET_URL + id, Dept.class);
Dept dept = restTemplate.exchange(DEPT_GET_URL+id, HttpMethod.GET,new HttpEntity<Object>(this.httpHeaders),Dept.class).getBody();
return dept;
}
@RequestMapping("/list")
public List<Dept> list(){
//List<Dept> deptList = restTemplate.getForObject(DEPT_LIST_URL, List.class);
List<Dept> deptList = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,new HttpEntity<Object>(this.httpHeaders),List.class).getBody();
return deptList;
}
@RequestMapping("/add")
public boolean add(Dept dept){
//Boolean flag = restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class);
Boolean flag = this.restTemplate.exchange(DEPT_ADD_URL,HttpMethod.POST,new HttpEntity<Object>(this.httpHeaders),Boolean.class).getBody();
return flag;
}*/
}
- 在啟動類中加入
@EnableFeignClients注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"cn.zgc.service"})
public class FeignConsumer_80_StartSpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumer_80_StartSpringCloudApplication.class,args);
}
}
Feign自帶了負責(zé)均衡特性,所以使用Feign之后可以不用使用Ribbon。
Feign的配置
Feign 最重要的功能就是將 Rest 服務(wù)的信息轉(zhuǎn)換為接口,但是在實際的使用之中也需要考慮到一些配置情況,例如:數(shù)據(jù)壓縮, Rest 的核心本質(zhì)在于: JSON 數(shù)據(jù)傳輸( XML、文本),于是就必須思考一種情況,萬一用戶發(fā)送的數(shù)據(jù)很大呢? 所以這個時候可以考慮修改application.yml 配置文件對傳輸數(shù)據(jù)進行壓縮;
feign:
compression:
request:
mime-types: # 可以被壓縮的類型
- text/xml
- application/xml
- application/json
min-request-size: 2048 # 超過2048的字節(jié)進行壓縮
開啟Feign的日志(默認是不開啟的)
logging:
level:
cn.zgc.service: DEBUG